Skip to content

Commit 16b91ad

Browse files
author
John Keyes
committed
its now possible to specify the number of argument values an option can have, refactored parsers argument value handling, refactored argument handling in Option, added getOptions method on CommandLine to return an array of the processed Options
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@129794 13f79535-47bb-0310-9956-ffa450edef68
1 parent 5aa47b7 commit 16b91ad

9 files changed

Lines changed: 222 additions & 105 deletions

File tree

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161

6262
package org.apache.commons.cli;
6363

64+
import java.util.Collection;
6465
import java.util.HashMap;
6566
import java.util.Iterator;
6667
import java.util.List;
@@ -88,9 +89,12 @@ public class CommandLine {
8889
/** the unrecognised options/arguments */
8990
private List args = new LinkedList();
9091

91-
/** the recognised options/arguments */
92+
/** the recognised options */
9293
private Map options = new HashMap();
9394

95+
/** the processed options */
96+
private Option[] optionsArray;
97+
9498
/**
9599
* <p>Creates a command line.</p>
96100
*/
@@ -208,4 +212,19 @@ public Iterator iterator( ) {
208212
return options.values().iterator();
209213
}
210214

215+
/**
216+
* <p>Returns an array of the processed {@link Option}s.</p>
217+
*
218+
* @return an array of the processed {@link Option}s.
219+
*/
220+
public Option[] getOptions( ) {
221+
Collection processed = options.values();
222+
223+
// reinitialise array
224+
optionsArray = new Option[ processed.size() ];
225+
226+
// return the array
227+
return (Option[]) processed.toArray( optionsArray );
228+
}
229+
211230
}

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

