Skip to content

Commit 1060ee7

Browse files
author
John Keyes
committed
bug 11458 fixed
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@129796 13f79535-47bb-0310-9956-ffa450edef68
1 parent e6ca08f commit 1060ee7

8 files changed

Lines changed: 297 additions & 25 deletions

File tree

src/java/org/apache/commons/cli/CommandLine.java

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,22 @@ public class CommandLine {
103103

104104
/** <p>Query to see if an option has been set.</p>
105105
*
106-
* @param opt Short single-character name of the option
106+
* @param opt Short name of the option
107107
* @return true if set, false if not
108108
*/
109109
public boolean hasOption(String opt) {
110110
return options.containsKey( opt );
111111
}
112112

113+
/** <p>Query to see if an option has been set.</p>
114+
*
115+
* @param opt character name of the option
116+
* @return true if set, false if not
117+
*/
118+
public boolean hasOption( char opt ) {
119+
return hasOption( String.valueOf( opt ) );
120+
}
121+
113122
/**
114123
* @param opt the name of the option
115124
* @return the type of opt
@@ -120,6 +129,14 @@ public Object getOptionObject( String opt ) {
120129
return res == null ? null : TypeHandler.createValue(res, type);
121130
}
122131

132+
/**
133+
* @param opt the name of the option
134+
* @return the type of opt
135+
*/
136+
public Object getOptionObject( char opt ) {
137+
return getOptionObject( String.valueOf( opt ) );
138+
}
139+
123140
/** <p>Retrieve the argument, if any, of an option.</p>
124141
*
125142
* @param opt the name of the option
@@ -129,18 +146,36 @@ public String getOptionValue( String opt ) {
129146
return (String)((Option)options.get( opt )).getValue();
130147
}
131148

149+
/** <p>Retrieve the argument, if any, of an option.</p>
150+
*
151+
* @param opt the character name of the option
152+
* @return Value of the argument if option is set, and has an argument, else null.
153+
*/
154+
public String getOptionValue( char opt ) {
155+
return getOptionValue( String.valueOf( opt ) );
156+
}
157+
132158
/** <p>Retrieves the array of values, if any, of an option.</p>
133159
*
134-
* @param opt Single-character name of the option
160+
* @param opt string name of the option
135161
* @return An array of values if the option is set, and has an argument, else null.
136162
*/
137163
public String[] getOptionValues( String opt ) {
138164
return (String[])((Option)options.get( opt )).getValues();
139165
}
166+
167+
/** <p>Retrieves the array of values, if any, of an option.</p>
168+
*
169+
* @param opt character name of the option
170+
* @return An array of values if the option is set, and has an argument, else null.
171+
*/
172+
public String[] getOptionValues( char opt ) {
173+
return getOptionValues( String.valueOf( opt ) );
174+
}
140175

141-
/** <p>Retrieve the argument, if any, of an option.</p>
176+
/** <p>Retrieve the argument, if any, of an option.</p>
142177
*
143-
* @param opt Short single-character name of the option
178+
* @param opt name of the option
144179
* @param defaultValue is the default value to be returned if the option is not specified
145180
* @return Value of the argument if option is set, and has an argument, else null.
146181
*/
@@ -149,6 +184,16 @@ public String getOptionValue( String opt, String defaultValue ) {
149184
return ( answer != null ) ? answer : defaultValue;
150185
}
151186

187+
/** <p>Retrieve the argument, if any, of an option.</p>
188+
*
189+
* @param opt character name of the option
190+
* @param defaultValue is the default value to be returned if the option is not specified
191+
* @return Value of the argument if option is set, and has an argument, else null.
192+
*/
193+
public String getOptionValue( char opt, String defaultValue ) {
194+
return getOptionValue( String.valueOf( opt ), defaultValue );
195+
}
196+
152197
/** <p>Retrieve any left-over non-recognized options and arguments</p>
153198
*
154199
* @return an array of remaining items passed in but not parsed

src/java/org/apache/commons/cli/GnuParser.java

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/GnuParser.java,v 1.3 2002/08/03 23:45:09 jkeyes Exp $
3-
* $Revision: 1.3 $
4-
* $Date: 2002/08/03 23:45:09 $
2+
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/GnuParser.java,v 1.4 2002/08/04 23:04:52 jkeyes Exp $
3+
* $Revision: 1.4 $
4+
* $Date: 2002/08/04 23:04:52 $
55
*
66
* ====================================================================
77
*
@@ -198,9 +198,26 @@ public void processArgs( Option opt, ListIterator iter )
198198
}
199199
// its a value
200200
else {
201-
if( !opt.addValue( var ) ) {
201+
char sep = opt.getValueSeparator();
202+
203+
if( sep > 0 ) {
204+
int findex;
205+
while( ( findex = var.indexOf( sep ) ) != -1 ) {
206+
String val = var.substring( 0, findex );
207+
var = var.substring( findex + 1);
208+
if( !opt.addValue( val ) ) {
209+
iter.previous();
210+
return;
211+
}
212+
}
213+
if( !opt.addValue( var ) ) {
214+
iter.previous();
215+
return;
216+
};
217+
}
218+
else if( !opt.addValue( var ) ) {
202219
iter.previous();
203-
break;
220+
return;
204221
}
205222
}
206223
}
@@ -223,7 +240,25 @@ private void processOption( String arg, ListIterator iter )
223240
if( specialOption != null && opt == null) {
224241
opt = specialOption;
225242
value = arg.substring( 2 );
226-
opt.addValue( value );
243+
char sep = opt.getValueSeparator();
244+
245+
if( sep > 0 ) {
246+
int findex;
247+
while( ( findex = value.indexOf( sep ) ) != -1 ) {
248+
String val = value.substring( 0, findex );
249+
value = value.substring( findex + 1);
250+
if( !opt.addValue( val ) ) {
251+
cmd.addArg( val );
252+
}
253+
}
254+
if( !opt.addValue( value ) ) {
255+
cmd.addArg( value );
256+
}
257+
}
258+
else {
259+
// add the argument value
260+
opt.addValue( value );
261+
}
227262
}
228263

229264
// if there is no option throw an UnrecognisedOptionException

src/java/org/apache/commons/cli/Option.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,16 @@ public void setArgs( int num ) {
307307
this.numberOfArgs = num;
308308
}
309309

310+
private char valuesep;
311+
312+
public void setValueSeparator( char sep ) {
313+
this.valuesep = sep;
314+
}
315+
316+
public char getValueSeparator() {
317+
return this.valuesep;
318+
}
319+
310320
/** <p>Returns the number of argument values this Option can take.</p>
311321
*
312322
* @return num the number of argument values
@@ -353,6 +363,7 @@ public String toString() {
353363
* @param value is a/the value of this Option
354364
*/
355365
public boolean addValue( String value ) {
366+
356367
switch( numberOfArgs ) {
357368
case UNINITIALIZED:
358369
return false;
@@ -376,6 +387,16 @@ public String getValue() {
376387
return this.values.size()==0 ? null : (String)this.values.get( 0 );
377388
}
378389

390+
/**
391+
* @return the specified value of this Option or
392+
* null if there are no values.
393+
*/
394+
public String getValue( int index )
395+
throws IndexOutOfBoundsException
396+
{
397+
return ( this.values.size()==0 ) ? null : (String)this.values.get( index );
398+
}
399+
379400
/**
380401
* @return the value/first value of this Option or the
381402
* <code>defaultValue</code> if there are no values.

src/java/org/apache/commons/cli/OptionBuilder.java

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/OptionBuilder.java,v 1.3 2002/08/03 23:45:09 jkeyes Exp $
3-
* $Revision: 1.3 $
4-
* $Date: 2002/08/03 23:45:09 $
2+
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/OptionBuilder.java,v 1.4 2002/08/04 23:04:52 jkeyes Exp $
3+
* $Revision: 1.4 $
4+
* $Date: 2002/08/04 23:04:52 $
55
*
66
* ====================================================================
77
*
@@ -83,6 +83,8 @@ public class OptionBuilder {
8383
/** option type */
8484
private static Object type;
8585

86+
private static char valuesep;
87+
8688
/** option builder instance */
8789
private static OptionBuilder instance = new OptionBuilder();
8890

@@ -144,6 +146,48 @@ public static OptionBuilder isRequired( ) {
144146
return instance;
145147
}
146148

149+
/**
150+
* <p>The next Option created uses <code>sep</code> as a means to
151+
* separate argument values.</p>
152+
*
153+
* <b>Example:</b>
154+
* <pre>
155+
* Option opt = OptionBuilder.withValueSeparator( ':' )
156+
* .create( 'D' );
157+
*
158+
* CommandLine line = parser.parse( args );
159+
* String propertyName = opt.getValue( 0 );
160+
* String propertyValue = opt.getValue( 1 );
161+
* </pre>
162+
*
163+
* @return the OptionBuilder instance
164+
*/
165+
public static OptionBuilder withValueSeparator( char sep ) {
166+
instance.valuesep = sep;
167+
return instance;
168+
}
169+
170+
/**
171+
* <p>The next Option created uses '<code>=</code>' as a means to
172+
* separate argument values.</p>
173+
*
174+
* <b>Example:</b>
175+
* <pre>
176+
* Option opt = OptionBuilder.withValueSeparator( )
177+
* .create( 'D' );
178+
*
179+
* CommandLine line = parser.parse( args );
180+
* String propertyName = opt.getValue( 0 );
181+
* String propertyValue = opt.getValue( 1 );
182+
* </pre>
183+
*
184+
* @return the OptionBuilder instance
185+
*/
186+
public static OptionBuilder withValueSeparator( ) {
187+
instance.valuesep = '=';
188+
return instance;
189+
}
190+
147191
/**
148192
* <p>The next Option created will be required if <code>required</code>
149193
* is true.</p>
@@ -237,7 +281,7 @@ public static Option create( String opt )
237281
option.setRequired( required );
238282
option.setArgs( numberOfArgs );
239283
option.setType( type );
240-
284+
option.setValueSeparator( valuesep );
241285
// reset the OptionBuilder properties
242286
instance.reset();
243287

src/java/org/apache/commons/cli/PosixParser.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/PosixParser.java,v 1.4 2002/08/03 23:45:09 jkeyes Exp $
3-
* $Revision: 1.4 $
4-
* $Date: 2002/08/03 23:45:09 $
2+
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/PosixParser.java,v 1.5 2002/08/04 23:04:52 jkeyes Exp $
3+
* $Revision: 1.5 $
4+
* $Date: 2002/08/04 23:04:52 $
55
*
66
* ====================================================================
77
*
@@ -181,8 +181,26 @@ else if ( token.length() == 2 ) {
181181
throw new MissingArgumentException( "Missing argument value for " + opt.getOpt() );
182182
}
183183

184-
// add the argument value
185-
opt.addValue( token.substring(i+1) );
184+
String var = token.substring(i+1);
185+
char sep = opt.getValueSeparator();
186+
187+
if( sep > 0 ) {
188+
int findex;
189+
while( ( findex = var.indexOf( sep ) ) != -1 ) {
190+
String val = var.substring( 0, findex );
191+
var = var.substring( findex + 1);
192+
if( !opt.addValue( val ) ) {
193+
cmd.addArg( val );
194+
}
195+
}
196+
if( !opt.addValue( var ) ) {
197+
cmd.addArg( var );
198+
};
199+
}
200+
else {
201+
// add the argument value
202+
opt.addValue( token.substring(i+1) );
203+
}
186204

187205
// set the option
188206
cmd.setOpt( opt );
@@ -289,9 +307,26 @@ public void processArgs( Option opt, ListIterator iter )
289307
}
290308
// its a value
291309
else {
292-
if( !opt.addValue( var ) ) {
310+
char sep = opt.getValueSeparator();
311+
312+
if( sep > 0 ) {
313+
int findex;
314+
while( ( findex = var.indexOf( sep ) ) != -1 ) {
315+
String val = var.substring( 0, findex );
316+
var = var.substring( findex + 1);
317+
if( !opt.addValue( val ) ) {
318+
iter.previous();
319+
return;
320+
}
321+
}
322+
if( !opt.addValue( var ) ) {
323+
iter.previous();
324+
return;
325+
};
326+
}
327+
else if( !opt.addValue( var ) ) {
293328
iter.previous();
294-
break;
329+
return;
295330
}
296331
}
297332
}

src/test/org/apache/commons/cli/ApplicationTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public void testAnt() {
4949
options.addOption( "listener", true, "add an instance of a class as a project listener" );
5050
options.addOption( "buildfile", true, "use given buildfile" );
5151
options.addOption( OptionBuilder.withDescription( "use value for given property" )
52-
.hasArg()
5352
.hasArgs()
5453
.create( 'D' ) );
5554
//, null, true, , false, true );

src/test/org/apache/commons/cli/PatternOptionBuilderTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,27 @@ public void testSimplePattern()
4747

4848
CommandLineParser parser = CommandLineParserFactory.newParser();
4949
CommandLine line = parser.parse(options,args);
50+
51+
// tests the char methods of CommandLine that delegate to
52+
// the String methods
5053
assertEquals("flag a", "foo", line.getOptionValue("a"));
54+
assertEquals("flag a", "foo", line.getOptionValue('a'));
5155
assertEquals("string flag a", "foo", line.getOptionObject("a"));
56+
assertEquals("string flag a", "foo", line.getOptionObject('a'));
5257
assertEquals("object flag b", new java.util.Vector(), line.getOptionObject("b"));
58+
assertEquals("object flag b", new java.util.Vector(), line.getOptionObject('b'));
5359
assertEquals("boolean true flag c", true, line.hasOption("c"));
60+
assertEquals("boolean true flag c", true, line.hasOption('c'));
5461
assertEquals("boolean false flag d", false, line.hasOption("d"));
62+
assertEquals("boolean false flag d", false, line.hasOption('d'));
5563
assertEquals("file flag e", new java.io.File("build.xml"), line.getOptionObject("e"));
64+
assertEquals("file flag e", new java.io.File("build.xml"), line.getOptionObject('e'));
5665
assertEquals("class flag f", java.util.Calendar.class, line.getOptionObject("f"));
66+
assertEquals("class flag f", java.util.Calendar.class, line.getOptionObject('f'));
5767
assertEquals("number flag n", new Float(4.5), line.getOptionObject("n"));
68+
assertEquals("number flag n", new Float(4.5), line.getOptionObject('n'));
5869
assertEquals("url flag t", new java.net.URL("http://jakarta.apache.org/"), line.getOptionObject("t"));
70+
assertEquals("url flag t", new java.net.URL("http://jakarta.apache.org/"), line.getOptionObject('t'));
5971
/// DATES NOT SUPPORTED YET.
6072
// assertEquals("number flag t", new java.util.Date(1023400137276L), line.getOptionObject('z'));
6173
// input is: "Thu Jun 06 17:48:57 EDT 2002"

0 commit comments

Comments
 (0)