[
https://issues.apache.org/jira/browse/CLI-265?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15472346#comment-15472346
]
Martin Sandiford commented on CLI-265:
--------------------------------------
Hey Benedikt, thanks for looking at this quickly.
I'm not sure if this is a complete fix. It seems to miss the case where short
options are concatenated after an option that takes an optional argument.
A failing test case for this would be to modify {{setUp()}} in
BugCLI265Test.java to include short options "a" and "b":
{code:java}
@Before
public void setUp() throws Exception {
parser = new DefaultParser();
Option TYPE1 =
Option.builder("t1").hasArg().numberOfArgs(1).optionalArg(true).argName("t1_path").build();
Option OPTION_A = Option.builder("a").hasArg(false).build();
Option OPTION_B = Option.builder("b").hasArg(false).build();
Option LAST = Option.builder("last").hasArg(false).build();
options = new
Options().addOption(TYPE1).addOption(OPTION_A).addOption(OPTION_B).addOption(LAST);
}
{code}
Add add a test for the concatenated options following an option with optional
argument case:
{code:java}
@Test
public void shouldParseConcatenatedShortOptions() throws Exception {
String[] concatenatedShortOptions = new String[] { "-t1", "-ab" };
final CommandLine commandLine = parser.parse(options,
concatenatedShortOptions);
assertTrue(commandLine.hasOption("t1"));
assertEquals(null, commandLine.getOptionValue("t1"));
assertTrue(commandLine.hasOption("a"));
assertTrue(commandLine.hasOption("b"));
assertFalse(commandLine.hasOption("last"));
}
{code}
One possible fix is to check that at least the first character of the option is
a short option if all the other cases fail in {{isShortOption(...)}} like so:
{code:java}
private boolean isShortOption(String token)
{
// short options (-S, -SV, -S=V, -SV1=V2, -S1S2)
if (!token.startsWith("-") || token.length() == 1)
{
return false;
}
// remove leading "-" and "=value"
int pos = token.indexOf("=");
String optName = pos == -1 ? token.substring(1) : token.substring(1,
pos);
if (options.hasShortOption(optName))
{
return true;
}
return optName.length() > 0 &&
options.hasShortOption(String.valueOf(optName.charAt(0)));
}
{code}
> Optional argument picking up next regular option as its argument
> ----------------------------------------------------------------
>
> Key: CLI-265
> URL: https://issues.apache.org/jira/browse/CLI-265
> Project: Commons CLI
> Issue Type: Bug
> Components: CLI-1.x
> Affects Versions: 1.3.1
> Reporter: Lynn Henderson
> Assignee: Benedikt Ritter
> Fix For: 1.4
>
>
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)