[ 
https://issues.apache.org/jira/browse/GROOVY-6536?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16311941#comment-16311941
 ] 

Raviteja Lokineni edited comment on GROOVY-6536 at 1/4/18 7:56 PM:
-------------------------------------------------------------------

I did a runtime override instead, see below. BTW, I feel this should be 
available.

{code:java}
CliBuilder cliBuilder = new CliBuilder()
cliBuilder.usage = 'cli'
// Runtime override to use auto
cliBuilder.metaClass.usage = {->
    cliBuilder.formatter.printHelp(
        cliBuilder.writer,
        cliBuilder.width,
        cliBuilder.usage,
        cliBuilder.header,
        cliBuilder.options,
        cliBuilder.formatter.defaultLeftPad,
        cliBuilder.formatter.defaultDescPad,
        cliBuilder.footer,
        true
    )
    cliBuilder.writer.flush()
}{code}

Just for future reference to preserve the insertion order of options, I did 
something like below.

{code:java}
// To preserve insertion order
cliBuilder.formatter.optionComparator = null
{code}


was (Author: bond_):
I did a runtime override instead, see below. BTW, I feel this should be 
available.

{code:java}
        CliBuilder cliBuilder = new CliBuilder()
        cliBuilder.usage = 'cli'
        // Runtime override to use auto
        cliBuilder.metaClass.usage = {->
            cliBuilder.formatter.printHelp(cliBuilder.writer, cliBuilder.width, 
cliBuilder.usage, cliBuilder.header, cliBuilder.options, 
cliBuilder.formatter.defaultLeftPad, cliBuilder.formatter.defaultDescPad, 
cliBuilder.footer, true)
            cliBuilder.writer.flush()
        }
{code}

Just for future reference to preserve the insertion order of options, I did 
something like below.

{code:java}
        // To preserve insertion order
        cliBuilder.formatter.optionComparator = null
{code}

> Support autoUsage in CliBuilder
> -------------------------------
>
>                 Key: GROOVY-6536
>                 URL: https://issues.apache.org/jira/browse/GROOVY-6536
>             Project: Groovy
>          Issue Type: Improvement
>          Components: command line processing
>    Affects Versions: 2.2.1
>            Reporter: Björn Kautler
>              Labels: CliBuilder
>
> I'd like to have an "autoUsage" property on the CliBuilder object that is 
> "true" by default and is passed on to the HelpFormatter in the 
> CliBuilder.usage() method.
> Currently to use autoUsage you have to do something like
> {code}
> def cliBuilder = new CliBuilder()
> cliBuilder.h longOpt: 'help', 'show help'
> cliBuilder.u longOpt: 'until', 'u option'
> def arguments
> class HelpFormatterCategory {
>    static void printHelp(HelpFormatter self, PrintWriter pw, int width, 
> String cmdLineSyntax, String header, Options options, int leftPad, int 
> descPad, String footer) {
>       self.printHelp(pw, width, cmdLineSyntax, header, options, leftPad, 
> descPad, footer, true);
>    }
> }
> use(HelpFormatterCategory) {
>    arguments = cliBuilder.parse args
>    if (!arguments) {
>       System.exit 1
>    }
>    if (arguments.arguments()) {
>       errPrintln 'error: Invalid arguments specified'
>       cliBuilder.usage()
>       System.exit 1
>    }
>    if (arguments.help) {
>       cliBuilder.usage()
>       System.exit 0
>    }
> }
> if (arguments.until) {
>    doStuff();
> }
> {code}
> if would be nicer to instead just do
> {code}
> def cliBuilder = new CliBuilder(autoUsage: true)
> cliBuilder.h longOpt: 'help', 'show help'
> cliBuilder.u longOpt: 'until', 'u option'
> def arguments = cliBuilder.parse args
> if (!arguments) {
>     System.exit 1
> }
> if (arguments.arguments()) {
>     errPrintln 'error: Invalid arguments specified'
>     cliBuilder.usage()
>     System.exit 1
> }
> if (arguments.help) {
>     cliBuilder.usage()
>     System.exit 0
> }
> if (arguments.until) {
>    doStuff();
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to