You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by jk...@apache.org on 2002/08/05 01:04:53 UTC
cvs commit: jakarta-commons/cli/src/test/org/apache/commons/cli ApplicationTest.java PatternOptionBuilderTest.java ValuesTest.java
jkeyes 2002/08/04 16:04:53
Modified: cli/src/java/org/apache/commons/cli CommandLine.java
GnuParser.java Option.java OptionBuilder.java
PosixParser.java
cli/src/test/org/apache/commons/cli ApplicationTest.java
PatternOptionBuilderTest.java ValuesTest.java
Log:
bug 11458 fixed
Revision Changes Path
1.6 +49 -4 jakarta-commons/cli/src/java/org/apache/commons/cli/CommandLine.java
Index: CommandLine.java
===================================================================
RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/CommandLine.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- CommandLine.java 3 Aug 2002 23:45:09 -0000 1.5
+++ CommandLine.java 4 Aug 2002 23:04:52 -0000 1.6
@@ -103,13 +103,22 @@
/** <p>Query to see if an option has been set.</p>
*
- * @param opt Short single-character name of the option
+ * @param opt Short name of the option
* @return true if set, false if not
*/
public boolean hasOption(String opt) {
return options.containsKey( opt );
}
+ /** <p>Query to see if an option has been set.</p>
+ *
+ * @param opt character name of the option
+ * @return true if set, false if not
+ */
+ public boolean hasOption( char opt ) {
+ return hasOption( String.valueOf( opt ) );
+ }
+
/**
* @param opt the name of the option
* @return the type of opt
@@ -120,6 +129,14 @@
return res == null ? null : TypeHandler.createValue(res, type);
}
+ /**
+ * @param opt the name of the option
+ * @return the type of opt
+ */
+ public Object getOptionObject( char opt ) {
+ return getOptionObject( String.valueOf( opt ) );
+ }
+
/** <p>Retrieve the argument, if any, of an option.</p>
*
* @param opt the name of the option
@@ -129,18 +146,36 @@
return (String)((Option)options.get( opt )).getValue();
}
+ /** <p>Retrieve the argument, if any, of an option.</p>
+ *
+ * @param opt the character name of the option
+ * @return Value of the argument if option is set, and has an argument, else null.
+ */
+ public String getOptionValue( char opt ) {
+ return getOptionValue( String.valueOf( opt ) );
+ }
+
/** <p>Retrieves the array of values, if any, of an option.</p>
*
- * @param opt Single-character name of the option
+ * @param opt string name of the option
* @return An array of values if the option is set, and has an argument, else null.
*/
public String[] getOptionValues( String opt ) {
return (String[])((Option)options.get( opt )).getValues();
}
+
+ /** <p>Retrieves the array of values, if any, of an option.</p>
+ *
+ * @param opt character name of the option
+ * @return An array of values if the option is set, and has an argument, else null.
+ */
+ public String[] getOptionValues( char opt ) {
+ return getOptionValues( String.valueOf( opt ) );
+ }
- /** <p>Retrieve the argument, if any, of an option.</p>
+ /** <p>Retrieve the argument, if any, of an option.</p>
*
- * @param opt Short single-character name of the option
+ * @param opt name of the option
* @param defaultValue is the default value to be returned if the option is not specified
* @return Value of the argument if option is set, and has an argument, else null.
*/
@@ -149,6 +184,16 @@
return ( answer != null ) ? answer : defaultValue;
}
+ /** <p>Retrieve the argument, if any, of an option.</p>
+ *
+ * @param opt character name of the option
+ * @param defaultValue is the default value to be returned if the option is not specified
+ * @return Value of the argument if option is set, and has an argument, else null.
+ */
+ public String getOptionValue( char opt, String defaultValue ) {
+ return getOptionValue( String.valueOf( opt ), defaultValue );
+ }
+
/** <p>Retrieve any left-over non-recognized options and arguments</p>
*
* @return an array of remaining items passed in but not parsed
1.4 +41 -6 jakarta-commons/cli/src/java/org/apache/commons/cli/GnuParser.java
Index: GnuParser.java
===================================================================
RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/GnuParser.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- GnuParser.java 3 Aug 2002 23:45:09 -0000 1.3
+++ GnuParser.java 4 Aug 2002 23:04:52 -0000 1.4
@@ -198,9 +198,26 @@
}
// its a value
else {
- if( !opt.addValue( var ) ) {
+ char sep = opt.getValueSeparator();
+
+ if( sep > 0 ) {
+ int findex;
+ while( ( findex = var.indexOf( sep ) ) != -1 ) {
+ String val = var.substring( 0, findex );
+ var = var.substring( findex + 1);
+ if( !opt.addValue( val ) ) {
+ iter.previous();
+ return;
+ }
+ }
+ if( !opt.addValue( var ) ) {
+ iter.previous();
+ return;
+ };
+ }
+ else if( !opt.addValue( var ) ) {
iter.previous();
- break;
+ return;
}
}
}
@@ -223,7 +240,25 @@
if( specialOption != null && opt == null) {
opt = specialOption;
value = arg.substring( 2 );
- opt.addValue( value );
+ char sep = opt.getValueSeparator();
+
+ if( sep > 0 ) {
+ int findex;
+ while( ( findex = value.indexOf( sep ) ) != -1 ) {
+ String val = value.substring( 0, findex );
+ value = value.substring( findex + 1);
+ if( !opt.addValue( val ) ) {
+ cmd.addArg( val );
+ }
+ }
+ if( !opt.addValue( value ) ) {
+ cmd.addArg( value );
+ }
+ }
+ else {
+ // add the argument value
+ opt.addValue( value );
+ }
}
// if there is no option throw an UnrecognisedOptionException
1.8 +21 -0 jakarta-commons/cli/src/java/org/apache/commons/cli/Option.java
Index: Option.java
===================================================================
RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/Option.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- Option.java 3 Aug 2002 23:45:09 -0000 1.7
+++ Option.java 4 Aug 2002 23:04:52 -0000 1.8
@@ -307,6 +307,16 @@
this.numberOfArgs = num;
}
+ private char valuesep;
+
+ public void setValueSeparator( char sep ) {
+ this.valuesep = sep;
+ }
+
+ public char getValueSeparator() {
+ return this.valuesep;
+ }
+
/** <p>Returns the number of argument values this Option can take.</p>
*
* @return num the number of argument values
@@ -353,6 +363,7 @@
* @param value is a/the value of this Option
*/
public boolean addValue( String value ) {
+
switch( numberOfArgs ) {
case UNINITIALIZED:
return false;
@@ -374,6 +385,16 @@
*/
public String getValue() {
return this.values.size()==0 ? null : (String)this.values.get( 0 );
+ }
+
+ /**
+ * @return the specified value of this Option or
+ * null if there are no values.
+ */
+ public String getValue( int index )
+ throws IndexOutOfBoundsException
+ {
+ return ( this.values.size()==0 ) ? null : (String)this.values.get( index );
}
/**
1.4 +48 -4 jakarta-commons/cli/src/java/org/apache/commons/cli/OptionBuilder.java
Index: OptionBuilder.java
===================================================================
RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/OptionBuilder.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- OptionBuilder.java 3 Aug 2002 23:45:09 -0000 1.3
+++ OptionBuilder.java 4 Aug 2002 23:04:52 -0000 1.4
@@ -83,6 +83,8 @@
/** option type */
private static Object type;
+ private static char valuesep;
+
/** option builder instance */
private static OptionBuilder instance = new OptionBuilder();
@@ -145,6 +147,48 @@
}
/**
+ * <p>The next Option created uses <code>sep</code> as a means to
+ * separate argument values.</p>
+ *
+ * <b>Example:</b>
+ * <pre>
+ * Option opt = OptionBuilder.withValueSeparator( ':' )
+ * .create( 'D' );
+ *
+ * CommandLine line = parser.parse( args );
+ * String propertyName = opt.getValue( 0 );
+ * String propertyValue = opt.getValue( 1 );
+ * </pre>
+ *
+ * @return the OptionBuilder instance
+ */
+ public static OptionBuilder withValueSeparator( char sep ) {
+ instance.valuesep = sep;
+ return instance;
+ }
+
+ /**
+ * <p>The next Option created uses '<code>=</code>' as a means to
+ * separate argument values.</p>
+ *
+ * <b>Example:</b>
+ * <pre>
+ * Option opt = OptionBuilder.withValueSeparator( )
+ * .create( 'D' );
+ *
+ * CommandLine line = parser.parse( args );
+ * String propertyName = opt.getValue( 0 );
+ * String propertyValue = opt.getValue( 1 );
+ * </pre>
+ *
+ * @return the OptionBuilder instance
+ */
+ public static OptionBuilder withValueSeparator( ) {
+ instance.valuesep = '=';
+ return instance;
+ }
+
+ /**
* <p>The next Option created will be required if <code>required</code>
* is true.</p>
*
@@ -237,7 +281,7 @@
option.setRequired( required );
option.setArgs( numberOfArgs );
option.setType( type );
-
+ option.setValueSeparator( valuesep );
// reset the OptionBuilder properties
instance.reset();
1.5 +42 -7 jakarta-commons/cli/src/java/org/apache/commons/cli/PosixParser.java
Index: PosixParser.java
===================================================================
RCS file: /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/PosixParser.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- PosixParser.java 3 Aug 2002 23:45:09 -0000 1.4
+++ PosixParser.java 4 Aug 2002 23:04:52 -0000 1.5
@@ -181,8 +181,26 @@
throw new MissingArgumentException( "Missing argument value for " + opt.getOpt() );
}
- // add the argument value
- opt.addValue( token.substring(i+1) );
+ String var = token.substring(i+1);
+ char sep = opt.getValueSeparator();
+
+ if( sep > 0 ) {
+ int findex;
+ while( ( findex = var.indexOf( sep ) ) != -1 ) {
+ String val = var.substring( 0, findex );
+ var = var.substring( findex + 1);
+ if( !opt.addValue( val ) ) {
+ cmd.addArg( val );
+ }
+ }
+ if( !opt.addValue( var ) ) {
+ cmd.addArg( var );
+ };
+ }
+ else {
+ // add the argument value
+ opt.addValue( token.substring(i+1) );
+ }
// set the option
cmd.setOpt( opt );
@@ -289,9 +307,26 @@
}
// its a value
else {
- if( !opt.addValue( var ) ) {
+ char sep = opt.getValueSeparator();
+
+ if( sep > 0 ) {
+ int findex;
+ while( ( findex = var.indexOf( sep ) ) != -1 ) {
+ String val = var.substring( 0, findex );
+ var = var.substring( findex + 1);
+ if( !opt.addValue( val ) ) {
+ iter.previous();
+ return;
+ }
+ }
+ if( !opt.addValue( var ) ) {
+ iter.previous();
+ return;
+ };
+ }
+ else if( !opt.addValue( var ) ) {
iter.previous();
- break;
+ return;
}
}
}
1.7 +0 -1 jakarta-commons/cli/src/test/org/apache/commons/cli/ApplicationTest.java
Index: ApplicationTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/ApplicationTest.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- ApplicationTest.java 3 Aug 2002 23:45:09 -0000 1.6
+++ ApplicationTest.java 4 Aug 2002 23:04:53 -0000 1.7
@@ -49,7 +49,6 @@
options.addOption( "listener", true, "add an instance of a class as a project listener" );
options.addOption( "buildfile", true, "use given buildfile" );
options.addOption( OptionBuilder.withDescription( "use value for given property" )
- .hasArg()
.hasArgs()
.create( 'D' ) );
//, null, true, , false, true );
1.5 +12 -0 jakarta-commons/cli/src/test/org/apache/commons/cli/PatternOptionBuilderTest.java
Index: PatternOptionBuilderTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/PatternOptionBuilderTest.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- PatternOptionBuilderTest.java 30 Jul 2002 23:06:21 -0000 1.4
+++ PatternOptionBuilderTest.java 4 Aug 2002 23:04:53 -0000 1.5
@@ -47,15 +47,27 @@
CommandLineParser parser = CommandLineParserFactory.newParser();
CommandLine line = parser.parse(options,args);
+
+ // tests the char methods of CommandLine that delegate to
+ // the String methods
assertEquals("flag a", "foo", line.getOptionValue("a"));
+ assertEquals("flag a", "foo", line.getOptionValue('a'));
assertEquals("string flag a", "foo", line.getOptionObject("a"));
+ assertEquals("string flag a", "foo", line.getOptionObject('a'));
assertEquals("object flag b", new java.util.Vector(), line.getOptionObject("b"));
+ assertEquals("object flag b", new java.util.Vector(), line.getOptionObject('b'));
assertEquals("boolean true flag c", true, line.hasOption("c"));
+ assertEquals("boolean true flag c", true, line.hasOption('c'));
assertEquals("boolean false flag d", false, line.hasOption("d"));
+ assertEquals("boolean false flag d", false, line.hasOption('d'));
assertEquals("file flag e", new java.io.File("build.xml"), line.getOptionObject("e"));
+ assertEquals("file flag e", new java.io.File("build.xml"), line.getOptionObject('e'));
assertEquals("class flag f", java.util.Calendar.class, line.getOptionObject("f"));
+ assertEquals("class flag f", java.util.Calendar.class, line.getOptionObject('f'));
assertEquals("number flag n", new Float(4.5), line.getOptionObject("n"));
+ assertEquals("number flag n", new Float(4.5), line.getOptionObject('n'));
assertEquals("url flag t", new java.net.URL("http://jakarta.apache.org/"), line.getOptionObject("t"));
+ assertEquals("url flag t", new java.net.URL("http://jakarta.apache.org/"), line.getOptionObject('t'));
/// DATES NOT SUPPORTED YET.
// assertEquals("number flag t", new java.util.Date(1023400137276L), line.getOptionObject('z'));
// input is: "Thu Jun 06 17:48:57 EDT 2002"
1.6 +84 -3 jakarta-commons/cli/src/test/org/apache/commons/cli/ValuesTest.java
Index: ValuesTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/ValuesTest.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ValuesTest.java 3 Aug 2002 23:45:09 -0000 1.5
+++ ValuesTest.java 4 Aug 2002 23:04:53 -0000 1.6
@@ -20,6 +20,7 @@
{
/** CommandLine instance */
private CommandLine _cmdline = null;
+ private Option _option = null;
public static Test suite() {
return new TestSuite( ValuesTest.class );
@@ -75,6 +76,26 @@
opts.addOption( OptionBuilder.withLongOpt( "i" )
.withDescription( "set -i")
.create( 'i' ) );
+
+ opts.addOption( OptionBuilder.withLongOpt( "j" )
+ .hasArgs( 2 )
+ .withDescription( "set -j")
+ .withValueSeparator( '=' )
+ .create( 'j' ) );
+
+ opts.addOption( OptionBuilder.withLongOpt( "k" )
+ .hasArgs( )
+ .withDescription( "set -k")
+ .withValueSeparator( '=' )
+ .create( 'k' ) );
+
+ _option = OptionBuilder.withLongOpt( "m" )
+ .hasArgs( )
+ .withDescription( "set -m")
+ .withValueSeparator( )
+ .create( 'm' );
+
+ opts.addOption( _option );
String[] args = new String[] { "-a",
"-b", "foo",
@@ -85,7 +106,12 @@
"arg1", "arg2",
"-g", "val1", "val2" , "arg3",
"-h", "val1", "-i",
- "-h", "val2" };
+ "-h", "val2",
+ "-jkey=value",
+ "-j", "key=value",
+ "-kkey1=value1",
+ "-kkey2=value2",
+ "-mkey=value"};
CommandLineParser parser = CommandLineParserFactory.newParser();
@@ -154,8 +180,63 @@
public void testExtraArgs()
{
- String[] args = new String[] { "arg1", "arg2", "arg3" };
- assertTrue( _cmdline.getArgs().length == 3 );
+ String[] args = new String[] { "arg1", "arg2", "arg3", "key=value" };
+ assertTrue( _cmdline.getArgs().length == 4 );
assertTrue( Arrays.equals( args, _cmdline.getArgs() ) );
+ }
+
+ public void testCharSeparator()
+ {
+ // tests the char methods of CommandLine that delegate to
+ // the String methods
+ String[] values = new String[] { "key", "value" };
+ assertTrue( _cmdline.hasOption( "j" ) );
+ assertTrue( _cmdline.hasOption( 'j' ) );
+ assertTrue( _cmdline.getOptionValues( "j" ).length == 2);
+ assertTrue( _cmdline.getOptionValues( 'j' ).length == 2);
+ assertTrue( Arrays.equals( values, _cmdline.getOptionValues( "j" ) ) );
+ assertTrue( Arrays.equals( values, _cmdline.getOptionValues( 'j' ) ) );
+
+ values = new String[] { "key1", "value1", "key2", "value2" };
+ assertTrue( _cmdline.hasOption( "k" ) );
+ assertTrue( _cmdline.hasOption( 'k' ) );
+ assertTrue( _cmdline.getOptionValues( "k" ).length == 4 );
+ assertTrue( _cmdline.getOptionValues( 'k' ).length == 4 );
+ assertTrue( Arrays.equals( values, _cmdline.getOptionValues( "k" ) ) );
+ assertTrue( Arrays.equals( values, _cmdline.getOptionValues( 'k' ) ) );
+
+ values = new String[] { "key", "value" };
+ assertTrue( _cmdline.hasOption( "m" ) );
+ assertTrue( _cmdline.hasOption( 'm' ) );
+ assertTrue( _cmdline.getOptionValues( "m" ).length == 2);
+ assertTrue( _cmdline.getOptionValues( 'm' ).length == 2);
+ assertTrue( Arrays.equals( values, _cmdline.getOptionValues( "m" ) ) );
+ assertTrue( Arrays.equals( values, _cmdline.getOptionValues( 'm' ) ) );
+ }
+
+ public void testGetValue()
+ {
+ // the 'm' option
+ assertTrue( _option.getValues().length == 2 );
+ assertEquals( _option.getValue(), "key" );
+ assertEquals( _option.getValue( 0 ), "key" );
+ assertEquals( _option.getValue( 1 ), "value" );
+
+ try {
+ assertEquals( _option.getValue( 2 ), "key" );
+ fail( "IndexOutOfBounds not caught" );
+ }
+ catch( IndexOutOfBoundsException exp ) {
+
+ }
+
+ try {
+ assertEquals( _option.getValue( -1 ), "key" );
+ fail( "IndexOutOfBounds not caught" );
+ }
+ catch( IndexOutOfBoundsException exp ) {
+
+ }
+
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>