Groovy – Command Line

  • Post author:
  • Post category:Groovy
  • Post comments:0 Comments

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 parameterFull NameDetails
-C–color[=FLAG]Enable or disable use of ANSI colors
-D–define=NAME=VALUEDefine a system property
-T–terminal=TYPESpecify the terminal TYPE to use
-V–versionDisplay the version
-classpathSpecify where to find the class files – must be the first argument
-cp–classpathAliases for ‘-classpath’
-d–debug–debug Enable debug output
-e–evaluate=argEvaluate option fist when starting interactive session
-h–helpDisplay this help message
-q–quietSuppress superfluous output
-v–verboseEnable 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.

Classes and Functions

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.

command line

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.

command line

Commands

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.NoCommand &smp; Command Description
1:help(:h ) Display this help message
2?(:? ) Alias to: :help
3:exit(:x ) Exit the shell
4:quit(:q ) Alias to: :exit
5import(:i ) Import a class into the namespace
6:display(:d ) Display the current buffer
7:clear(:c ) Clear the buffer and reset the prompt counter
8:show(:S ) Show variables, classes or imports
9:inspect(:n ) Inspect a variable or the last result with the GUI object browser
10:purge(:p ) Purge variables, classes, imports or preferences
11:edit(:e ) Edit the current buffer
12:load(:l ) Load a file or URL into the buffer
13.(:. ) Alias to: :load
14.save(:s ) Save the current buffer to a file
15.record(:r ) Record the current session to a file
16:alias(:a ) Create an alias
17:set(:= ) Set (or list) preferences
18:register(:rc) Registers a new command with the shell
19:doc(:D ) Opens a browser window displaying the doc for the argument
20:history(:H ) Display, manage and recall edit-line history

Next Topic:-Click Here

Leave a Reply