Skip to content

Commit d81b366

Browse files
committed
GROOVY-8162: Update Groovysh to JLine3 (fancy banner and begin reworking CLI options)
1 parent 76fc705 commit d81b366

File tree

2 files changed

+81
-3
lines changed
  • subprojects/groovy-groovysh/src/main

2 files changed

+81
-3
lines changed

subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/Main.groovy

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.apache.groovy.groovysh
2020

21+
import groovy.cli.internal.CliBuilderInternal
22+
import groovy.cli.internal.OptionAccessor
2123
import org.apache.groovy.groovysh.jline.GroovyBuiltins
2224
import org.apache.groovy.groovysh.jline.GroovyCommands
2325
import org.apache.groovy.groovysh.jline.GroovyConsoleEngine
@@ -180,6 +182,39 @@ class Main {
180182
}
181183

182184
static void main(String[] args) {
185+
def cli = new CliBuilderInternal(usage: 'groovysh [options] [...]', stopAtNonOption: false,
186+
header: messages['cli.option.header'])
187+
cli.with {
188+
_(names: ['-cp', '-classpath', '--classpath'], messages['cli.option.classpath.description'])
189+
h(longOpt: 'help', messages['cli.option.help.description'])
190+
V(longOpt: 'version', messages['cli.option.version.description'])
191+
v(longOpt: 'verbose', messages['cli.option.verbose.description'])
192+
q(longOpt: 'quiet', messages['cli.option.quiet.description'])
193+
d(longOpt: 'debug', messages['cli.option.debug.description'])
194+
e(longOpt: 'evaluate', args: 1, argName: 'CODE', optionalArg: false, messages['cli.option.evaluate.description'])
195+
C(longOpt: 'color', args: 1, argName: 'FLAG', optionalArg: true, messages['cli.option.color.description'])
196+
D(longOpt: 'define', type: Map, argName: 'name=value', messages['cli.option.define.description'])
197+
T(longOpt: 'terminal', args: 1, argName: 'TYPE', messages['cli.option.terminal.description'])
198+
pa(longOpt: 'parameters', messages['cli.option.parameters.description'])
199+
pr(longOpt: 'enable-preview', messages['cli.option.enable.preview.description'])
200+
}
201+
OptionAccessor options = cli.parse(args)
202+
203+
if (options == null) {
204+
// CliBuilder prints error, but does not exit
205+
System.exit(22) // Invalid Args
206+
}
207+
208+
if (options.h) {
209+
cli.usage()
210+
System.exit(0)
211+
}
212+
213+
if (options.V) {
214+
println render(messages.format('cli.info.version', GroovySystem.version))
215+
System.exit(0)
216+
}
217+
183218
try {
184219
Supplier<Path> workDir = () -> Paths.get(System.getProperty('user.dir'))
185220
DefaultParser parser = new DefaultParser(
@@ -284,11 +319,16 @@ class Main {
284319
keyMap.bind(new Reference(Widgets.AUTOSUGGEST_TOGGLE), KeyMap.alt("v"))
285320
def init = configPath.getUserConfig('groovysh_init')
286321
if (init) {
287-
systemRegistry.initialize(configPath.getUserConfig('groovysh_init').toFile())
322+
systemRegistry.setConsoleOption() // initialize(configPath.getUserConfig('groovysh_init').toFile())
288323
}
289324

290-
println render(messages.format('startup_banner.0', GroovySystem.version, System.properties['java.version'], terminal.type))
291-
println render(messages['startup_banner.1'])
325+
if (options.q) {
326+
println render(messages.format('cli.info.version', GroovySystem.version))
327+
} else {
328+
println render(messages.format('startup_banner.0', GroovySystem.version, System.properties['java.version'], terminal.type))
329+
println render(messages['startup_banner.1'])
330+
println render(messages['startup_banner.2'])
331+
}
292332
println '-' * (terminal.width - 1)
293333
// for debugging
294334
// def index = 0

subprojects/groovy-groovysh/src/main/resources/org/apache/groovy/groovysh/Main.properties

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,41 @@
2020
startup_banner.0=@|green Groovy Shell|@ ({0}, JVM: {1}, Term: {2})
2121

2222
startup_banner.1=Type '@|bold /help|@' or '@|bold /h|@' for help, '@|bold /exit|@' or '@|bold /x|@' to exit.
23+
24+
startup_banner.2=\
25+
@|red |@@|yellow |@@|green |@@|cyan |@@|blue |@@|magenta _ |@\n\
26+
@|red __ _ |@@|yellow _ __ |@@|green ___ ___|@@|cyan __ __|@@|blue _ _|@@|magenta ___| |__ |@\n\
27+
@|red / _` ||@@|yellow '__/|@@|green _ \\ / _ \\|@@|cyan \\ / /|@@|blue | | |@@|magenta / __| '_ \\ |@\n\
28+
@|red | (_| ||@@|yellow | |@@|green | (_) | (_) \\|@@|cyan V /|@@|blue | |_| |@@|magenta \\__ \\ | | | |@\n\
29+
@|red \\__, ||@@|yellow _| |@@|green \\___/ \\___/ |@@|cyan \\_/ |@@|blue \\__, |@@|magenta |___/_| |_| |@\n\
30+
@|red |___/ |@@|yellow |@@|green |@@|cyan |@@|blue |___/ |@
31+
32+
cli.option.header=The Groovy Shell, aka groovysh, is a command-line application which allows easy access to evaluate Groovy expressions, define classes and run simple experiments.
33+
34+
cli.option.help.description=Display this help message
35+
36+
cli.option.version.description=Display the version
37+
38+
cli.option.verbose.description=Enable verbose output
39+
40+
cli.option.quiet.description=Suppress superfluous output
41+
42+
cli.option.debug.description=Enable debug output
43+
44+
cli.option.evaluate.description=Evaluate the code first when starting interactive session
45+
46+
cli.option.cp.description=Aliases for '-classpath'
47+
48+
cli.option.classpath.description=Specify where to find the class files - must be first argument
49+
50+
cli.option.color.description=Enable or disable use of ANSI colors
51+
52+
cli.option.define.description=Define a system property
53+
54+
cli.option.terminal.description=Specify the terminal TYPE to use
55+
56+
cli.option.enable.preview.description=Enable preview Java features (jdk12+ only) - must be after classpath but before other arguments
57+
58+
cli.option.parameters.description=Generate metadata for reflection on method parameter names (jdk8+ only)
59+
60+
cli.info.version=@|red g|@@|yellow r|@@|green oo|@@|cyan v|@@|blue y|@@|magenta sh|@ {0}

0 commit comments

Comments
 (0)