groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Remko Popma <remko.po...@gmail.com>
Subject Re: new GroovyCliBuilder
Date Fri, 24 May 2019 17:39:54 GMT
The new CliBuilder annotations API works well for both the Commons CLI and
the picocli implementation.
(And I agree that the annotations API has some advantages over the previous
DSL style, but that may be a matter of taste.)

Note that you can swap the implementation from Commons CLI to picocli by
changing the import
from

import groovy.cli.commons.CliBuilder

to

import groovy.cli.picocli.CliBuilder

With the latter the usage help will show ANSI colors and gives some other
features like the `cliBuilder.usageMessage
<http://docs.groovy-lang.org/latest/html/gapi/groovy/cli/picocli/CliBuilder.html>`
property to customize the usage help message.

Glad you like it! :-)


On Sat, May 25, 2019 at 2:11 AM MG <mgbiz@arscreat.com> wrote:

> 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