6161
6262package org .apache .commons .cli ;
6363
64+ import java .util .HashMap ;
65+ import java .util .Iterator ;
6466import java .util .List ;
6567import java .util .LinkedList ;
6668import 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}
0 commit comments