You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Paul King <pa...@asert.com.au> on 2016/04/09 14:04:08 UTC

CliBuilder enhancements

Hi everyone,

More than a year ago, Jim White and myself worked on some possible
enhancements to CliBuilder. At the time Jim was mostly trying to add
support for JCommander to allow annotation style CLI usage patterns
and I was trying to work out a nice way to allow us to swap in
different CLI packages underneath CliBuilder. To cut a long story
short, we never finished those spikes, so I grabbed a subset of the
ideas and did a minimal change to allow annotation support with
CliBuilder.
The results can be found here:

https://github.com/apache/groovy/pull/308

The tests and doco give all the details but as a sneak peek, if you
define this option specification:

interface GreeterI {
  @Option(shortName='h', description='display usage') Boolean help()
  @Option(shortName='a', description='greeting audience') String audience()
  @Unparsed List remaining()
}

You can then use CliBuilder as follows:

def cli = new CliBuilder(usage: 'groovy Greeter [option]')
def argz = '--audience Groovologist'.split()
def options = cli.parseFromSpec(GreeterI, argz)
assert options.audience() == 'Groovologist'

There are numerous ways to define the option specification and
numerous other features which are covered in the doco and tests but
hopefully this will give the idea.

Any feedback/comments welcome.

Cheers, Paul.

Re: CliBuilder enhancements

Posted by Shil Sinha <sh...@gmail.com>.
Did a quick review of the tests and documentation, looks great! This is a
feature I'll definitely be using upon its availability.

On Sat, Apr 9, 2016 at 8:04 AM, Paul King <pa...@asert.com.au> wrote:

> Hi everyone,
>
> More than a year ago, Jim White and myself worked on some possible
> enhancements to CliBuilder. At the time Jim was mostly trying to add
> support for JCommander to allow annotation style CLI usage patterns
> and I was trying to work out a nice way to allow us to swap in
> different CLI packages underneath CliBuilder. To cut a long story
> short, we never finished those spikes, so I grabbed a subset of the
> ideas and did a minimal change to allow annotation support with
> CliBuilder.
> The results can be found here:
>
> https://github.com/apache/groovy/pull/308
>
> The tests and doco give all the details but as a sneak peek, if you
> define this option specification:
>
> interface GreeterI {
>   @Option(shortName='h', description='display usage') Boolean help()
>   @Option(shortName='a', description='greeting audience') String audience()
>   @Unparsed List remaining()
> }
>
> You can then use CliBuilder as follows:
>
> def cli = new CliBuilder(usage: 'groovy Greeter [option]')
> def argz = '--audience Groovologist'.split()
> def options = cli.parseFromSpec(GreeterI, argz)
> assert options.audience() == 'Groovologist'
>
> There are numerous ways to define the option specification and
> numerous other features which are covered in the doco and tests but
> hopefully this will give the idea.
>
> Any feedback/comments welcome.
>
> Cheers, Paul.
>