I am learning Groovy CliBuilder and I find it great, except, I don't know how to recognise wrong arguments. Consider the following example code:
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 instance, -p
, which is an invalid option nothing happens. Similarly, if I add arguments after the options they aren't detected too.
How can I detect and signal the error?
Also, a small inconvenience is that in my example -s
is a required parameter, so in theory, I could add required: true
, in practice I can't or it would be required also with -h
, but I think testing it with an if
is fine, unless there is a better way.
My real problem is about finding unwanted options and arguments. Any help appreciated, thank you.
UPDATE: Thanks @rodion for your input, I guess I will settle with good enough instead of perfect for the sake of 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 doesn't tell you which parameter is wrong, but just tells you with a concise 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'm more than satisfied, but if anyone knows a better way, let me know.
Also, I figured out that the !opt
case can happen when there is a required: true
option and the argument is missing, but from my understanding it can never be used, since otherwise is not possible to have an help option alone.
Set the groovy CliBuilder property stopAtNonOption to false. (It defaults to true). I know this seems unintuitive.
CliBuilder cli = new CliBuilder(usage:'script-name',stopAtNonOption:false)
Then when you run your command line application, the CliBuilder will exit with an error message as soon as it encounters an unrecognized option, e.g.:
$ script-name -bad
error: Unrecognized option: -bad
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With