Fullmenu null

 

30 August 2017

You can have the situation to having a script that performs certain functions that we want to parameterize.

The easiest option is to use the variable * args * implicit in the script that is an array of String that contains the parameters that are passed after the filename.

However, you also have CliBuilder, a Groovy utility that allows you to make the arguments that you can pass to a script be more explicit.

A very basic script using this tool could be the following:

def cli = new CliBuilder(usage: 'groovy CliBuilder.groovy -[habcd]')

cli.with { // (1)
     h(longOpt: 'help',    'Usage Information \n', required: false)
     a(longOpt: 'Option a','Al seleccionar "a" pinta seleccionada -> a ', required: false)
     b(longOpt: 'Option b','Al seleccionar "b" pinta seleccionada -> b ', required: false)
     c(longOpt: 'Option c','Al seleccionar "c" pinta seleccionada -> c ', required: false)
     d(longOpt: 'Option d','Al seleccionar "d" pinta seleccionada -> d ', required: false)
}

def options = cli.parse(args)

if (!options) {
    return
}
if (options.h) { // (2)
    cli.usage()
    return
}
if (options.a) { // (3)
    println "------------------------------------------------------------------"
    println "Seleccionada ha sido la 'a'"
    println "------------------------------------------------------------------"
}
if (options.b) {
    println "------------------------------------------------------------------"
    println "Seleccionada ha sido la 'b'"
    println "------------------------------------------------------------------"
}
if (options.c) {
    println "------------------------------------------------------------------"
    println "Seleccionada ha sido la 'c'"
    println "------------------------------------------------------------------"
}
if (options.d) {
    println "------------------------------------------------------------------"
    println "Seleccionada ha sido la 'd'"
    println "------------------------------------------------------------------"
}
  1. We define the parameters that we will need.

  2. It will show the legend of our command.

  3. This part is executed when sending as parameter -a.

If for example we call our script passing the parameter -h we will get the legend of our command:

groovy clibuilder_ebook.groovy  -h
usage: clibuilder_ebook.groovy -[habcd]
 -a,--Option a   When selecting "a" write seleccionada -> a
 -b,--Option b   When selecting "b" write seleccionada -> b
 -c,--Option c   When selecting "c" write seleccionada -> c
 -d,--Option d   When selecting "d" write seleccionada -> d
 -h,--help       Usage Information

Script
def cli = new CliBuilder(usage: 'groovy CliBuilder.groovy -[habcd]')

cli.with { // (1)
     h(longOpt: 'help',    'Usage Information \n', required: false)
     a(longOpt: 'Option a','Al seleccionar "a" pinta seleccionada -> a ', required: false)
     b(longOpt: 'Option b','Al seleccionar "b" pinta seleccionada -> b ', required: false)
     c(longOpt: 'Option c','Al seleccionar "c" pinta seleccionada -> c ', required: false)
     d(longOpt: 'Option d','Al seleccionar "d" pinta seleccionada -> d ', required: false)
}

def options = cli.parse(args)

if (!options) {
    return
}
if (options.h) { // (2)
    cli.usage()
    return
}
if (options.a) { // (3)
    println "------------------------------------------------------------------"
    println "Seleccionada ha sido la 'a'"
    println "------------------------------------------------------------------"
}
if (options.b) {
    println "------------------------------------------------------------------"
    println "Seleccionada ha sido la 'b'"
    println "------------------------------------------------------------------"
}
if (options.c) {
    println "------------------------------------------------------------------"
    println "Seleccionada ha sido la 'c'"
    println "------------------------------------------------------------------"
}
if (options.d) {
    println "------------------------------------------------------------------"
    println "Seleccionada ha sido la 'd'"
    println "------------------------------------------------------------------"
}