Bob Kuhar
Bob Kuhar

Reputation: 11140

How do I get the non-option arguments from the CliBuilder?

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.

Upvotes: 3

Views: 1361

Answers (1)

tim_yates
tim_yates

Reputation: 171154

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

Upvotes: 3

Related Questions