Lines changed: 17 additions & 18 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.2 2002/07/04 22:32:12 jkeyes Exp $
3-
* $Revision: 1.2 $
4-
* $Date: 2002/07/04 22:32:12 $
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 $
55
*
66
* ====================================================================
77
*
@@ -126,7 +126,6 @@ public CommandLine parse( Options opts, String[] arguments, boolean stopAtNonOpt
126126

127127
while ( iter.hasNext() ) {
128128
token = (String) iter.next();
129-
130129
if ( token.equals("--") ) {
131130
eatTheRest = true;
132131
}
@@ -181,20 +180,28 @@ else if ( token.startsWith("-") ) {
181180
* @param opt the specified option
182181
* @param iter the iterator over the command line tokens
183182
*/
184-
public void processMultipleArgs( Option opt, ListIterator iter ) {
183+
public void processArgs( Option opt, ListIterator iter )
184+
throws ParseException
185+
{
186+
if( !iter.hasNext() ) {
187+
throw new MissingArgumentException( "no argument for:" + opt.getOpt() );
188+
}
185189
// loop until an option is found
186190
while( iter.hasNext() ) {
187191
String var = (String)iter.next();
188192

189193
// its an option
190-
if( var.startsWith( "-" ) ) {
194+
if( !var.equals( "-" ) && var.startsWith( "-" ) ) {
191195
// set the iterator pointer back a position
192196
iter.previous();
193197
break;
194198
}
195199
// its a value
196200
else {
197-
opt.addValue( var );
201+
if( !opt.addValue( var ) ) {
202+
iter.previous();
203+
break;
204+
}
198205
}
199206
}
200207
}
@@ -216,6 +223,7 @@ private void processOption( String arg, ListIterator iter )
216223
if( specialOption != null && opt == null) {
217224
opt = specialOption;
218225
value = arg.substring( 2 );
226+
opt.addValue( value );
219227
}
220228

221229
// if there is no option throw an UnrecognisedOptionException
@@ -236,17 +244,8 @@ private void processOption( String arg, ListIterator iter )
236244
}
237245

238246
// if the option takes an argument value
239-
if ( opt.hasArg() ) {
240-
try {
241-
value = (value != null) ? value : (String)iter.next();
242-
}
243-
catch( java.util.NoSuchElementException exp ) {
244-
throw new MissingArgumentException( "no argument for:" + arg );
245-
}
246-
opt.addValue( value );
247-
if (opt.hasMultipleArgs() ) {
248-
processMultipleArgs( opt, iter );
249-
}
247+
if ( opt.hasArg() ) {
248+
processArgs( opt, iter );
250249
}
251250

252251
// set the option on the command line

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

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,24 @@ public class Option {
106106
/** required specifies whether this option is required to be present */
107107
private boolean required = false;
108108

109-
/** multipleArgs specifies whether this option has multiple argument values */
110-
private boolean multipleArgs = false;
109+
/**
110+
* numberOfArgs specifies the number of argument values this option
111+
* can have
112+
*/
113+
private int numberOfArgs = UNINITIALIZED;
114+
115+
/** number of arguments constants */
116+
public final static int UNINITIALIZED = -1;
117+
public final static int UNLIMITED_VALUES = -2;
111118

112119
/** the type of this Option */
113120
private Object type = null;
114121

115122
/** ?? **/
116123
private ArrayList values = new ArrayList();
117124

125+
/** option char (only valid for single character options) */
126+
private char id;
118127

119128
private void validateOption( String opt )
120129
throws IllegalArgumentException
@@ -127,6 +136,7 @@ else if( opt.length() == 1 ) {
127136
throw new IllegalArgumentException( "illegal option value '"
128137
+ opt.charAt( 0 ) + "'" );
129138
}
139+
id = opt.charAt( 0 );
130140
}
131141
else {
132142
char[] chars = opt.toCharArray();
@@ -153,6 +163,24 @@ private boolean isValidChar( char c )
153163
}
154164
return true;
155165
}
166+
167+
public int getId( ) {
168+
return id;
169+
}
170+
171+
/**
172+
* Creates an Option using the specified parameters.
173+
*
174+
* @param opt short representation of the option
175+
* @param hasArg specifies whether the Option takes an argument or not
176+
* @param description describes the function of the option
177+
*/
178+
public Option(String opt, String description)
179+
throws IllegalArgumentException
180+
{
181+
this(opt, null, false, description);
182+
}
183+
156184
/**
157185
* Creates an Option using the specified parameters.
158186
*
@@ -181,6 +209,10 @@ public Option(String opt, String longOpt, boolean hasArg, String description)
181209

182210
this.opt = opt;
183211
this.longOpt = longOpt;
212+
213+
if( hasArg ) {
214+
this.numberOfArgs = 1;
215+
}
184216
this.hasArg = hasArg;
185217
this.description = description;
186218
}
@@ -236,7 +268,7 @@ public boolean hasLongOpt() {
236268
* @return boolean flag indicating if an argument is required
237269
*/
238270
public boolean hasArg() {
239-
return this.hasArg;
271+
return this.numberOfArgs > 0 || numberOfArgs == UNLIMITED_VALUES;
240272
}
241273

242274
/** <p>Retrieve the self-documenting description of this Option</p>
@@ -259,16 +291,28 @@ public void setRequired( boolean required ) {
259291
this.required = required;
260292
}
261293

262-
/** <p>Query to see if this Option can take multiple values</p>
294+
/** <p>Query to see if this Option can take many values</p>
263295
*
264296
* @return boolean flag indicating if multiple values are allowed
265297
*/
266-
public boolean hasMultipleArgs() {
267-
return this.multipleArgs;
298+
public boolean hasArgs() {
299+
return ( this.numberOfArgs > 1 || this.numberOfArgs == UNLIMITED_VALUES );
268300
}
269301

270-
public void setMultipleArgs( boolean multipleArgs ) {
271-
this.multipleArgs = multipleArgs;
302+
/** <p>Sets the number of argument values this Option can take.</p>
303+
*
304+
* @param num the number of argument values
305+
*/
306+
public void setArgs( int num ) {
307+
this.numberOfArgs = num;
308+
}
309+
310+
/** <p>Returns the number of argument values this Option can take.</p>
311+
*
312+
* @return num the number of argument values
313+
*/
314+
public int getArgs( ) {
315+
return this.numberOfArgs;
272316
}
273317

274318
/** <p>Dump state, suitable for debugging.</p>
@@ -308,8 +352,20 @@ public String toString() {
308352
*
309353
* @param value is a/the value of this Option
310354
*/
311-
public void addValue( String value ) {
312-
this.values.add( value );
355+
public boolean addValue( String value ) {
356+
switch( numberOfArgs ) {
357+
case UNINITIALIZED:
358+
return false;
359+
case UNLIMITED_VALUES:
360+
this.values.add( value );
361+
return true;
362+
default:
363+
if( values.size() > numberOfArgs-1 ) {
364+
return false;
365+
}
366+
this.values.add( value );
367+
return true;
368+
}
313369
}
314370

315371
/**

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

Lines changed: 25 additions & 16 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.2 2002/07/30 23:06:21 jkeyes Exp $
3-
* $Revision: 1.2 $
4-
* $Date: 2002/07/30 23:06:21 $
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 $
55
*
66
* ====================================================================
77
*
@@ -76,12 +76,10 @@ public class OptionBuilder {
7676
private static String longopt;
7777
/** option description */
7878
private static String description;
79-
/** has an argument? */
80-
private static boolean arg;
8179
/** is required? */
8280
private static boolean required;
83-
/** has multiple arguments */
84-
private static boolean multipleArgs;
81+
/** the number of arguments */
82+
private static int numberOfArgs = Option.UNINITIALIZED;
8583
/** option type */
8684
private static Object type;
8785

@@ -99,9 +97,8 @@ private static void reset() {
9997
description = null;
10098
longopt = null;
10199
type = null;
102-
arg = false;
103100
required = false;
104-
multipleArgs = false;
101+
numberOfArgs = Option.UNINITIALIZED;
105102
}
106103

107104
/**
@@ -121,7 +118,7 @@ public static OptionBuilder withLongOpt( String longopt ) {
121118
* @return the OptionBuilder instance
122119
*/
123120
public static OptionBuilder hasArg( ) {
124-
instance.arg = true;
121+
instance.numberOfArgs = 1;
125122
return instance;
126123
}
127124

@@ -133,7 +130,7 @@ public static OptionBuilder hasArg( ) {
133130
* @return the OptionBuilder instance
134131
*/
135132
public static OptionBuilder hasArg( boolean hasArg ) {
136-
instance.arg = hasArg;
133+
instance.numberOfArgs = ( hasArg == true ) ? 1 : Option.UNINITIALIZED;
137134
return instance;
138135
}
139136

@@ -160,12 +157,24 @@ public static OptionBuilder isRequired( boolean required ) {
160157
}
161158

162159
/**
163-
* <p>The next Option created can have multiple argument values.</p>
160+
* <p>The next Option created can have unlimited argument values.</p>
164161
*
165162
* @return the OptionBuilder instance
166163
*/
167-
public static OptionBuilder hasMultipleArgs( ) {
168-
instance.multipleArgs = true;
164+
public static OptionBuilder hasArgs( ) {
165+
instance.numberOfArgs = Option.UNLIMITED_VALUES;
166+
return instance;
167+
}
168+
169+
/**
170+
* <p>The next Option created can have <code>num</code>
171+
* argument values.</p>
172+
*
173+
* @param num the number of args that the option can have
174+
* @return the OptionBuilder instance
175+
*/
176+
public static OptionBuilder hasArgs( int num ) {
177+
instance.numberOfArgs = num;
169178
return instance;
170179
}
171180

@@ -221,12 +230,12 @@ public static Option create( String opt )
221230
throws IllegalArgumentException
222231
{
223232
// create the option
224-
Option option = new Option( opt, arg, description );
233+
Option option = new Option( opt, description );
225234

226235
// set the option properties
227236
option.setLongOpt( longopt );
228237
option.setRequired( required );
229-
option.setMultipleArgs( multipleArgs );
238+
option.setArgs( numberOfArgs );
230239
option.setType( type );
231240

232241
// reset the OptionBuilder properties

0 commit comments

Comments
 (0)