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

I use CliBuilderto parse the command line arguments for the Groovy script. Among the arguments that I have identified, I have one that is mandatory. Is there a way to support an argument -h,--helpthat will print using the command without annoying error messages regarding missing arguments?

For example, run the following Groovy script with only an argument -h:

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 hits the line def options = cli.parse (args), and will automatically stop the script from executing:

error: Missing required option: r
usage: test
 -h, - help Prints this message
 -r, - required Required argument.

, -h --help, required:true . ?

+4
1

, ,

cli.usage()

?

- , :

def options

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

Source: https://habr.com/ru/post/1526437/


All Articles