Details
-
Bug
-
Status: Open
-
Minor
-
Resolution: Unresolved
-
1.2
-
None
-
None
-
Windows 64-bit
Description
Title might be hard to understand, but basically:
Arguments:
String [] cli =
Options:
Options opts = new Options();
opts.addOption(OptionBuilder.create("cc"));
opts.addOption(OptionBuilder.create("c"));
As you can see, "-ccc" is an extra unrecognized argument.
When you try to parse it:
CommandLineParser parser = new GnuParser();
CommandLine line = parser.parse(opts, cli, true);
Content on line:
args: [cc, -c]
line: [[ option: cc :: null ], [ option: c :: null ]]
I would expect:
args: [-ccc]
line: [[ option: cc :: null ], [ option: c :: null ]]
Wrote a few unit tests (3rd one fails):
UnitTest.java
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.junit.Assert;
import org.junit.Test;
public class CommonsCli {
@Test
public void test1() throws ParseException {
String [] cli = {"-cc"};
Options opts = new Options();
opts.addOption(OptionBuilder.create("cc"));
CommandLineParser parser = new GnuParser();
CommandLine line = parser.parse(opts, cli);
if (line.getArgs().length > 0 ) { Assert.fail("Extra argument found: " + line.getArgList().get(0)); }
}
@Test
public void test2() throws ParseException {
String [] cli = {"-cc", "-ccc"};
Options opts = new Options();
opts.addOption(OptionBuilder.create("cc"));
CommandLineParser parser = new GnuParser();
CommandLine line = parser.parse(opts, cli, true);
if (line.getArgs().length > 0 ) { Assert.assertEquals("-ccc", line.getArgList().get(0)); }
}
@Test
public void test3() throws ParseException {
String [] cli = {"-cc", "-ccc", "-c"}
;
Options opts = new Options();
opts.addOption(OptionBuilder.create("cc"));
opts.addOption(OptionBuilder.create("c"));
CommandLineParser parser = new GnuParser();
CommandLine line = parser.parse(opts, cli, true);
if (line.getArgs().length > 0 )
{ Assert.assertEquals("-ccc", line.getArgList().get(0)); }}
}