Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

The Groovy Shell, aka. groovysh is a command-line application which allows easy access to evaluate Groovy expressions, define classes and run simple experiments.

Table of Contents
maxLevel3
minLevel2

Features

  • No need for go command to execute buffer.
  • Rich cross-platform edit-line editing, history and completion thanks to JLine.
  • ANSI colors (prompt, exception traces, etc).
  • Simple, yet robust, command system with online help, user alias support and more.
  • User profile support

...

The shell supports several options to control verbosity, ANSI coloring and other features.

No Format

./bin/groovysh --help
usage: groovysh [options] [...]
  -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
  -d, --debug                Enable debug output
  -h, --help                 Display this help message
  -q, --quiet                Suppress superfluous output
  -v, --verbose              Enable verbose output

...

Execute a Command
No Format

./bin/groovysh 'show preferences'
No preferences are set
Evaluate an Expression
No Format

./bin/groovysh 'System.properties.each { k, v -> println("$k = $v") }'
java.runtime.name = Java(TM) 2 Runtime Environment, Standard Edition
sun.boot.library.path = /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Libraries
java.vm.version = 1.5.0_07-87
awt.nativeDoubleBuffering = true
gopherProxySet = false
...

...

Simple Expressions

No Format

println "Hello"

Evaluation Result

...

Multi-line/complex expressions (like closure or class definitions) may be defined over several lines. When the shell detects that it has a complete expression it will compile and evaluate it.

Define a Class
No Format

class Foo {
    def bar() {
        println "baz"
    }
}
Use the Class
No Format

foo = new Foo()
foo.bar()

...

This will set a shell variable:

No Format

foo = "bar"

But, this will evaluate a local variable and will not be saved to the shell's environment:

No Format

def foo = "bar"

Functions

Functions can be defined in the shell, and will be saved for later use.

Defining a function is easy:

No Format

groovy:000> def hello(name) {
groovy:001> println("Hello $name")
groovy:002> }

And then using it is as one might expect:

No Format

hello("Jason")
Note

Internally the shell creates a closure to encapsulate the function and then binds the closure to a variable. So variables and functions share the same namespace.

...

The Command List
No Format

groovy:000> help

For information about Groovy, visit:
    http://groovy.codehaus.org

Available commands:
  help     (\h ) Display this help message
  ?        (\? ) Alias to: help
  exit     (\x ) Exit the shell
  quit     (\q ) Alias to: exit
  import   (\i ) Import a class into the namespace
  display  (\d ) Display the current buffer
  clear    (\c ) Clear the buffer
  show     (\S ) Show variables, classes or imports
  inspect  (\n ) Inspect a variable or the last result with the GUI object browser
  purge    (\p ) Purge variables, classes, imports or buffers
  edit     (\e ) Edit the current buffer
  load     (\l ) Load a file or URL into the buffer
  .        (\. ) Alias to: load
  save     (\s ) Save the current buffer to a file
  record   (\r ) Record the current session to a file
  history  (\H ) Display, manage and recall edit-line history
  alias    (\a ) Create an alias
  set      (\= ) Set (or list) preferences

For help on a specific command type:
    help <command>

...

While in the interactive shell, you can ask for help for any command to get more details about its syntax or function. Here is an example of what happens when you ask for help for the help command:

No Format

groovy:000> help help

usage: help [<command>]

Display the list of commands or the help text for <command>.

...

This only displays the buffer of an incomplete expression. Once the expression is complete, the buffer is rest. The prompt will update to show the size of the current buffer as well.

Example
No Format

groovy:000> class Foo {
groovy:001> def bar
groovy:002> def baz() {
groovy:003> display
 001> class Foo {
 002> def bar
 003> def baz() {

clear

Clear the current buffer.s the current buffer, resetting the prompt counter to 000. Can be used to recover from compilation errors.

show

Show variables, classes or preferences or imports.

show variables
No Format

groovy:000> show variables
Variables:
  _ = true

...

Tip
titleMac OS X

To use TextEdit, the default text editor on Mac OS X, configure:

No Format

set editor /Applications/TextEdit.app/Contents/MacOS/TextEdit

Setting a Preference

No Format

set verbosity DEBUG

Listing Preferences

To list the current set preferences (and their values):

No Format

show preferences
Note
titleLimitation

At the moment, there is no way to list all of the known/available preferences to be set.

Clearing Preferences (ie. Resetting to Defaults)

No Format

purge preferences

User Profile Scripts and State

...

These shots have been taken over the development of the new shell, so some of the content might look slightly different. Also, note the yellow colors here are the shell's bold color, so the colors might look different depending on how the enclosing shell has its colors setup.

Image Modified

Image Modified

Image Modified

Troubleshooting

...

  • none
  • false
  • off
  • jline.UnsupportedTerminal
No Format

groovysh --terminal=none

Problems with Cygwin on Windows

Some people have issues when running groovysh with cygwin. If you have troubles, the following may help:

No Format

stty -icanon min 1 -echo
groovysh --terminal=unix
stty icanon echo

...