groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From MG <mg...@arscreat.com>
Subject new GroovyCliBuilder
Date Fri, 24 May 2019 17:11:49 GMT
Hi guys,

just wanted to say that I recently had to revisit some older Groovy script code in my project,
and I took the opportunity to switch the old CliBuilder implementation to the new Picocli
based one (http://docs.groovy-lang.org/latest/html/gapi/groovy/cli/commons/CliBuilder.html
) - and I can just say that it is an absolute joy to work with: Concise, elegant, and type
safe G-)


import groovy.cli.commons.CliBuilder
import groovy.cli.Option
import groovy.cli.Unparsed

// Alternative is to use an interface and let Groovy instantiate a class instance from it
class MyOptions {
  boolean isTestRun
  @Option(shortName='h', description='output help info') Boolean help
  @Option(shortName='r', description='process regular entries') Boolean regular
  // etc (for supported types outside of Boolean see URL above)
  @Unparsed List remaining // all additional options given

  MyScriptOptions(boolean isTestRun) { this.isTestRun = isTestRun }
}
  

final cli = new CliBuilder(usage:'my_powerful_groovy_script')
def opts = new MyOptions(true)
cli.parseFromInstance(opts, args.split())  // From now on all cmd line 
params can be accessed in a type safe manner

if(opts.help) {
   println cli.usage()
   System.exit(0)
}

if(opts.regular) {
   ...
}

Cheers,
mg






Mime
View raw message