…lines 1–12 unchanged (12 lines)
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.apache.commons.cli;
17
18import java.util.Collection;
19import java.util.HashMap;
20import java.util.Iterator;
21import java.util.LinkedList;
22import java.util.List;
23import java.util.Map;
24
25/**
26 * <p>Represents list of arguments parsed against
27 * a {@link Options} descriptor.<p>
28 *
29 * <p>It allows querying of a boolean {@link #hasOption(String opt)},
…lines 30–39 unchanged (10 lines)
40public class CommandLine {
41
42 /** the unrecognised options/arguments */
43 private List args = new LinkedList();
44
45 /** the processed options */
46 private Map options = new HashMap();
47 private Map names = new HashMap();
48
49 /** Map of unique options for ease to get complete list of options */
50// private Set allOptions = new HashSet();
51 private Map hashcodeMap = new HashMap();
52
53 /**
54 * Creates a command line.
55 */
56 CommandLine()
57 {
…lines 58–62 unchanged (5 lines)
63 *
64 * @param opt Short name of the option
65 * @return true if set, false if not
66 */
67 public boolean hasOption(String opt)
68 {
69 return options.containsKey(opt);
70 }
71
72 /**
73 * Query to see if an option has been set.
74 *
75 * @param opt character name of the option
…lines 76–85 unchanged (10 lines)
86 * @param opt the name of the option
87 * @return the type of this <code>Option</code>
88 */
89 public Object getOptionObject(String opt)
90 {
91 String res = getOptionValue(opt);
92
93 if (!options.containsKey(opt))
94 {
95 return null;
96 }
97
98 Object type = ((Option) options.get(opt)).getType();
99
100 return (res == null) ? null : TypeHandler.createValue(res, type);
101 }
102
103 /**
104 * Return the <code>Object</code> type of this <code>Option</code>.
…lines 105–142 unchanged (38 lines)
143 * @param opt string name of the option
144 * @return Values of the argument if option is set, and has an argument,
145 * otherwise null.
146 */
147 public String[] getOptionValues(String opt)
148 {
149 opt = Util.stripLeadingHyphens(opt);
150
151 String key = opt;
152 if (names.containsKey(opt))
153
154 {
155 key = (String) names.get(opt);
156 }
157
158 if (options.containsKey(key))
159 {
160 return ((Option) options.get(key)).getValues();
161 }
162 return null;
163 }
164
165 /**
166 * <p>Retrieves the option object given the long or short option as a String</p>
167 * @param opt short or long name of the option
168 * @return Canonicalized option
169 */
170
171
172 /**
173 * Retrieves the array of values, if any, of an option.
174 *
175 * @param opt character name of the option
176 * @return Values of the argument if option is set, and has an argument,
…lines 177–271 unchanged (95 lines)
272 * the option are stored.
273 *
274 * @param opt the processed option
275 */
276 void addOption(Option opt)
277 {
278 hashcodeMap.put(new Integer(opt.hashCode()), opt);
279 String key = opt.getKey();
280 if (key == null)
281 {
282 key = opt.getLongOpt();
283 }
284 else
285 {
286 names.put(opt.getLongOpt(), key);
287 }
288 options.put(key, opt);
289 }
290
291 /**
292 * Returns an iterator over the Option members of CommandLine.
293 *
294 * @return an <code>Iterator</code> over the processed {@link Option}
295 * members of this {@link CommandLine}
296 */
297 public Iterator iterator()
298 {
299 return hashcodeMap.values().iterator();
300 }
301
302 /**
303 * Returns an array of the processed {@link Option}s.
304 *
305 * @return an array of the processed {@link Option}s.
306 */
307 public Option[] getOptions()
308 {
309 Collection processed = options.values();
310
311 // reinitialise array
312 Option[] optionsArray = new Option[processed.size()];
313
314 // return the array
315 return (Option[]) processed.toArray(optionsArray);
316 }
317}
318