Skip to content

Commit 4fbf597

Browse files
author
John Keyes
committed
bug no. 11456 - optional argument values implemented
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@129801 13f79535-47bb-0310-9956-ffa450edef68
1 parent 83b7ceb commit 4fbf597

5 files changed

Lines changed: 89 additions & 59 deletions

File tree

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

Lines changed: 5 additions & 5 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.5 2002/08/14 22:27:39 jkeyes Exp $
3-
* $Revision: 1.5 $
4-
* $Date: 2002/08/14 22:27:39 $
2+
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/GnuParser.java,v 1.6 2002/08/15 22:05:18 jkeyes Exp $
3+
* $Revision: 1.6 $
4+
* $Date: 2002/08/15 22:05:18 $
55
*
66
* ====================================================================
77
*
@@ -184,7 +184,7 @@ else if ( token.startsWith("-") ) {
184184
public void processArgs( Option opt, ListIterator iter )
185185
throws ParseException
186186
{
187-
if( !iter.hasNext() ) {
187+
if( !iter.hasNext() && !opt.hasOptionalArg() ) {
188188
throw new MissingArgumentException( "no argument for:" + opt.getOpt() );
189189
}
190190
// loop until an option is found
@@ -261,7 +261,7 @@ private void processOption( String arg, ListIterator iter )
261261
}
262262

263263
// if the option takes an argument value
264-
if ( opt.hasArg() ) {
264+
if ( opt.hasArg() ) {
265265
processArgs( opt, iter );
266266
}
267267

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ public class Option implements Cloneable {
106106
/** required specifies whether this option is required to be present */
107107
private boolean required = false;
108108

109+
private boolean optionalArg = false;
110+
109111
/**
110112
* numberOfArgs specifies the number of argument values this option
111113
* can have
@@ -254,6 +256,26 @@ public String getLongOpt() {
254256
public void setLongOpt( String longOpt ) {
255257
this.longOpt = longOpt;
256258
}
259+
260+
/**
261+
* Sets whether this Option can have an optional argument.
262+
*
263+
* @param optionalArg specifies whether the Option can have
264+
* an optional argument.
265+
*/
266+
public void setOptionalArg( boolean optionalArg ) {
267+
if( optionalArg ) {
268+
this.numberOfArgs = 1;
269+
}
270+
this.optionalArg = optionalArg;
271+
}
272+
273+
/**
274+
* @return whether this Option can have an optional argument
275+
*/
276+
public boolean hasOptionalArg( ) {
277+
return this.optionalArg;
278+
}
257279

258280
/** <p>Query to see if this Option has a long name</p>
259281
*
@@ -438,6 +460,7 @@ public java.util.List getValuesList() {
438460
public Object clone() {
439461
Option option = new Option( getOpt(), getDescription() );
440462
option.setArgs( getArgs() );
463+
option.setOptionalArg( hasOptionalArg() );
441464
option.setRequired( isRequired() );
442465
option.setLongOpt( getLongOpt() );
443466
option.setType( getType() );

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

Lines changed: 15 additions & 3 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.4 2002/08/04 23:04:52 jkeyes Exp $
3-
* $Revision: 1.4 $
4-
* $Date: 2002/08/04 23:04:52 $
2+
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/OptionBuilder.java,v 1.5 2002/08/15 22:05:18 jkeyes Exp $
3+
* $Revision: 1.5 $
4+
* $Date: 2002/08/15 22:05:18 $
55
*
66
* ====================================================================
77
*
@@ -83,6 +83,7 @@ public class OptionBuilder {
8383
/** option type */
8484
private static Object type;
8585

86+
private static boolean optionalArg;
8687
private static char valuesep;
8788

8889
/** option builder instance */
@@ -222,6 +223,16 @@ public static OptionBuilder hasArgs( int num ) {
222223
return instance;
223224
}
224225

226+
/**
227+
* <p>The next Option can have an optional argument.</p>
228+
*
229+
* @return the OptionBuilder instance
230+
*/
231+
public static OptionBuilder hasOptionalArg( ) {
232+
instance.optionalArg = true;
233+
return instance;
234+
}
235+
225236
/**
226237
* <p>The next Option created will have a value that will be an instance
227238
* of <code>type</code>.</p>
@@ -279,6 +290,7 @@ public static Option create( String opt )
279290
// set the option properties
280291
option.setLongOpt( longopt );
281292
option.setRequired( required );
293+
option.setOptionalArg( optionalArg );
282294
option.setArgs( numberOfArgs );
283295
option.setType( type );
284296
option.setValueSeparator( valuesep );

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

Lines changed: 6 additions & 50 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.6 2002/08/14 22:27:39 jkeyes Exp $
3-
* $Revision: 1.6 $
4-
* $Date: 2002/08/14 22:27:39 $
2+
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//cli/src/java/org/apache/commons/cli/PosixParser.java,v 1.7 2002/08/15 22:05:18 jkeyes Exp $
3+
* $Revision: 1.7 $
4+
* $Date: 2002/08/15 22:05:18 $
55
*
66
* ====================================================================
77
*
@@ -181,33 +181,12 @@ else if ( token.length() == 2 ) {
181181
// to be the argument value
182182

183183
// if there is no argument value
184-
if( token.substring(i+1).length() == 0 ) {
184+
if( token.substring(i+1).length() == 0 && !opt.hasOptionalArg() ) {
185185
throw new MissingArgumentException( "Missing argument value for " + opt.getOpt() );
186186
}
187-
188-
/*
189-
String var = token.substring(i+1);
190-
char sep = opt.getValueSeparator();
191-
192-
if( sep > 0 ) {
193-
int findex;
194-
while( ( findex = var.indexOf( sep ) ) != -1 ) {
195-
String val = var.substring( 0, findex );
196-
var = var.substring( findex + 1);
197-
if( !opt.addValue( val ) ) {
198-
cmd.addArg( val );
199-
}
200-
}
201-
if( !opt.addValue( var ) ) {
202-
cmd.addArg( var );
203-
};
204-
}
205187
else {
206-
// add the argument value
207188
opt.addValue( token.substring(i+1) );
208189
}
209-
*/
210-
opt.addValue( token.substring(i+1) );
211190

212191
// set the option
213192
cmd.setOpt( opt );
@@ -261,7 +240,7 @@ private void processOption( String arg, ListIterator iter )
261240
throws ParseException
262241
{
263242
// get the option represented by arg
264-
Option opt = null;//(Option) options.getOption( arg );
243+
Option opt = null;
265244

266245
boolean hasOption = options.hasOption( arg );
267246

@@ -304,7 +283,7 @@ private void processOption( String arg, ListIterator iter )
304283
public void processArgs( Option opt, ListIterator iter )
305284
throws ParseException
306285
{
307-
if( !iter.hasNext() ) {
286+
if( !iter.hasNext() && !opt.hasOptionalArg() ) {
308287
throw new MissingArgumentException( "no argument for:" + opt.getOpt() );
309288
}
310289
// loop until an option is found
@@ -319,29 +298,6 @@ public void processArgs( Option opt, ListIterator iter )
319298
}
320299
// its a value
321300
else {
322-
/*
323-
char sep = opt.getValueSeparator();
324-
325-
if( sep > 0 ) {
326-
int findex;
327-
while( ( findex = var.indexOf( sep ) ) != -1 ) {
328-
String val = var.substring( 0, findex );
329-
var = var.substring( findex + 1);
330-
if( !opt.addValue( val ) ) {
331-
iter.previous();
332-
return;
333-
}
334-
}
335-
if( !opt.addValue( var ) ) {
336-
iter.previous();
337-
return;
338-
};
339-
}
340-
else if( !opt.addValue( var ) ) {
341-
iter.previous();
342-
return;
343-
}
344-
*/
345301
if( !opt.addValue( var ) ) {
346302
iter.previous();
347303
break;

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* version 1.1, a copy of which has been included with this distribution in
66
* the LICENSE file.
77
*
8-
* $Id: BugsTest.java,v 1.1 2002/08/15 20:31:23 jkeyes Exp $
8+
* $Id: BugsTest.java,v 1.2 2002/08/15 22:05:19 jkeyes Exp $
99
*/
1010

1111
package org.apache.commons.cli;
@@ -60,7 +60,46 @@ public void test11680()
6060
catch( ParseException exp ) {
6161
fail( "Unexpected Exception: " + exp.getMessage() );
6262
}
63+
}
64+
65+
public void test11456()
66+
{
67+
// Posix
68+
Options options = new Options();
69+
options.addOption( OptionBuilder.hasOptionalArg()
70+
.create( 'a' ) );
71+
options.addOption( OptionBuilder.hasArg()
72+
.create( 'b' ) );
73+
String[] args = new String[] { "-a", "-bvalue" };
74+
75+
CommandLineParser parser = CommandLineParserFactory.newParser();
76+
77+
try {
78+
CommandLine cmd = parser.parse( options, args );
79+
assertEquals( cmd.getOptionValue( 'b' ), "value" );
80+
}
81+
catch( ParseException exp ) {
82+
fail( "Unexpected Exception: " + exp.getMessage() );
83+
}
84+
85+
// GNU
86+
options = new Options();
87+
options.addOption( OptionBuilder.hasOptionalArg()
88+
.create( 'a' ) );
89+
options.addOption( OptionBuilder.hasArg()
90+
.create( 'b' ) );
91+
args = new String[] { "-a", "-b", "value" };
6392

93+
parser = CommandLineParserFactory.newParser( "org.apache.commons.cli.GnuParser" );
94+
95+
try {
96+
CommandLine cmd = parser.parse( options, args );
97+
assertEquals( cmd.getOptionValue( 'b' ), "value" );
98+
}
99+
catch( ParseException exp ) {
100+
fail( "Unexpected Exception: " + exp.getMessage() );
101+
}
64102

65103
}
104+
66105
}

0 commit comments

Comments
 (0)