Emmanuel Bourg a écrit :
I'm going to convert the Avalon CLI test case to the Commons CLI API to
see what features are missing. I'll see if these features can be
implemented in CLI. So maybe we'll have a "common" API at last.
I completed the conversion of the Avalon CLI tests to the Commons CLI
API to compare the behavior of the 2 parsers. There were 36 test
methods, 3 couldn't be converted due to a missing feature in Commons
CLI, 4 methods fail due to a different behavior, and the rest just works.
Here are the differences I noticed:
1. When 2 exclusive options of a group are in the same command line,
Commons CLI throws an exception but Avalon CLI doesn't, it exposes an
error message and the result of the parsing is available. With Commons
CLI the exception prevents from accessing the result of the parsing.
2. Avalon CLI requires that optional arguments have an '=' separator.
"--foo=bar" is ok but "--foo bar" isn't. I haven't checked but I guess
it's the same with the short options (ie "-O1" vs "-O 1").
3. The PosixParser doesn't stop the parsing on the "--" token if the
option preceding the token accepts an arguments, the token is used as an
argument. The Avalon CLI parser and the GnuParser aren't affected by
this issue.
4. There is no equivalent for the ParserControl interface in Commons
CLI. It's used to interrupt the parsing on a specific option, or after a
sequence of events.
5. Missing arguments are initialized to an empty string in Avalon CLI.
For example with these options:
OptionBuilder.hasArgs(2).withValueSeparator().create('D');
OptionBuilder.create('c')
and this command line:
-Dfoo -c
Avalon CLI assigns 2 values to the -D option, "foo" and "", but Commons
CLI returns only one value.
My quick analysis of these differences:
1. Avalon CLI warns and Commons CLI fails, I don't know if this is
useful but we may probably add a setting in OptionGroup to ignore the
error and continue the parsing.
2. I guess this restriction solves some ambiguous cases but it's also
confusing for the end user, if he omits the '=' separator he may not
understand why the value is not assigned to the option. I'm not sure
it's worth implementing in Commons CLI, maybe as a parser setting or as
an additional parser.
3. PosixParser should be fixed
4. I ignore the use cases for this, I'll check the JMeter code to see if
it's actually used. If this is proved useful this may be implemented in
Commons CLI, but not before CLI 1.3.
5. Minor difference, can be easily worked around by an application
migrating from Avalon CLI to Commons CLI
Emmanuel Bourg
package org.apache.commons.cli;
import java.util.Iterator;
import junit.framework.TestCase;
/**
* This test case replicates the tests from the Avalon CLI API transposed
* to the Commons CLI API.
*
* @author Emmanuel Bourg
* @version $Revision$, $Date$
*/
public class AvalonParserTest extends TestCase
{
private static final String[] ARGLIST1 = { "--you", "are", "--all",
"-cler", "kid" };
private static final String[] ARGLIST2 = { "-Dstupid=idiot", "are",
"--all", "here", "-d" };
//duplicates
private static final String[] ARGLIST3 = { "-Dstupid=idiot", "are",
"--all", "--all", "here" };
//incompatable (blee/all)
private static final String[] ARGLIST4 = { "-Dstupid","idiot", "are",
"--all", "--blee", "here" };
private static final String[] ARGLIST5 = { "-f", "myfile.txt" };
private static final char DEFINE_OPT = 'D';
private static final char CASE_CHECK_OPT = 'd';
private static final char YOU_OPT = 'y';
private static final char ALL_OPT = 'a';
private static final char CLEAR1_OPT = 'c';
private static final char CLEAR2_OPT = 'l';
private static final char CLEAR3_OPT = 'e';
private static final char CLEAR5_OPT = 'r';
private static final char BLEE_OPT = 'b';
private static final char FILE_OPT = 'f';
private static final char TAINT_OPT = 'T';
private static final Option DEFINE =
OptionBuilder.withLongOpt("define").hasArgs(2).withValueSeparator().create(DEFINE_OPT);
private static final Option DEFINE_MANY =
OptionBuilder.withLongOpt("define").hasArgs(2).withValueSeparator().create(DEFINE_OPT);
// no equivalent for CLOptionDescriptor.DUPLICATES_ALLOWED
private static final Option CASE_CHECK =
OptionBuilder.withLongOpt("charCheck").withDescription("check character case
sensitivity").create(CASE_CHECK_OPT);
private static final Option YOU =
OptionBuilder.withLongOpt("you").create(YOU_OPT);
private static final Option CLEAR1 =
OptionBuilder.withLongOpt("c").create(CLEAR1_OPT);
private static final Option CLEAR2 =
OptionBuilder.withLongOpt("l").create(CLEAR2_OPT);
private static final Option CLEAR3 =
OptionBuilder.withLongOpt("e").create(CLEAR3_OPT);
private static final Option CLEAR5 =
OptionBuilder.withLongOpt("r").create(CLEAR5_OPT);
private static final Option BLEE =
OptionBuilder.withLongOpt("blee").create(BLEE_OPT);
private static final Option ALL =
OptionBuilder.withLongOpt("all").create(ALL_OPT); // todo use a group of
mutually exclusive option : all | blee
private static final OptionGroup GROUP = new
OptionGroup().addOption(ALL).addOption(BLEE);
private static final Option FILE =
OptionBuilder.hasArg().withLongOpt("file").withDescription("the build
file.").create(FILE_OPT);
private static final Option TAINT =
OptionBuilder.hasOptionalArg().withLongOpt("taint").withDescription("turn on
tainting checks (optional level).").create(TAINT_OPT);
public void testOptionalArgWithSpace() throws Exception
{
Options options = new Options();
options.addOption(ALL);
options.addOption(TAINT);
String[] args = new String[]{"-T", "param", "-a"};
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, args);
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( "Option count", 3, size );
//CLOption option0 = (CLOption)clOptions.get( 0 );
//assertEquals( "Option Code: " + option0.getDescriptor().getId(),
TAINT_OPT, option0.getDescriptor().getId() );
//assertEquals( "Option Arg: " + option0.getArgument( 0 ), null,
option0.getArgument( 0 ) );
assertTrue(cl.hasOption(TAINT_OPT));
assertEquals("param", cl.getOptionValue(TAINT_OPT));
//CLOption option1 = (CLOption)clOptions.get( 1 );
//assertEquals( option1.getDescriptor().getId(), CLOption.TEXT_ARGUMENT
);
//assertEquals( option1.getArgument( 0 ), "param" );
//CLOption option2 = (CLOption)clOptions.get( 2 );
//assertEquals( option2.getDescriptor().getId(), ALL_OPT );
//assertEquals( option2.getArgument( 0 ), null );
assertTrue(cl.hasOption(ALL_OPT));
assertEquals(null, cl.getOptionValue(ALL_OPT));
}
public void testOptionalArgLong() throws Exception
{
Options options = new Options();
options.addOption(ALL);
options.addOption(TAINT);
// Check that optional args work woth long options
String[] args = new String[]{"--taint", "param", "-a"};
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, args);
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( "Option count", 3, size );
//CLOption option0 = (CLOption)clOptions.get( 0 );
//assertEquals( "Option Code: " + option0.getDescriptor().getId(),
TAINT_OPT, option0.getDescriptor().getId() );
//assertEquals( "Option Arg: " + option0.getArgument( 0 ), null,
option0.getArgument( 0 ) );
assertTrue(cl.hasOption(TAINT_OPT));
assertEquals("param", cl.getOptionValue(TAINT_OPT));
//CLOption option1 = (CLOption)clOptions.get( 1 );
//assertEquals( CLOption.TEXT_ARGUMENT ,
option1.getDescriptor().getId());
//assertEquals( "param" , option1.getArgument( 0 ) );
//CLOption option2 = (CLOption)clOptions.get( 2 );
//assertEquals( option2.getDescriptor().getId(), ALL_OPT );
//assertEquals( option2.getArgument( 0 ), null );
assertTrue(cl.hasOption(ALL_OPT));
assertEquals(null, cl.getOptionValue(ALL_OPT));
}
public void testOptionalArgLongEquals() throws Exception
{
Options options = new Options();
options.addOption(ALL);
options.addOption(TAINT);
// Check that optional args work woth long options
String[] args = new String[] { "--taint=param", "-a" };
Parser parser = new PosixParser();
CommandLine cl = parser.parse(options, args);
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals("Option count", 2, size);
//CLOption option0 = (CLOption) clOptions.get(0);
//assertEquals("Option Code: " + option0.getDescriptor().getId(),
TAINT_OPT, option0.getDescriptor().getId());
//assertEquals("Option Arg: " + option0.getArgument(0), "param",
option0.getArgument(0));
assertTrue(cl.hasOption(TAINT_OPT));
assertEquals("param", cl.getOptionValue(TAINT_OPT));
//CLOption option2 = (CLOption) clOptions.get(1);
//assertEquals(option2.getDescriptor().getId(), ALL_OPT);
//assertEquals(option2.getArgument(0), null);
assertTrue(cl.hasOption(ALL_OPT));
assertEquals(null, cl.getOptionValue(ALL_OPT));
}
public void testShortOptArgUnenteredBeforeOtherOpt() throws Exception
{
Options options = new Options();
options.addOption(ALL);
options.addOption(TAINT);
String[] args = new String[]{"-T", "-a"};
Parser parser = new PosixParser();
CommandLine cl = parser.parse(options, args);
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( "Option count", 2, size );
//CLOption option0 = (CLOption)clOptions.get( 0 );
//assertEquals( "Option Code: " + option0.getDescriptor().getId(),
TAINT_OPT, option0.getDescriptor().getId() );
//assertEquals( "Option Arg: " + option0.getArgument( 0 ), null,
option0.getArgument( 0 ) );
assertTrue(cl.hasOption(TAINT_OPT));
assertEquals(null, cl.getOptionValue(TAINT_OPT));
//CLOption option1 = (CLOption)clOptions.get( 1 );
//assertEquals( option1.getDescriptor().getId(), ALL_OPT );
//assertEquals( option1.getArgument( 0 ), null );
assertTrue(cl.hasOption(ALL_OPT));
assertEquals(null, cl.getOptionValue(ALL_OPT));
}
public void testOptionalArgsWithArgShortBeforeOtherOpt() throws Exception
{
//"-T3","-a"
Options options = new Options();
options.addOption(ALL);
options.addOption(TAINT);
String[] args = new String[]{"-T3", "-a"};
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, args);
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( size, 2 );
//CLOption option0 = (CLOption)clOptions.get( 0 );
//assertEquals( option0.getDescriptor().getId(), TAINT_OPT );
//assertEquals( option0.getArgument( 0 ), "3" );
assertTrue(cl.hasOption(TAINT_OPT));
assertEquals("3", cl.getOptionValue(TAINT_OPT));
//CLOption option1 = (CLOption)clOptions.get( 1 );
//assertEquals( ALL_OPT, option1.getDescriptor().getId() );
//assertEquals( null, option1.getArgument( 0 ) );
assertTrue(cl.hasOption(ALL_OPT));
assertEquals(null, cl.getOptionValue(ALL_OPT));
}
public void testOptionalArgsNoArgShortBeforeOtherOpt() throws Exception
{
//"-T","-a"
Options options = new Options();
options.addOption(ALL);
options.addOption(TAINT);
String[] args = new String[]{"-T", "-a"};
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, args);
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( size, 2 );
//CLOption option0 = (CLOption)clOptions.get( 0 );
//assertEquals( TAINT_OPT, option0.getDescriptor().getId() );
//assertEquals( null, option0.getArgument( 0 ) );
assertTrue(cl.hasOption(TAINT_OPT));
assertEquals(null, cl.getOptionValue(TAINT_OPT));
//CLOption option1 = (CLOption)clOptions.get( 1 );
//assertEquals( ALL_OPT, option1.getDescriptor().getId() );
//assertEquals( null, option1.getArgument( 0 ) );
assertTrue(cl.hasOption(ALL_OPT));
assertEquals(null, cl.getOptionValue(ALL_OPT));
}
public void testFullParse() throws Exception
{
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// YOU, ALL, CLEAR1, CLEAR2, CLEAR3, CLEAR5
//};
Options options = new Options();
options.addOption(YOU);
options.addOption(ALL);
options.addOption(CLEAR1);
options.addOption(CLEAR2);
options.addOption(CLEAR3);
options.addOption(CLEAR5);
Parser parser = new PosixParser();
CommandLine cl = parser.parse(options, ARGLIST1);
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( size, 8 );
//assertEquals( ((CLOption)clOptions.get( 0 )).getDescriptor().getId(),
YOU_OPT );
assertTrue(cl.hasOption(YOU_OPT));
//assertEquals( ((CLOption)clOptions.get( 1 )).getDescriptor().getId(),
0 );
assertTrue(cl.getArgList().contains("are"));
//assertEquals( ((CLOption)clOptions.get( 2 )).getDescriptor().getId(),
ALL_OPT );
assertTrue(cl.hasOption(ALL_OPT));
//assertEquals( ((CLOption)clOptions.get( 3 )).getDescriptor().getId(),
CLEAR1_OPT );
assertTrue(cl.hasOption(CLEAR1_OPT));
//assertEquals( ((CLOption)clOptions.get( 4 )).getDescriptor().getId(),
CLEAR2_OPT );
assertTrue(cl.hasOption(CLEAR2_OPT));
//assertEquals( ((CLOption)clOptions.get( 5 )).getDescriptor().getId(),
CLEAR3_OPT );
assertTrue(cl.hasOption(CLEAR3_OPT));
//assertEquals( ((CLOption)clOptions.get( 6 )).getDescriptor().getId(),
CLEAR5_OPT );
assertTrue(cl.hasOption(CLEAR5_OPT));
//assertEquals( ((CLOption)clOptions.get( 7 )).getDescriptor().getId(),
0 );
assertTrue(cl.getArgList().contains("kid"));
}
public void testDuplicateOptions() throws Exception
{
//"-Dstupid=idiot","are","--all","--all","here"
Options options = new Options();
options.addOption(DEFINE);
options.addOption(ALL);
options.addOption(CLEAR1);
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, ARGLIST3);
//CLArgsParser parser = new CLArgsParser( ARGLIST3, options );
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( size, 5 );
Option[] opts = cl.getOptions();
assertEquals(3, opts.length);
//assertEquals( ((CLOption)clOptions.get( 0 )).getDescriptor().getId(),
DEFINE_OPT );
assertEquals(DEFINE_OPT, opts[0].getId());
assertEquals("stupid", opts[0].getValue(0));
assertEquals("idiot", opts[0].getValue(1));
//assertEquals( ((CLOption)clOptions.get( 1 )).getDescriptor().getId(),
0 );
assertTrue(cl.getArgList().contains("are"));
//assertEquals( ((CLOption)clOptions.get( 2 )).getDescriptor().getId(),
ALL_OPT );
assertEquals(ALL_OPT, opts[1].getId());
//assertEquals( ((CLOption)clOptions.get( 3 )).getDescriptor().getId(),
ALL_OPT );
assertEquals(ALL_OPT, opts[2].getId());
//assertEquals( ((CLOption)clOptions.get( 4 )).getDescriptor().getId(),
0 );
assertTrue(cl.getArgList().contains("here"));
}
public void testIncompatableOptions() throws Exception
{
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// DEFINE, ALL, CLEAR1, BLEE
//};
Options options = new Options();
options.addOption(DEFINE);
options.addOptionGroup(GROUP);
options.addOption(CLEAR1);
//CLArgsParser parser = new CLArgsParser( ARGLIST4, options );
try
{
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, ARGLIST4);
fail("AlreadySelectedException not thrown");
}
catch (AlreadySelectedException e)
{
// expected
}
// TODO different behavior from Avalon CLI, the result isn't available
//assertNotNull( parser.getErrorString() );
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( size, 5 );
//assertEquals( ((CLOption)clOptions.get( 0 )).getDescriptor().getId(),
DEFINE_OPT );
//assertEquals( ((CLOption)clOptions.get( 1 )).getDescriptor().getId(),
0 );
//assertEquals( ((CLOption)clOptions.get( 2 )).getDescriptor().getId(),
ALL_OPT );
//assertEquals( ((CLOption)clOptions.get( 3 )).getDescriptor().getId(),
BLEE_OPT );
//assertEquals( ((CLOption)clOptions.get( 4 )).getDescriptor().getId(),
0 );
}
public void testSingleArg() throws Exception
{
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// FILE
//};
Options options = new Options();
options.addOption(FILE);
//CLArgsParser parser = new CLArgsParser( ARGLIST5, options );
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, ARGLIST5);
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( size, 1 );
//assertEquals( ((CLOption)clOptions.get( 0 )).getDescriptor().getId(),
FILE_OPT );
assertTrue(cl.hasOption(FILE_OPT));
//assertEquals( ((CLOption)clOptions.get( 0 )).getArgument(),
"myfile.txt" );
assertEquals("myfile.txt", cl.getOptionValue(FILE_OPT));
}
public void testSingleArg2() throws Exception
{
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// FILE
//};
Options options = new Options();
options.addOption(FILE);
// Check delimiters are allowed
//CLArgsParser parser = new CLArgsParser(
// new String[]{"-f-=,=-"}
// , options );
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, new String[] {"-f-=,=-"});
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals(1, size);
//assertEquals(FILE_OPT, ((CLOption)
clOptions.get(0)).getDescriptor().getId());
assertTrue(cl.hasOption(FILE_OPT));
//assertEquals("-=,=-", ((CLOption) clOptions.get(0)).getArgument());
assertEquals("-=,=-", cl.getOptionValue(FILE_OPT));
}
public void testSingleArg3() throws Exception
{
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// FILE
//};
Options options = new Options();
options.addOption(FILE);
// Check delimiters are allowed
//CLArgsParser parser = new CLArgsParser(
// new String[]{"--file=-=,-"}
// , options );
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, new String[]{"--file=-=,-"});
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals(1, size);
//assertEquals(FILE_OPT, ((CLOption)
clOptions.get(0)).getDescriptor().getId());
assertTrue(cl.hasOption(FILE_OPT));
//assertEquals("-=,-", ((CLOption) clOptions.get(0)).getArgument());
assertEquals("-=,-", cl.getOptionValue(FILE_OPT));
}
public void testSingleArg4() throws Exception
{
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// FILE
//};
Options options = new Options();
options.addOption(FILE);
//CLArgsParser parser = new CLArgsParser(
// new String[]{"--file","myfile.txt"}
// , options );
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, new
String[]{"--file","myfile.txt"});
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( 1, size );
//assertEquals( FILE_OPT, ((CLOption)clOptions.get( 0
)).getDescriptor().getId() );
assertTrue(cl.hasOption(FILE_OPT));
//assertEquals( "myfile.txt", ((CLOption)clOptions.get( 0
)).getArgument() );
assertEquals("myfile.txt", cl.getOptionValue(FILE_OPT));
}
public void testSingleArg5() throws Exception
{
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// FILE
//};
Options options = new Options();
options.addOption(FILE);
//CLArgsParser parser = new CLArgsParser(
// new String[]{"-f","myfile.txt"}
// , options );
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, new String[]{"-f","myfile.txt"});
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( 1, size );
//assertEquals( FILE_OPT, ((CLOption)clOptions.get( 0
)).getDescriptor().getId() );
assertTrue(cl.hasOption(FILE_OPT));
//assertEquals( "myfile.txt", ((CLOption)clOptions.get( 0
)).getArgument() );
assertEquals("myfile.txt", cl.getOptionValue(FILE_OPT));
}
public void testSingleArg6() throws Exception
{
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// FILE
//};
Options options = new Options();
options.addOption(FILE);
//CLArgsParser parser = new CLArgsParser(
// new String[]{"-f","-=-"}
// , options );
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, new String[]{"-f","-=-"});
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( 1, size );
//assertEquals( FILE_OPT, ((CLOption)clOptions.get( 0
)).getDescriptor().getId() );
assertTrue(cl.hasOption(FILE_OPT));
//assertEquals( "-=-", ((CLOption)clOptions.get( 0 )).getArgument() );
assertEquals("-=-", cl.getOptionValue(FILE_OPT));
}
public void testSingleArg7() throws Exception
{
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// FILE
//};
Options options = new Options();
options.addOption(FILE);
//CLArgsParser parser = new CLArgsParser(
// new String[]{"--file=-=-"}
// , options );
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, new String[]{"--file=-=-"});
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( 1, size );
//assertEquals( FILE_OPT, ((CLOption)clOptions.get( 0
)).getDescriptor().getId() );
assertTrue(cl.hasOption(FILE_OPT));
//assertEquals( "-=-", ((CLOption)clOptions.get( 0 )).getArgument() );
assertEquals("-=-", cl.getOptionValue(FILE_OPT));
}
public void testSingleArg8() throws Exception
{
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// FILE
//};
Options options = new Options();
options.addOption(FILE);
//CLArgsParser parser = new CLArgsParser(
// new String[]{"--file","-=-"}
// , options );
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, new String[]{"--file","-=-"});
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( 1, size );
//assertEquals( FILE_OPT, ((CLOption)clOptions.get( 0
)).getDescriptor().getId() );
assertTrue(cl.hasOption(FILE_OPT));
//assertEquals( "-=-", ((CLOption)clOptions.get( 0 )).getArgument() );
assertEquals("-=-", cl.getOptionValue(FILE_OPT));
}
public void testSingleArg9() throws Exception
{
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// FILE
//};
Options options = new Options();
options.addOption(FILE);
//CLArgsParser parser = new CLArgsParser(
// new String[]{"--file","-=-"}
// , options );
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, new String[]{"--file","-=-"});
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( 1, size );
//assertEquals( FILE_OPT, ((CLOption)clOptions.get( 0
)).getDescriptor().getId() );
assertTrue(cl.hasOption(FILE_OPT));
//assertEquals( "-=-", ((CLOption)clOptions.get( 0 )).getArgument() );
assertEquals("-=-", cl.getOptionValue(FILE_OPT));
}
public void testCombinedArgs1() throws Exception
{
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// BLEE, TAINT
//};
Options options = new Options();
options.addOption(BLEE);
options.addOption(TAINT);
//CLArgsParser parser = new CLArgsParser(
// new String[]{"-bT","rest"}
// , options );
Parser parser = new PosixParser();
CommandLine cl = parser.parse(options, new String[]{"-bT","rest"});
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( 3, size);
//assertEquals( BLEE_OPT, ((CLOption)clOptions.get( 0
)).getDescriptor().getId());
assertTrue(cl.hasOption(BLEE_OPT));
//assertEquals( TAINT_OPT, ((CLOption)clOptions.get( 1
)).getDescriptor().getId());
assertTrue(cl.hasOption(TAINT_OPT));
//assertEquals( 0, ((CLOption)clOptions.get( 2
)).getDescriptor().getId());
//assertEquals( "rest", ((CLOption)clOptions.get( 2 )).getArgument());
assertTrue(cl.getArgList().contains("rest"));
}
public void testCombinedArgs2() throws Exception
{
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// BLEE, TAINT, FILE
//};
Options options = new Options();
options.addOption(BLEE);
options.addOption(TAINT);
options.addOption(FILE);
//CLArgsParser parser = new CLArgsParser(
// new String[]{"-bT","-fa"}
// , options );
Parser parser = new PosixParser();
CommandLine cl = parser.parse(options, new String[]{"-bT","-fa"});
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( 3, size);
//assertEquals( BLEE_OPT, ((CLOption)clOptions.get( 0
)).getDescriptor().getId());
assertTrue(cl.hasOption(BLEE_OPT));
//assertEquals( TAINT_OPT, ((CLOption)clOptions.get( 1
)).getDescriptor().getId());
assertTrue(cl.hasOption(TAINT_OPT));
//assertEquals( FILE_OPT, ((CLOption)clOptions.get( 2
)).getDescriptor().getId());
assertTrue(cl.hasOption(FILE_OPT));
//assertEquals( "a", ((CLOption)clOptions.get( 2 )).getArgument());
assertEquals("a", cl.getOptionValue(FILE_OPT));
}
public void testCombinedArgs3() throws Exception
{
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// BLEE, TAINT, FILE
//};
Options options = new Options();
options.addOption(BLEE);
options.addOption(TAINT);
options.addOption(FILE);
//CLArgsParser parser = new CLArgsParser(
// new String[]{"-bT","--","-fa"}// Should not detect trailing
option
// , options );
Parser parser = new PosixParser();
CommandLine cl = parser.parse(options, new String[]{"-bT","--","-fa"});
// Should not detect trailing option
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( 3, size);
//assertEquals( BLEE_OPT, ((CLOption)clOptions.get( 0
)).getDescriptor().getId());
assertTrue(cl.hasOption(BLEE_OPT));
//assertEquals( TAINT_OPT, ((CLOption)clOptions.get( 1
)).getDescriptor().getId());
assertTrue(cl.hasOption(TAINT_OPT));
//assertEquals( 0, ((CLOption)clOptions.get( 2
)).getDescriptor().getId());
//assertEquals( "-fa", ((CLOption)clOptions.get( 2 )).getArgument());
assertFalse(cl.hasOption(FILE_OPT));
assertTrue(cl.getArgList().contains("-fa"));
}
public void testCombinedArgs4() throws Exception
{
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// BLEE, TAINT, FILE
//};
Options options = new Options();
options.addOption(BLEE);
options.addOption(TAINT);
options.addOption(FILE);
//CLArgsParser parser = new CLArgsParser(
// new String[]{"-bT","rest","-fa"} // should detect trailing
option
// , options );
Parser parser = new PosixParser();
CommandLine cl = parser.parse(options, new
String[]{"-bT","rest","-fa"}); // should detect trailing option
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( 4, size);
//assertEquals( BLEE_OPT, ((CLOption)clOptions.get( 0
)).getDescriptor().getId());
assertTrue(cl.hasOption(BLEE_OPT));
//assertEquals( TAINT_OPT, ((CLOption)clOptions.get( 1
)).getDescriptor().getId());
assertTrue(cl.hasOption(TAINT_OPT));
//assertEquals( 0, ((CLOption)clOptions.get( 2
)).getDescriptor().getId());
//assertEquals( "rest", ((CLOption)clOptions.get( 2 )).getArgument());
assertTrue(cl.getArgList().contains("rest"));
//assertEquals( FILE_OPT, ((CLOption)clOptions.get( 3
)).getDescriptor().getId());
assertTrue(cl.hasOption(FILE_OPT));
//assertEquals( "a", ((CLOption)clOptions.get( 3 )).getArgument());
assertEquals("a", cl.getOptionValue(FILE_OPT));
}
public void test2ArgsParse() throws Exception
{
//"-Dstupid=idiot","are","--all","here"
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// DEFINE, ALL, CLEAR1, CASE_CHECK
//};
Options options = new Options();
options.addOption(DEFINE);
options.addOption(ALL);
options.addOption(CLEAR1);
options.addOption(CASE_CHECK);
//CLArgsParser parser = new CLArgsParser( ARGLIST2, options );
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, ARGLIST2);
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( size, 5 );
//assertEquals( ((CLOption)clOptions.get( 0 )).getDescriptor().getId(),
DEFINE_OPT );
assertTrue(cl.hasOption(DEFINE_OPT));
//assertEquals( ((CLOption)clOptions.get( 1 )).getDescriptor().getId(),
0 );
assertEquals("are", cl.getArgs()[0]);
//assertEquals( ((CLOption)clOptions.get( 2 )).getDescriptor().getId(),
ALL_OPT );
assertTrue(cl.hasOption(ALL_OPT));
//assertEquals( ((CLOption)clOptions.get( 3 )).getDescriptor().getId(),
0 );
assertEquals("here", cl.getArgs()[1]);
//assertEquals( ((CLOption)clOptions.get( 4 )).getDescriptor().getId(),
CASE_CHECK_OPT );
assertTrue(cl.hasOption(CASE_CHECK_OPT));
//CLOption option = (CLOption)clOptions.get( 0 );
//assertEquals( "stupid", option.getArgument( 0 ) );
//assertEquals( "idiot", option.getArgument( 1 ) );
assertEquals("stupid", cl.getOptionValues(DEFINE_OPT)[0]);
assertEquals("idiot", cl.getOptionValues(DEFINE_OPT)[1]);
}
public void test2ArgsParse2() throws Exception
{
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// DEFINE
//};
Options options = new Options();
options.addOption(DEFINE);
//CLArgsParser parser = new CLArgsParser(
// new String[] {"--define","a-b,c=d-e,f"}, // Check "-" is
allowed in arg2
// options );
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, new String[]
{"--define","a-b,c=d-e,f"}); // Check "-" is allowed in arg2
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( 1, size );
//assertEquals( DEFINE_OPT, ((CLOption)clOptions.get( 0
)).getDescriptor().getId() );
assertTrue(cl.hasOption(DEFINE_OPT));
//CLOption option = (CLOption)clOptions.get( 0 );
//assertEquals( "a-b,c", option.getArgument( 0 ) );
//assertEquals( "d-e,f", option.getArgument( 1 ) );
assertEquals("a-b,c", cl.getOptionValues(DEFINE_OPT)[0]);
assertEquals("d-e,f", cl.getOptionValues(DEFINE_OPT)[1]);
}
public void test2ArgsParse3() throws Exception
{
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// DEFINE
//};
Options options = new Options();
options.addOption(DEFINE);
//CLArgsParser parser = new CLArgsParser(
// new String[] {"-D","A-b,c","G-e,f"},
// options );
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, new String[]
{"-D","A-b,c","G-e,f"});
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( 1, size );
//assertEquals( DEFINE_OPT, ((CLOption)clOptions.get( 0
)).getDescriptor().getId() );
assertTrue(cl.hasOption(DEFINE_OPT));
//CLOption option = (CLOption)clOptions.get( 0 );
//assertEquals( "A-b,c", option.getArgument( 0 ) );
//assertEquals( "G-e,f", option.getArgument( 1 ) );
assertEquals("A-b,c", cl.getOptionValues(DEFINE_OPT)[0]);
assertEquals("G-e,f", cl.getOptionValues(DEFINE_OPT)[1]);
}
public void test2ArgsParse4() throws Exception
{
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// DEFINE_MANY
//};
Options options = new Options();
options.addOption(DEFINE_MANY);
//CLArgsParser parser = new CLArgsParser(
// new String[] {"-Dval1=-1","-D","val2=-2",
"--define=val-3=-3","--define","val4-=-4"},
// options );
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, new String[]
{"-Dval1=-1","-D","val2=-2", "--define=val-3=-3","--define","val4-=-4"});
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( 4, size );
//for (int i=0;i<size;i++){
// assertEquals( DEFINE_OPT, ((CLOption)clOptions.get( i
)).getDescriptor().getId() );
//}
Iterator it = cl.iterator();
//CLOption option;
//option = (CLOption)clOptions.get( 0 );
//assertEquals( "val1", option.getArgument( 0 ) );
//assertEquals( "-1", option.getArgument( 1 ) );
Option opt = (Option) it.next();
assertEquals(DEFINE.getOpt(), opt.getOpt());
assertEquals("val1", opt.getValue(0));
assertEquals("-1", opt.getValue(1));
//option = (CLOption)clOptions.get( 1 );
//assertEquals( "val2", option.getArgument( 0 ) );
//assertEquals( "-2", option.getArgument( 1 ) );
opt = (Option) it.next();
assertEquals(DEFINE.getOpt(), opt.getOpt());
assertEquals("val2", opt.getValue(0));
assertEquals("-2", opt.getValue(1));
//option = (CLOption)clOptions.get( 2 );
//assertEquals( "val-3", option.getArgument( 0 ) );
//assertEquals( "-3", option.getArgument( 1 ) );
opt = (Option) it.next();
assertEquals(DEFINE.getOpt(), opt.getOpt());
assertEquals("val-3", opt.getValue(0));
assertEquals("-3", opt.getValue(1));
//option = (CLOption)clOptions.get( 3 );
//assertEquals( "val4-", option.getArgument( 0 ) );
//assertEquals( "-4", option.getArgument( 1 ) );
opt = (Option) it.next();
assertEquals(DEFINE.getOpt(), opt.getOpt());
assertEquals("val4-", opt.getValue(0));
assertEquals("-4", opt.getValue(1));
}
/**
* TODO no equivalent for the ParserControl class
*/
/*
public void testPartParse() throws Exception
{
CLOptionDescriptor[] options = new CLOptionDescriptor[]
{
YOU
};
ParserControl control = new AbstractParserControl()
{
public boolean isFinished( int lastOptionCode )
{
return (lastOptionCode == YOU_OPT);
}
};
CLArgsParser parser = new CLArgsParser( ARGLIST1, options, control );
List clOptions = parser.getArguments();
int size = clOptions.size();
assertEquals( size, 1 );
assertEquals( ((CLOption)clOptions.get( 0 )).getDescriptor().getId(),
YOU_OPT );
}
public void test2PartParse() throws Exception
{
CLOptionDescriptor[] options1 = new CLOptionDescriptor[]
{
YOU
};
CLOptionDescriptor[] options2 = new CLOptionDescriptor[]
{
ALL, CLEAR1, CLEAR2, CLEAR3, CLEAR5
};
ParserControl control1 = new AbstractParserControl()
{
public boolean isFinished( int lastOptionCode )
{
return (lastOptionCode == YOU_OPT);
}
};
CLArgsParser parser1 = new CLArgsParser( ARGLIST1, options1, control1 );
assertNull( parser1.getErrorString(), parser1.getErrorString() );
List clOptions1 = parser1.getArguments();
int size1 = clOptions1.size();
assertEquals( size1, 1 );
assertEquals( ((CLOption)clOptions1.get( 0 )).getDescriptor().getId(),
YOU_OPT );
CLArgsParser parser2 =
new CLArgsParser( parser1.getUnparsedArgs(), options2 );
assertNull( parser2.getErrorString(), parser2.getErrorString() );
List clOptions2 = parser2.getArguments();
int size2 = clOptions2.size();
assertEquals( size2, 7 );
assertEquals( ((CLOption)clOptions2.get( 0 )).getDescriptor().getId(),
0 );
assertEquals( ((CLOption)clOptions2.get( 1 )).getDescriptor().getId(),
ALL_OPT );
assertEquals( ((CLOption)clOptions2.get( 2 )).getDescriptor().getId(),
CLEAR1_OPT );
assertEquals( ((CLOption)clOptions2.get( 3 )).getDescriptor().getId(),
CLEAR2_OPT );
assertEquals( ((CLOption)clOptions2.get( 4 )).getDescriptor().getId(),
CLEAR3_OPT );
assertEquals( ((CLOption)clOptions2.get( 5 )).getDescriptor().getId(),
CLEAR5_OPT );
assertEquals( ((CLOption)clOptions2.get( 6 )).getDescriptor().getId(),
0 );
}
public void test2PartPartialParse() throws Exception
{
CLOptionDescriptor[] options1 = new CLOptionDescriptor[]
{
YOU, ALL, CLEAR1
};
CLOptionDescriptor[] options2 = new CLOptionDescriptor[]{};
ParserControl control1 = new AbstractParserControl()
{
public boolean isFinished( int lastOptionCode )
{
return (lastOptionCode == CLEAR1_OPT);
}
};
CLArgsParser parser1 = new CLArgsParser( ARGLIST1, options1, control1 );
assertNull( parser1.getErrorString(), parser1.getErrorString() );
List clOptions1 = parser1.getArguments();
int size1 = clOptions1.size();
assertEquals( size1, 4 );
assertEquals( ((CLOption)clOptions1.get( 0 )).getDescriptor().getId(),
YOU_OPT );
assertEquals( ((CLOption)clOptions1.get( 1 )).getDescriptor().getId(),
0 );
assertEquals( ((CLOption)clOptions1.get( 2 )).getDescriptor().getId(),
ALL_OPT );
assertEquals( ((CLOption)clOptions1.get( 3 )).getDescriptor().getId(),
CLEAR1_OPT );
assertTrue( parser1.getUnparsedArgs()[0].equals( "ler" ) );
CLArgsParser parser2 =
new CLArgsParser( parser1.getUnparsedArgs(), options2 );
assertNull( parser2.getErrorString(), parser2.getErrorString() );
List clOptions2 = parser2.getArguments();
int size2 = clOptions2.size();
assertEquals( size2, 2 );
assertEquals( ((CLOption)clOptions2.get( 0 )).getDescriptor().getId(),
0 );
assertEquals( ((CLOption)clOptions2.get( 1 )).getDescriptor().getId(),
0 );
}
*/
public void testDuplicatesFail() throws Exception
{
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// YOU, ALL, CLEAR1, CLEAR2, CLEAR3, CLEAR5
//};
Options options = new Options();
options.addOption(YOU);
options.addOption(ALL);
options.addOption(CLEAR1);
options.addOption(CLEAR2);
options.addOption(CLEAR3);
options.addOption(CLEAR5);
//CLArgsParser parser = new CLArgsParser( ARGLIST1, options );
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, ARGLIST1);
}
public void testIncomplete2Args() throws Exception
{
//"-Dstupid="
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// DEFINE
//};
Options options = new Options();
options.addOption(DEFINE);
//CLArgsParser parser = new CLArgsParser( new String[]{"-Dstupid="},
options );
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, new String[]{"-Dstupid="});
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( size, 1 );
//CLOption option = (CLOption)clOptions.get( 0 );
//assertEquals( option.getDescriptor().getId(), DEFINE_OPT );
assertTrue(cl.hasOption(DEFINE_OPT));
//assertEquals( option.getArgument( 0 ), "stupid" );
//assertEquals( option.getArgument( 1 ), "" );
assertEquals("",
cl.getOptionProperties(DEFINE.getOpt()).getProperty("stupid"));
}
public void testIncomplete2ArgsMixed() throws Exception
{
//"-Dstupid=","-c"
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// DEFINE, CLEAR1
//};
Options options = new Options();
options.addOption(DEFINE);
options.addOption(CLEAR1);
String[] args = new String[]{"-Dstupid=", "-c"};
//CLArgsParser parser = new CLArgsParser( args, options );
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, args);
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( size, 2 );
//assertEquals( ((CLOption)clOptions.get( 1 )).getDescriptor().getId(),
CLEAR1_OPT );
assertTrue(cl.hasOption(CLEAR1_OPT));
//CLOption option = (CLOption)clOptions.get( 0 );
//assertEquals( option.getDescriptor().getId(), DEFINE_OPT );
assertTrue(cl.hasOption(DEFINE_OPT));
//assertEquals( option.getArgument( 0 ), "stupid" );
//assertEquals( option.getArgument( 1 ), "" );
assertEquals("",
cl.getOptionProperties(DEFINE.getOpt()).getProperty("stupid"));
}
public void testIncomplete2ArgsMixedNoEq() throws Exception
{
//"-Dstupid","-c"
//CLOptionDescriptor[] options = new CLOptionDescriptor[]
//{
// DEFINE, CLEAR1
//};
Options options = new Options();
options.addOption(DEFINE);
options.addOption(CLEAR1);
String[] args = new String[]{"-DStupid", "-c"};
//CLArgsParser parser = new CLArgsParser( args, options );
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, args);
//List clOptions = parser.getArguments();
//int size = clOptions.size();
//assertEquals( size, 2 );
//assertEquals( ((CLOption)clOptions.get( 1 )).getDescriptor().getId(),
CLEAR1_OPT );
assertTrue(cl.hasOption(CLEAR1_OPT));
//CLOption option = (CLOption)clOptions.get( 0 );
//assertEquals( option.getDescriptor().getId(), DEFINE_OPT );
assertTrue(cl.hasOption(DEFINE_OPT));
//assertEquals( option.getArgument( 0 ), "Stupid" );
//assertEquals( option.getArgument( 1 ), "" );
//assertEquals(2, cl.getOptionValues(DEFINE_OPT).length);
//assertEquals("Stupid", cl.getOptionValues(DEFINE_OPT)[0]);
//assertEquals("", cl.getOptionValues(DEFINE_OPT)[1]);
assertEquals("",
cl.getOptionProperties(DEFINE.getOpt()).getProperty("Stupid")); // todo
divergence with Avalon CLI, "true" instead of ""
}
/**
* Test the getArgumentById and getArgumentByName lookup methods.
*/
public void testArgumentLookup() throws Exception
{
String[] args = {"-f", "testarg"};
//CLOptionDescriptor[] options = {FILE};
//CLArgsParser parser = new CLArgsParser( args, options );
Options options = new Options();
options.addOption(FILE);
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, args);
//CLOption optionById = parser.getArgumentById( FILE_OPT );
//assertNotNull( optionById );
//assertEquals( FILE_OPT, optionById.getDescriptor().getId() );
assertTrue(cl.hasOption(FILE_OPT));
//CLOption optionByName = parser.getArgumentByName( FILE.getName() );
//assertNotNull( optionByName );
//assertEquals( FILE_OPT, optionByName.getDescriptor().getId() );
assertTrue(cl.hasOption("file"));
}
/**
* Test that you can have null long forms.
*/
public void testNullLongForm() throws Exception
{
//CLOptionDescriptor test = new CLOptionDescriptor( null,
CLOptionDescriptor.ARGUMENT_DISALLOWED, 'n', "test null long form" );
Option test = OptionBuilder.withDescription("test null long
form").create('n');
String[] args = {"-n", "testarg"};
//CLOptionDescriptor[] options = {test};
//CLArgsParser parser = new CLArgsParser( args, options );
Options options = new Options();
options.addOption(test);
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, args);
//CLOption optionByID = parser.getArgumentById( 'n' );
//assertNotNull( optionByID );
//assertEquals( 'n', optionByID.getDescriptor().getId() );
assertTrue(cl.hasOption('n'));
//CLOption optionByName = parser.getArgumentByName( FILE.getName() );
//assertNull( "Looking for non-existent option by name", optionByName );
assertFalse(cl.hasOption(FILE_OPT));
}
/**
* Test that you can have null descriptions.
*/
public void testNullDescription() throws Exception
{
//CLOptionDescriptor test = new CLOptionDescriptor( "nulltest",
CLOptionDescriptor.ARGUMENT_DISALLOWED, 'n', null );
Option test = OptionBuilder.withLongOpt("nulltest").create('n');
String[] args = {"-n", "testarg"};
//CLOptionDescriptor[] options = {test};
//CLArgsParser parser = new CLArgsParser( args, options );
Options options = new Options();
options.addOption(test);
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, args);
//CLOption optionByID = parser.getArgumentById( 'n' );
//assertNotNull( optionByID );
//assertEquals( 'n', optionByID.getDescriptor().getId() );
assertTrue(cl.hasOption('n'));
//StringBuffer sb = CLUtil.describeOptions( options );
StringBuffer sb = new StringBuffer();
new HelpFormatter().renderOptions(sb,80, options, 0, 1);
//String lineSeparator = System.getProperty( "line.separator" );
assertEquals( "Testing display of null description",
// "\t-n, --nulltest" + lineSeparator,
"-n,--nulltest",
sb.toString() );
}
/*
* TODO add tests to check for:
* - name clash
* - long option abbreviations (match shortest unique abbreviation)
*/
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]