Groovy CliBuilder, how to recognize invalid arguments?

I am learning Groovy CliBuilder and I find it wonderful, except that I do not know how to recognize the wrong arguments. Consider the following code example:

def cli = new CliBuilder() cli.s args: 1, longOpt: 'sdkdir', 'sdkdir usage info' cli.h args: 0, longOpt: 'help', 'print usage information' def opt = cli.parse(args) if (!opt) { //how to be in this case? seems I can never reach here println "something went wrong, but I don't know what" } else if (opt.h) { cli.usage() } else (!opt.s) { println "missing required option -s, try with --help for more information" } else { //do something } 

If I call my script with, for example, -p , which is an invalid option, nothing happens. Similarly, if I add arguments after options, they are also not detected.

How can I detect and signal an error?

In addition, a slight inconvenience is that in my example, -s is a required parameter, so theoretically I could add required: true , in practice I cannot or would also be needed with -h , but I think testing it with using if is ok, unless there is a better way.

My real problem is finding unwanted options and arguments. Any help is appreciated, thanks.

UPDATE . Thanks @rodion for your input. I think I agree with the good ones, and not just for simplicity. Here is what I came up with:

 #!/usr/bin/groovy def cli = new CliBuilder(usage: 'cliTest -s sdkdir {projectName}', header: 'Command line parameter parsing test in Groovy') cli.s longOpt: 'sdkdir', args: 1, 'sdkdir usage info, REQUIRED' cli.h longOpt: 'help', 'print usage information' def opt = cli.parse(args) def errMsg = "Invalid arguments.\nusage: ${cli.usage}\n" + "Try `cliTest --help' for more information." if (!opt) { //should never happen, since I don't have required parameters in CliBuilder println "error processing arguments\n" } else if (opt.h) { cli.usage() } else if (!opt.s) { println errMsg } else if (opt.arguments().size() != 1) { println errMsg } else { println "Creating project ${opt.arguments()[0]}, sdkdir ${opt.s.value}" } 

This solution is good enough, but not perfect, because it does not tell you which parameter is wrong, but just tells you a short message or prints usage information. Here are some tests:

 $ ./cliTest Invalid arguments. usage: cliTest -s sdkdir {projectName} Try `cliTest --help' for more information. $ ./cliTest -a Invalid arguments. usage: cliTest -s sdkdir {projectName} Try `cliTest --help' for more information. $ ./cliTest -a -s ../sdkdir Invalid arguments. usage: cliTest -s sdkdir {projectName} Try `cliTest --help' for more information. $ ./cliTest -s ../sdkdir Invalid arguments. usage: cliTest -s sdkdir {projectName} Try `cliTest --help' for more information. $ ./cliTest -s ../sdkdir projectName Creating project projectName, sdkdir ../sdkdir $ ./cliTest -s ../sdkdir projectName wrong Invalid arguments. usage: cliTest -s sdkdir {projectName} Try `cliTest --help' for more information. $ ./cliTest -s ../sdkdir -a projectName Invalid arguments. usage: cliTest -s sdkdir {projectName} Try `cliTest --help' for more information. $ ./cliTest -s error: Missing argument for option: s usage: cliTest -s sdkdir {projectName} Command line parameter parsing test in Groovy -h,--help print usage information -s,--sdkdir <arg> sdkdir usage info, REQUIRED error processing arguments 

For my purposes, I am more than satisfied, but if someone knows a better way, let me know.

In addition, I realized that the !opt case can occur when there is a required: true parameter and the argument is absent, but, in my opinion, it will never be used, because otherwise it would be impossible to get the help option alone.

+4
source share
2 answers

It looks like this problem . Obviously, you can check opt after parsing (but if it is zero, then I think you have left nothing). Oh, and the problem is that it's not A Bug, hmm ... wierd.

+2
source

Set the groovy CliBuilder property to stopAtNonOption property to false. (By default, it is true). I know this seems unintuitive.

 CliBuilder cli = new CliBuilder(usage:'script-name',stopAtNonOption:false) 

Then, when you start the command line application, CliBuilder will exit with an error message as soon as it detects an unrecognized option, for example:

 $ script-name -bad error: Unrecognized option: -bad 
+9
source

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


All Articles