[
https://issues.apache.org/jira/browse/CLI-306?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17824004#comment-17824004
]
Claude Warren commented on CLI-306:
-----------------------------------
The issue arises because:
* --one allows multiple args/values
* we support both " --one a,b" and "–one a b" as valid arguments to have both
"a" and "b" as values for "one"
* we support negative numbers as proper values. So "–one -2" would be give
"one" the value of "-2".
* We don't have any way to signal the end of the option. We do have an option
"–" that turns off all argument processing after it.
I can see 3 solutions to this particular issue:
# create a '.enforceValueSeparator(',') that will require all arguments to
have the ','. This way the "-2" in the example will be seen as an option and
not a value.
# create an "end of values" token. Perhaps "-" by itself. So that the valid
input would then be "–one a,b - -2 c" this would force the parsing to stop
after "a,b".
# disallow all numeric options., Thus "-2" would be invalid, as would "-45",
and "-2.1". But options like "-3bf" would still be valid.
[~ggregory] I think #3 is the correct approach. Do you have an opinion.
> Issue parsing numeric options following an option which accepts multiple args
> in DefaultParser
> ----------------------------------------------------------------------------------------------
>
> Key: CLI-306
> URL: https://issues.apache.org/jira/browse/CLI-306
> Project: Commons CLI
> Issue Type: Bug
> Components: CLI-1.x
> Affects Versions: 1.4
> Reporter: Todd Ye
> Priority: Major
>
> commons-cli seems to be unable to detect numeric options in their short "opt"
> form following an option which takes multiple arguments.
> Will consistently throw:
> {code:java}
> Exception in thread "main" org.apache.commons.cli.MissingOptionException:
> Missing required option: 2
> {code}
> How to reproduce:
> {code:java}
> Option multipleOptional = Option.builder("1")
> .longOpt("one")
> .argName("value1,value2,...,valueN")
> .hasArgs()
> .valueSeparator(',')
> .build();
> Option singleMandatory = Option.builder("2")
> .argName("value")
> .longOpt("two")
> .hasArg()
> .required()
> .build();
> Options options = new Options();
> options.addOption(singleMandatory);
> options.addOption(multipleOptional);
> CommandLineParser parser = new DefaultParser();
> CommandLine line = parser.parse(options, args);
> for (Option o : line.getOptions()) {
> System.out.println(o.getOpt() + '\t'
> + Arrays.toString(o.getValues()));
> }
> {code}
> now pass in:
> {code:java}
> --one argNumOne,argNumTwo -2 argNumThree
> {code}
> Note that an error will not occur if "opt" is set to a char like "a/b/c" or
> if the previous option is set with hasArg() instead of hasArgs()
> Also error will not occur if the longOpt is used such as:
> {code:java}
> --one argNumOne,argNumTwo --two argNumThree
> {code}
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)