LightOptionParser: multiple options with arguments.

Previously if there was an option which took multiple arguments, the parser
always returned a flat array of all values given for that option, regardless of
how many arguments it expected. E.g. the following:

    cmd --file-pair f1 f2 --file-pair a b

resulted in the following:

    file-pair: [ f1, f2, a, b ]

For the case where the option is defined as taking only one argument, this
behavior is unchanged, but for the cases where the option is defined as taking
more than one option, an array of arrays is returned instead. For the above
example the result is now:

    file-pair: [ [f1, f2], [a, b] ]

The behavior is the same for options with variable arguments. The following:

    cmd --set f1 f2 --set a b c --set last

results in:

    file-pair: [ [f1, f2], [a, b, c], [last] ]
This commit is contained in:
Jonathan Bernard 2015-12-11 23:08:53 -06:00
parent 5ce1cfab88
commit 7fb35060c2
2 changed files with 7 additions and 6 deletions

View File

@ -2,7 +2,7 @@ apply plugin: "groovy"
apply plugin: "maven"
group = "com.jdbernard"
version = "4.1"
version = "4.2"
repositories {
mavenLocal()

View File

@ -170,12 +170,13 @@ public class LightOptionParser {
else {
if (!returnOpts.containsKey(optName))
returnOpts[optName] = []
returnOpts[optName] += retVal
if (optDef.longName) {
if (!returnOpts.containsKey(optDef.longName))
returnOpts[optDef.longName] = []
returnOpts[optDef.longName] += retVal } } }
if (optDef.arguments == 1) returnOpts[optName] += retVal
else returnOpts[optName] << retVal
if (optDef.longName)
returnOpts[optDef.longName] = returnOpts[optName] } }
/// This was not as option, it is an unclaomed argument.
else { returnOpts.args << args[i]; i++ } }