Skip to content

Commit 5aa47b7

Browse files
author
John Keyes
committed
added iterator method to CommandLine for the processed Options, refactored CommandLine
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@129793 13f79535-47bb-0310-9956-ffa450edef68
1 parent fbc0bd0 commit 5aa47b7

2 files changed

Lines changed: 34 additions & 40 deletions

File tree

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

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@
6161

6262
package org.apache.commons.cli;
6363

64+
import java.util.HashMap;
65+
import java.util.Iterator;
6466
import java.util.List;
6567
import java.util.LinkedList;
6668
import java.util.Map;
67-
import java.util.HashMap;
6869

6970
/** <p>Represents list of arguments parsed against
7071
* a {@link Options} descriptor.<p>
@@ -90,9 +91,6 @@ public class CommandLine {
9091
/** the recognised options/arguments */
9192
private Map options = new HashMap();
9293

93-
/** the option types */
94-
private Map types = new HashMap();
95-
9694
/**
9795
* <p>Creates a command line.</p>
9896
*/
@@ -112,9 +110,9 @@ public boolean hasOption(String opt) {
112110
* @param opt the name of the option
113111
* @return the type of opt
114112
*/
115-
public Object getOptionObject(String opt) {
113+
public Object getOptionObject( String opt ) {
116114
String res = getOptionValue( opt );
117-
Object type = types.get( opt );
115+
Object type = ((Option)options.get( opt )).getType();
118116
return res == null ? null : TypeHandler.createValue(res, type);
119117
}
120118

@@ -123,19 +121,17 @@ public Object getOptionObject(String opt) {
123121
* @param opt the name of the option
124122
* @return Value of the argument if option is set, and has an argument, else null.
125123
*/
126-
public String getOptionValue(String opt) {
127-
String[] result = (String[])options.get( opt );
128-
return result == null ? null : result[0];
124+
public String getOptionValue( String opt ) {
125+
return (String)((Option)options.get( opt )).getValue();
129126
}
130127

131128
/** <p>Retrieves the array of values, if any, of an option.</p>
132129
*
133130
* @param opt Single-character name of the option
134131
* @return An array of values if the option is set, and has an argument, else null.
135132
*/
136-
public String[] getOptionValues(String opt) {
137-
String[] result = (String[])options.get( opt );
138-
return result == null ? null : result;
133+
public String[] getOptionValues( String opt ) {
134+
return (String[])((Option)options.get( opt )).getValues();
139135
}
140136

141137
/** <p>Retrieve the argument, if any, of an option.</p>
@@ -144,9 +140,9 @@ public String[] getOptionValues(String opt) {
144140
* @param defaultValue is the default value to be returned if the option is not specified
145141
* @return Value of the argument if option is set, and has an argument, else null.
146142
*/
147-
public String getOptionValue(String opt, String defaultValue) {
148-
String answer = getOptionValue(opt);
149-
return (answer != null) ? answer : defaultValue;
143+
public String getOptionValue( String opt, String defaultValue ) {
144+
String answer = getOptionValue( opt );
145+
return ( answer != null ) ? answer : defaultValue;
150146
}
151147

152148
/** <p>Retrieve any left-over non-recognized options and arguments</p>
@@ -191,36 +187,25 @@ public String toString() {
191187
void addArg(String arg) {
192188
args.add( arg );
193189
}
194-
195-
/**
196-
* <p>Add an option that does not have any value to the
197-
* command line.</p>
198-
*
199-
* @param opt the processed option
200-
*/
201-
void setOpt(String opt) {
202-
options.put( opt, null );
203-
}
204-
190+
205191
/**
206-
* <p>Add an option with the specified value to the
207-
* command line.</p>
192+
* <p>Add an option to the command line. The values of
193+
* the option are stored.</p>
208194
*
209195
* @param opt the processed option
210-
* @param value the value of the option
211196
*/
212-
void setOpt(String opt, String value) {
213-
options.put( opt, value );
197+
void setOpt( Option opt ) {
198+
options.put( opt.getOpt(), opt );
214199
}
215-
200+
216201
/**
217-
* <p>Add an option to the command line. The values of
218-
* the option are stored.</p>
202+
* <p>Returns an iterator over the Option members of CommandLine.</p>
219203
*
220-
* @param opt the processed option
204+
* @return an <code>Iterator</code> over the processed {@link Option}
205+
* members of this {@link CommandLine}
221206
*/
222-
void setOpt(Option opt) {
223-
options.put( opt.getOpt(), opt.getValues() );
224-
types.put( opt.getOpt(), opt.getType() );
207+
public Iterator iterator( ) {
208+
return options.values().iterator();
225209
}
210+
226211
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,22 @@ public void addValue( String value ) {
313313
}
314314

315315
/**
316-
* @return the value/first value of this Option or null if there are no
317-
* values
316+
* @return the value/first value of this Option or
317+
* null if there are no values.
318318
*/
319319
public String getValue() {
320320
return this.values.size()==0 ? null : (String)this.values.get( 0 );
321321
}
322322

323+
/**
324+
* @return the value/first value of this Option or the
325+
* <code>defaultValue</code> if there are no values.
326+
*/
327+
public String getValue( String defaultValue ) {
328+
String value = getValue( );
329+
return ( value != null ) ? value : defaultValue;
330+
}
331+
323332
/**
324333
* @return the values of this Option or null if there are no
325334
* values

0 commit comments

Comments
 (0)