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/01 00:24:29 UTC
cvs commit: jakarta-commons/cli/src/java/org/apache/commons/cli Option.java CommandLine.java
jkeyes 2002/07/31 15:24:29
Modified: cli/src/java/org/apache/commons/cli Option.java
CommandLine.java
Log:
added iterator method to CommandLine for the processed Options, refactored CommandLine
Revision Changes Path
1.6 +11 -2 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.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- Option.java 30 Jul 2002 23:06:21 -0000 1.5
+++ Option.java 31 Jul 2002 22:24:29 -0000 1.6
@@ -313,11 +313,20 @@
}
/**
- * @return the value/first value of this Option or null if there are no
- * values
+ * @return the value/first value of this Option or
+ * null if there are no values.
*/
public String getValue() {
return this.values.size()==0 ? null : (String)this.values.get( 0 );
+ }
+
+ /**
+ * @return the value/first value of this Option or the
+ * <code>defaultValue</code> if there are no values.
+ */
+ public String getValue( String defaultValue ) {
+ String value = getValue( );
+ return ( value != null ) ? value : defaultValue;
}
/**
1.4 +23 -38 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.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- CommandLine.java 9 Jul 2002 23:12:15 -0000 1.3
+++ CommandLine.java 31 Jul 2002 22:24:29 -0000 1.4
@@ -61,10 +61,11 @@
package org.apache.commons.cli;
+import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
import java.util.Map;
-import java.util.HashMap;
/** <p>Represents list of arguments parsed against
* a {@link Options} descriptor.<p>
@@ -90,9 +91,6 @@
/** the recognised options/arguments */
private Map options = new HashMap();
- /** the option types */
- private Map types = new HashMap();
-
/**
* <p>Creates a command line.</p>
*/
@@ -112,9 +110,9 @@
* @param opt the name of the option
* @return the type of opt
*/
- public Object getOptionObject(String opt) {
+ public Object getOptionObject( String opt ) {
String res = getOptionValue( opt );
- Object type = types.get( opt );
+ Object type = ((Option)options.get( opt )).getType();
return res == null ? null : TypeHandler.createValue(res, type);
}
@@ -123,9 +121,8 @@
* @param opt the name of the option
* @return Value of the argument if option is set, and has an argument, else null.
*/
- public String getOptionValue(String opt) {
- String[] result = (String[])options.get( opt );
- return result == null ? null : result[0];
+ public String getOptionValue( String opt ) {
+ return (String)((Option)options.get( opt )).getValue();
}
/** <p>Retrieves the array of values, if any, of an option.</p>
@@ -133,9 +130,8 @@
* @param opt Single-character 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) {
- String[] result = (String[])options.get( opt );
- return result == null ? null : result;
+ public String[] getOptionValues( String opt ) {
+ return (String[])((Option)options.get( opt )).getValues();
}
/** <p>Retrieve the argument, if any, of an option.</p>
@@ -144,9 +140,9 @@
* @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(String opt, String defaultValue) {
- String answer = getOptionValue(opt);
- return (answer != null) ? answer : defaultValue;
+ public String getOptionValue( String opt, String defaultValue ) {
+ String answer = getOptionValue( opt );
+ return ( answer != null ) ? answer : defaultValue;
}
/** <p>Retrieve any left-over non-recognized options and arguments</p>
@@ -191,36 +187,25 @@
void addArg(String arg) {
args.add( arg );
}
-
- /**
- * <p>Add an option that does not have any value to the
- * command line.</p>
- *
- * @param opt the processed option
- */
- void setOpt(String opt) {
- options.put( opt, null );
- }
-
+
/**
- * <p>Add an option with the specified value to the
- * command line.</p>
+ * <p>Add an option to the command line. The values of
+ * the option are stored.</p>
*
* @param opt the processed option
- * @param value the value of the option
*/
- void setOpt(String opt, String value) {
- options.put( opt, value );
+ void setOpt( Option opt ) {
+ options.put( opt.getOpt(), opt );
}
-
+
/**
- * <p>Add an option to the command line. The values of
- * the option are stored.</p>
+ * <p>Returns an iterator over the Option members of CommandLine.</p>
*
- * @param opt the processed option
+ * @return an <code>Iterator</code> over the processed {@link Option}
+ * members of this {@link CommandLine}
*/
- void setOpt(Option opt) {
- options.put( opt.getOpt(), opt.getValues() );
- types.put( opt.getOpt(), opt.getType() );
+ public Iterator iterator( ) {
+ return options.values().iterator();
}
+
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>