6060 */
6161package org .apache .commons .cli ;
6262
63- import java .util .ArrayList ;
6463import java .util .Collection ;
6564import java .util .HashMap ;
6665import java .util .Iterator ;
67- import java .util .List ;
6866import java .util .LinkedList ;
67+ import java .util .List ;
6968import java .util .Map ;
7069
7170/**
8483 * @author John Keyes (john at integralsource.com)
8584 */
8685public class CommandLine {
87-
86+
8887 /** the unrecognised options/arguments */
89- private List args = new LinkedList ();
88+ private List args = new LinkedList ();
9089
9190 /** the processed options */
9291 private Map options = new HashMap ();
9392
9493 /** the option name map */
95- private Map names = new HashMap ();
94+ private Map names = new HashMap ();
9695
9796 /** Map of unique options for ease to get complete list of options */
9897 private Map hashcodeMap = new HashMap ();
@@ -103,17 +102,19 @@ public class CommandLine {
103102 /**
104103 * <p>Creates a command line.</p>
105104 */
106- CommandLine () {
105+ CommandLine ()
106+ {
107107 }
108-
108+
109109 /**
110110 * <p>Query to see if an option has been set.</p>
111111 *
112112 * @param opt Short name of the option
113113 * @return true if set, false if not
114114 */
115- public boolean hasOption (String opt ) {
116- return options .containsKey ( opt );
115+ public boolean hasOption (String opt )
116+ {
117+ return options .containsKey (opt );
117118 }
118119
119120 /**
@@ -122,8 +123,9 @@ public boolean hasOption(String opt) {
122123 * @param opt character name of the option
123124 * @return true if set, false if not
124125 */
125- public boolean hasOption ( char opt ) {
126- return hasOption ( String .valueOf ( opt ) );
126+ public boolean hasOption (char opt )
127+ {
128+ return hasOption (String .valueOf (opt ));
127129 }
128130
129131 /**
@@ -132,14 +134,18 @@ public boolean hasOption( char opt ) {
132134 * @param opt the name of the option
133135 * @return the type of this <code>Option</code>
134136 */
135- public Object getOptionObject ( String opt ) {
136- String res = getOptionValue ( opt );
137-
138- if ( !options .containsKey ( opt ) ) {
137+ public Object getOptionObject (String opt )
138+ {
139+ String res = getOptionValue (opt );
140+
141+ if (!options .containsKey (opt ))
142+ {
139143 return null ;
140144 }
141- Object type = ((Option )options .get (opt )).getType ();
142- return res == null ? null : TypeHandler .createValue (res , type );
145+
146+ Object type = ((Option ) options .get (opt )).getType ();
147+
148+ return (res == null ) ? null : TypeHandler .createValue (res , type );
143149 }
144150
145151 /**
@@ -148,8 +154,9 @@ public Object getOptionObject( String opt ) {
148154 * @param opt the name of the option
149155 * @return the type of opt
150156 */
151- public Object getOptionObject ( char opt ) {
152- return getOptionObject ( String .valueOf ( opt ) );
157+ public Object getOptionObject (char opt )
158+ {
159+ return getOptionObject (String .valueOf (opt ));
153160 }
154161
155162 /**
@@ -159,8 +166,10 @@ public Object getOptionObject( char opt ) {
159166 * @return Value of the argument if option is set, and has an argument,
160167 * otherwise null.
161168 */
162- public String getOptionValue ( String opt ) {
169+ public String getOptionValue (String opt )
170+ {
163171 String [] values = getOptionValues (opt );
172+
164173 return (values == null ) ? null : values [0 ];
165174 }
166175
@@ -171,8 +180,9 @@ public String getOptionValue( String opt ) {
171180 * @return Value of the argument if option is set, and has an argument,
172181 * otherwise null.
173182 */
174- public String getOptionValue ( char opt ) {
175- return getOptionValue ( String .valueOf ( opt ) );
183+ public String getOptionValue (char opt )
184+ {
185+ return getOptionValue (String .valueOf (opt ));
176186 }
177187
178188 /**
@@ -182,19 +192,24 @@ public String getOptionValue( char opt ) {
182192 * @return Values of the argument if option is set, and has an argument,
183193 * otherwise null.
184194 */
185- public String [] getOptionValues ( String opt ) {
195+ public String [] getOptionValues (String opt )
196+ {
186197 List values = new java .util .ArrayList ();
187198
188- opt = Util .stripLeadingHyphens ( opt );
199+ opt = Util .stripLeadingHyphens (opt );
189200
190201 String key = opt ;
191- if ( names .containsKey ( opt ) ) {
192- key = (String )names .get ( opt );
202+
203+ if (names .containsKey (opt ))
204+ {
205+ key = (String ) names .get (opt );
193206 }
194207
195- if ( options .containsKey ( key ) ) {
196- return ((Option )options .get (key )).getValues ();
208+ if (options .containsKey (key ))
209+ {
210+ return ((Option ) options .get (key )).getValues ();
197211 }
212+
198213 return null ;
199214 }
200215
@@ -205,72 +220,83 @@ public String[] getOptionValues( String opt ) {
205220 * @return Values of the argument if option is set, and has an argument,
206221 * otherwise null.
207222 */
208- public String [] getOptionValues ( char opt ) {
209- return getOptionValues ( String .valueOf ( opt ) );
223+ public String [] getOptionValues (char opt )
224+ {
225+ return getOptionValues (String .valueOf (opt ));
210226 }
211-
227+
212228 /**
213229 * <p>Retrieve the argument, if any, of an option.</p>
214230 *
215231 * @param opt name of the option
216- * @param defaultValue is the default value to be returned if the option is not specified
232+ * @param defaultValue is the default value to be returned if the option
233+ * is not specified
217234 * @return Value of the argument if option is set, and has an argument,
218235 * otherwise <code>defaultValue</code>.
219236 */
220- public String getOptionValue ( String opt , String defaultValue ) {
221- String answer = getOptionValue ( opt );
222- return ( answer != null ) ? answer : defaultValue ;
237+ public String getOptionValue (String opt , String defaultValue )
238+ {
239+ String answer = getOptionValue (opt );
240+
241+ return (answer != null ) ? answer : defaultValue ;
223242 }
224-
243+
225244 /**
226245 * <p>Retrieve the argument, if any, of an option.</p>
227246 *
228247 * @param opt character name of the option
229- * @param defaultValue is the default value to be returned if the option is not specified
248+ * @param defaultValue is the default value to be returned if the option
249+ * is not specified
230250 * @return Value of the argument if option is set, and has an argument,
231251 * otherwise <code>defaultValue</code>.
232252 */
233- public String getOptionValue ( char opt , String defaultValue ) {
234- return getOptionValue ( String .valueOf ( opt ), defaultValue );
253+ public String getOptionValue (char opt , String defaultValue )
254+ {
255+ return getOptionValue (String .valueOf (opt ), defaultValue );
235256 }
236257
237258 /**
238259 * <p>Retrieve any left-over non-recognized options and arguments</p>
239260 *
240261 * @return remaining items passed in but not parsed as an array
241262 */
242- public String [] getArgs () {
243- String [] answer = new String [ args .size () ];
244- args .toArray ( answer );
263+ public String [] getArgs ()
264+ {
265+ String [] answer = new String [args .size ()];
266+
267+ args .toArray (answer );
268+
245269 return answer ;
246270 }
247-
271+
248272 /**
249273 * <p>Retrieve any left-over non-recognized options and arguments</p>
250274 *
251275 * @return remaining items passed in but not parsed as a <code>List</code>.
252276 */
253- public List getArgList () {
277+ public List getArgList ()
278+ {
254279 return args ;
255280 }
256-
281+
257282 /**
258283 * jkeyes
259284 * - commented out until it is implemented properly
260285 * <p>Dump state, suitable for debugging.</p>
261286 *
262287 * @return Stringified form of this object
263288 */
289+
264290 /*
265291 public String toString() {
266292 StringBuffer buf = new StringBuffer();
267-
268- buf.append( "[ CommandLine: [ options: " );
269- buf.append( options.toString() );
270- buf.append( " ] [ args: ");
271- buf.append( args.toString() );
272- buf.append( " ] ]" );
273-
293+
294+ buf.append("[ CommandLine: [ options: ");
295+ buf.append(options.toString());
296+ buf.append(" ] [ args: ");
297+ buf.append(args.toString());
298+ buf.append(" ] ]");
299+
274300 return buf.toString();
275301 }
276302 */
@@ -280,28 +306,33 @@ public String toString() {
280306 *
281307 * @param arg the unrecognised option/argument.
282308 */
283- void addArg (String arg ) {
284- args .add ( arg );
309+ void addArg (String arg )
310+ {
311+ args .add (arg );
285312 }
286-
313+
287314 /**
288315 * <p>Add an option to the command line. The values of
289316 * the option are stored.</p>
290317 *
291318 * @param opt the processed option
292319 */
293- void addOption ( Option opt ) {
294- hashcodeMap .put ( new Integer ( opt .hashCode () ), opt );
320+ void addOption (Option opt )
321+ {
322+ hashcodeMap .put (new Integer (opt .hashCode ()), opt );
295323
296324 String key = opt .getKey ();
297- if ( key == null ) {
325+
326+ if (key == null )
327+ {
298328 key = opt .getLongOpt ();
299329 }
300- else {
301- names .put ( opt .getLongOpt (), key );
330+ else
331+ {
332+ names .put (opt .getLongOpt (), key );
302333 }
303334
304- options .put ( key , opt );
335+ options .put (key , opt );
305336 }
306337
307338 /**
@@ -310,7 +341,8 @@ void addOption( Option opt ) {
310341 * @return an <code>Iterator</code> over the processed {@link Option}
311342 * members of this {@link CommandLine}
312343 */
313- public Iterator iterator ( ) {
344+ public Iterator iterator ()
345+ {
314346 return hashcodeMap .values ().iterator ();
315347 }
316348
@@ -319,14 +351,15 @@ public Iterator iterator( ) {
319351 *
320352 * @return an array of the processed {@link Option}s.
321353 */
322- public Option [] getOptions ( ) {
354+ public Option [] getOptions ()
355+ {
323356 Collection processed = options .values ();
324357
358+
325359 // reinitialise array
326- optionsArray = new Option [ processed .size () ];
360+ optionsArray = new Option [processed .size ()];
327361
328362 // return the array
329- return (Option []) processed .toArray ( optionsArray );
363+ return (Option []) processed .toArray (optionsArray );
330364 }
331-
332- }
365+ }
0 commit comments