How to support help command-line argument with required arguments in CliBuilder

command-line-arguments, groovy

Solution

I may not have understood the question properly, but is

cli.usage()

what you are looking for?

You can do something like below to avoid it:

def options

//or ['-h', '--help'].intersect(args?.toList())
if('-h' in args || '--help' in args) {
    cli.usage() 
} else {
    options = cli.parse (args)
}

Problem

I am using the `CliBuilder` to parse command-line arguments for a Groovy script. Among the arguments I have defined, I have one which is mandatory. Is there a way to support a `-h,--help` argument which would print the command usage without the annoying error message about missing arguments? For example, running the following Groovy script with only the `-h` argument: ``` def cli = new CliBuilder (usage:'test', stopAtNonOption:false) cli.r (longOpt:'required', required:true, 'Required argument.') cli.h (longOpt:'help', 'Prints this message') def options = cli.parse (args) ``` will generate the output below when it gets to the `def options = cli.parse (args)` line, and will automatically stop the script execution: ``` error: Missing required option: r usage: test -h,--help Prints this message -r,--required Required argument. ``` I would like to display only the usage when the `-h` or `--help` argument is specified, without having to drop the `required:true` option for my required arguments. Is this possible?

Original source