3

I have the simplest ever groovy script trying to figure out the CliBuilder. How do I make the CliBuilder give me the command line arguments beyond the options? My expectation is that a command line invocation like...

./hello.groovy -u robert Edward

...would produce output like...

ROBERT EDWARD

from my source like...

#!/usr/bin/env groovy
cli = new CliBuilder(usage:'hello.groovy [-hu] [name ...]')
cli.with {
  h longOpt: 'help', 'Show usage information'
  u longOpt: 'upper', 'uppercase eachg name'
}
options = cli.parse(args)
if(!options) {
  throw new IllegalStateException("WTF?!?")
}
if(options.h || options.arguments().isEmpty()) {
  cli.usage()
  return
}
println("$options.arguments()");

..but I can't figure out how to get the rest of the arguments, the ones beyond the option.

Bob Kuhar
  • 10,838
  • 11
  • 62
  • 115
  • So what does `println "${options.arguments()}"` show? – tim_yates Feb 12 '15 at 20:59
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  Oct 05 '17 at 14:21

1 Answers1

3

You don't need the -2 if -u is just a flag:

#!/usr/bin/env groovy

cli = new CliBuilder(usage:'hello.groovy [-hu] [name ...]')
cli.with {
  h longOpt: 'help', 'Show usage information'
  u longOpt: 'upper', 'uppercase eachg name'
}
options = cli.parse(args)
if(!options) {
  throw new IllegalStateException("WTF?!?")
}
if(options.h || options.arguments().isEmpty()) {
  cli.usage()
  return
}

if(options.u) {
    options.arguments().each { println it.toUpperCase() }
}
else {
    options.arguments().each { println it }
}
tim_yates
  • 167,322
  • 27
  • 342
  • 338