The Groovy shell known as groovysh can be easily used to evaluate groovy expressions, define classes and run simple programs. The command line shell gets installed when Groovy is installed.
Following are the command line options available in Groovy −
|Command line parameter||Full Name||Details|
|-C||--color[=FLAG]||Enable or disable use of ANSI colors|
|-D||--define=NAME=VALUE||Define a system property|
|-T||--terminal=TYPE||Specify the terminal TYPE to use|
|-V||--version||Display the version|
|-classpath||Specify where to find the class files – must be the first argument|
|-cp||--classpath||Aliases for '-classpath'|
|-d||--debug||--debug Enable debug output|
|-e||--evaluate=arg||Evaluate option fist when starting interactive session|
|-h||--help||Display this help message|
|-q||--quiet||Suppress superfluous output|
|-v||--verbose||Enable verbose output|
The following snapshot shows a simple example of an expression being executed in the Groovy shell. In the following example we are just printing “Hello World” in the groovy shell.
It is very easy to define a class in the command prompt, create a new object and invoke a method on the class. The following example shows how this can be implemented. In the following example, we are creating a simple Student class with a simple method. In the command prompt itself, we are creating an object of the class and calling the Display method.
It is very easy to define a method in the command prompt and invoke the method. Note that the method is defined using the def type. Also note that we have included a parameter called name which then gets substituted with the actual value when the Display method is called. The following example shows how this can be implemented.
The shell has a number of different commands, which provide rich access to the shell’s environment. Following is the list of them and what they do.
|Sr.No||Command &smp; Command Description|
(:h ) Display this help message
(:? ) Alias to: :help
(:x ) Exit the shell
(:q ) Alias to: :exit
(:i ) Import a class into the namespace
(:d ) Display the current buffer
(:c ) Clear the buffer and reset the prompt counter
(:S ) Show variables, classes or imports
(:n ) Inspect a variable or the last result with the GUI object browser
(:p ) Purge variables, classes, imports or preferences
(:e ) Edit the current buffer
(:l ) Load a file or URL into the buffer
(:. ) Alias to: :load
(:s ) Save the current buffer to a file
(:r ) Record the current session to a file
(:a ) Create an alias
(:= ) Set (or list) preferences
(:rc) Registers a new command with the shell
(:D ) Opens a browser window displaying the doc for the argument
(:H ) Display, manage and recall edit-line history