Subsecciones

Comandos

La shell dispone de un cierto número de comandos que proporcionan acceso a su entorno. Cada comando tiene una forma abreviada (algo como \h). Los usuarios pueden crear sus propios alias.

help

Ya vimos el comando help que nos permite ver la lista de comandos:

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 preferences
  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

exit

Nos permite salir de la shell. Esta es la forma de salir de la shell. Es posible salir pulsando ctrl-C pero la shell se quejará:

groovy:000> exit
casiano@tonga:~/src/groovy$ groovysh 
Groovy Shell (, JVM: 1.6.0_0-b11)
Type 'help' or '\h' for help.
--------------------------------------------
groovy:000> quit
casiano@tonga:~/src/groovy$ groovysh 
Groovy Shell (, JVM: 1.6.0_0-b11)
Type 'help' or '\h' for help.
---------------------------------------------
groovy:000> ^C
WARNING: Abnormal JVM shutdown detected
casiano@tonga:~/src/groovy$

import

Para añadir un import que será incluido en las evaluaciones de la shell.

groovy:000> import java.util.regex.Pattern
groovy:000> p = Pattern.compile("a+b")        
===> a+b
groovy:000> m = p.matcher("aaaaab")
===> java.util.regex.Matcher[pattern=a+b region=0,6 lastmatch=]
groovy:000> b = m.matches()
===> true
groovy:000> m = p.matcher("b")      
===> java.util.regex.Matcher[pattern=a+b region=0,1 lastmatch=]
groovy:000> b = m.matches()   
===> false

display

Muestra los contenidos del buffer actual.

Muestra únicamente el buffer de una expresión incompleta.

groovy:000> class Foo {
groovy:001>   def bar
groovy:002> def baz () {
groovy:003> display
 001> class Foo {
 002>   def bar
 003> def baz () {
groovy:003>   println "Hola"
groovy:004> }}
===> true
groovy:000> display
Buffer is empty

clear

Limpia el buffer actual
groovy:000> class Foo {
groovy:001> display
 001> class Foo {
groovy:001> clear
groovy:000> display
Buffer is empty

show

Muestra variables, clases, preferencias o imports:

groovy:000> a=4
===> 4
groovy:000> show variables
Variables:
  a = 4
  _ = 4
groovy:000> class Foo {}
===> true
groovy:000> show classes
Classes:
  class Foo
groovy:000> import java.util.regex.Pattern
groovy:000> show imports
Custom imports:
  import java.util.regex.Pattern
groovy:000> set verbosity VERBOSE
groovy:000> show preferences
Preferences:
    editor=/Applications/TextEdit.app/Contents/MacOS/TextEdit
    editor="/usr/bin/vim"=true
    verbosity=VERBOSE
groovy:000> show all
Variables:
  a = 4
  _ = true
Classes:
  class Foo
Custom imports:
  import java.util.regex.Pattern
Preferences:
    editor=/Applications/TextEdit.app/Contents/MacOS/TextEdit
    editor="/usr/bin/vim"=true
    verbosity=VERBOSE
===> [variables, classes, imports, preferences, all]

inspect

Abre un browser gráfico que muestra el resultado de la última evaluación. Este browser es una interfaz de usario Swing que nos permite navegar en la API del objeto.

groovy:000> a = [ 1, 2, [3, 4], "a" ]
===> [1, 2, [3, 4], a]
groovy:000> inspect
groovy:000> h = [ a : 1, b : [3, 4], c : 5 ]
===> {a=1, b=[3, 4], c=5}
groovy:000> inspect
groovy:000> r = 1..5
===> 1..5
groovy:000> inspect

Podemos ver entonces los métodos del objeto y como usarlos:

groovy:000> h.size()        
===> 3
groovy:000> h.each {k,v -> println "$k -> $v" }
a -> 1
b -> [3, 4]
c -> 5
===> {a=1, b=[3, 4], c=5}
groovy:000>

purge

Suprime objetos de la shell.

Véase un ejemplo:

groovy:000> show variables
Variables:
  b = 5
  a = 4
  _ = 5
groovy:000> purge variables 
Custom variables purged
groovy:000> show variables   
No variables defined

edit

Permite editar el buffer de trabajo con un editor externo. sólo funciona en Unix y debe establecerse la variable EDITOR.

casiano@tonga:~$ export EDITOR='gvim -f'
casiano@tonga:~$ groovysh
aGroovy Shell (, JVM: 1.6.0_0-b11)
Type 'help' or '\h' for help.
-----------------------------------
groovy:000> a =
groovy:001> display
 001> a =
groovy:001> edit
 001> a =
 002> a = "hello world"
===> hello world
groovy:000> a
 001> a
===> hello world

La opción -f de gvim hace que no se cree un proceso aparte.

load

Carga uno o mas ficheros en el buffer.

Consideremos el siguiente programa Java:

casiano@tonga:~/src/groovy$ cat Greetings.groovy 
public class Greetings {
  public static void main(String[] args) {
    for(int i = 0; i < 3; i++) {
      System.out.print("jo! ");
    }
    System.out.println(".... ");
  }
}
Podemos ejecutarlo con groovy usando el comando load de la shell:
casiano@tonga:~/src/groovy$ groovysh
Groovy Shell (, JVM: 1.6.0_0-b11)
Type 'help' or '\h' for help.
-----------------------------------------
groovy:000> load Greetings. (pressed tab key)

Greetings.class     Greetings.groovy    Greetings.java
groovy:000> load Greetings.groovy 
Loading: file:/home/casiano/src/groovy/Greetings.groovy
 001> public class Greetings {
 002>   public static void main(String[] args) {
 003>     for(int i = 0; i < 3; i++) {
 004>       System.out.print("jo! ");
 005>     }
 006>     System.out.println(".... ");
 007>   }
 008> }
===> true
groovy:000> Greetings.main()
 001> Greetings.main()
jo! jo! jo! .... 
===> null

Ficheros asociados

Casiano Rodríguez León
2010-04-30