001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.commons.cli;
017:
018: import java.util.Collection;
019: import java.util.Iterator;
020: import java.util.LinkedList;
021: import java.util.List;
022: import java.util.Set;
023: import java.util.HashSet;
024:
025: /**
026: * <p>Represents list of arguments parsed against
027: * a {@link Options} descriptor.<p>
028: *
029: * <p>It allows querying of a boolean {@link #hasOption(String opt)},
030: * in addition to retrieving the {@link #getOptionValue(String opt)}
031: * for options requiring arguments.</p>
032: *
033: * <p>Additionally, any left-over or unrecognized arguments,
034: * are available for further processing.</p>
035: *
036: * @author bob mcwhirter (bob @ werken.com)
037: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
038: * @author John Keyes (john at integralsource.com)
039: */
040: public class CommandLine {
041:
042: /** the unrecognised options/arguments */
043: private List args = new LinkedList();
044:
045: /** the processed options */
046: private Set options = new HashSet();
047:
048: /** Map of unique options for ease to get complete list of options */
049: // private Set allOptions = new HashSet();
050: /**
051: * Creates a command line.
052: */
053: CommandLine() {
054: // nothing to do
055: }
056:
057: /**
058: * Query to see if an option has been set.
059: *
060: * @param opt Short name of the option
061: * @return true if set, false if not
062: */
063: public boolean hasOption(String opt) {
064: return options.contains(resolveOption(opt));
065: }
066:
067: /**
068: * Query to see if an option has been set.
069: *
070: * @param opt character name of the option
071: * @return true if set, false if not
072: */
073: public boolean hasOption(char opt) {
074: return hasOption(String.valueOf(opt));
075: }
076:
077: /**
078: * Return the <code>Object</code> type of this <code>Option</code>.
079: *
080: * @param opt the name of the option
081: * @return the type of this <code>Option</code>
082: */
083: public Object getOptionObject(String opt) {
084: String res = getOptionValue(opt);
085:
086: Option option = resolveOption(opt);
087: if (option == null) {
088: return null;
089: }
090:
091: Object type = option.getType();
092:
093: return (res == null) ? null : TypeHandler
094: .createValue(res, type);
095: }
096:
097: /**
098: * Return the <code>Object</code> type of this <code>Option</code>.
099: *
100: * @param opt the name of the option
101: * @return the type of opt
102: */
103: public Object getOptionObject(char opt) {
104: return getOptionObject(String.valueOf(opt));
105: }
106:
107: /**
108: * Retrieve the argument, if any, of this option.
109: *
110: * @param opt the name of the option
111: * @return Value of the argument if option is set, and has an argument,
112: * otherwise null.
113: */
114: public String getOptionValue(String opt) {
115: String[] values = getOptionValues(opt);
116:
117: return (values == null) ? null : values[0];
118: }
119:
120: /**
121: * Retrieve the argument, if any, of this option.
122: *
123: * @param opt the character name of the option
124: * @return Value of the argument if option is set, and has an argument,
125: * otherwise null.
126: */
127: public String getOptionValue(char opt) {
128: return getOptionValue(String.valueOf(opt));
129: }
130:
131: /**
132: * Retrieves the array of values, if any, of an option.
133: *
134: * @param opt string name of the option
135: * @return Values of the argument if option is set, and has an argument,
136: * otherwise null.
137: */
138: public String[] getOptionValues(String opt) {
139: Option key = resolveOption(opt);
140:
141: if (options.contains(key)) {
142: return key.getValues();
143: }
144:
145: return null;
146: }
147:
148: /**
149: * <p>Retrieves the option object given the long or short option as a String</p>
150: * @param opt short or long name of the option
151: * @return Canonicalized option
152: */
153: private Option resolveOption(String opt) {
154: opt = Util.stripLeadingHyphens(opt);
155: for (Iterator it = options.iterator(); it.hasNext();) {
156: Option option = (Option) it.next();
157: if (opt.equals(option.getOpt())) {
158: return option;
159: }
160: if (opt.equals(option.getLongOpt())) {
161: return option;
162: }
163:
164: }
165: return null;
166: }
167:
168: /**
169: * Retrieves the array of values, if any, of an option.
170: *
171: * @param opt character name of the option
172: * @return Values of the argument if option is set, and has an argument,
173: * otherwise null.
174: */
175: public String[] getOptionValues(char opt) {
176: return getOptionValues(String.valueOf(opt));
177: }
178:
179: /**
180: * Retrieve the argument, if any, of an option.
181: *
182: * @param opt name of the option
183: * @param defaultValue is the default value to be returned if the option
184: * is not specified
185: * @return Value of the argument if option is set, and has an argument,
186: * otherwise <code>defaultValue</code>.
187: */
188: public String getOptionValue(String opt, String defaultValue) {
189: String answer = getOptionValue(opt);
190:
191: return (answer != null) ? answer : defaultValue;
192: }
193:
194: /**
195: * Retrieve the argument, if any, of an option.
196: *
197: * @param opt character name of the option
198: * @param defaultValue is the default value to be returned if the option
199: * is not specified
200: * @return Value of the argument if option is set, and has an argument,
201: * otherwise <code>defaultValue</code>.
202: */
203: public String getOptionValue(char opt, String defaultValue) {
204: return getOptionValue(String.valueOf(opt), defaultValue);
205: }
206:
207: /**
208: * Retrieve any left-over non-recognized options and arguments
209: *
210: * @return remaining items passed in but not parsed as an array
211: */
212: public String[] getArgs() {
213: String[] answer = new String[args.size()];
214:
215: args.toArray(answer);
216:
217: return answer;
218: }
219:
220: /**
221: * Retrieve any left-over non-recognized options and arguments
222: *
223: * @return remaining items passed in but not parsed as a <code>List</code>.
224: */
225: public List getArgList() {
226: return args;
227: }
228:
229: /**
230: * jkeyes
231: * - commented out until it is implemented properly
232: * <p>Dump state, suitable for debugging.</p>
233: *
234: * @return Stringified form of this object
235: */
236:
237: /*
238: public String toString() {
239: StringBuffer buf = new StringBuffer();
240:
241: buf.append("[ CommandLine: [ options: ");
242: buf.append(options.toString());
243: buf.append(" ] [ args: ");
244: buf.append(args.toString());
245: buf.append(" ] ]");
246:
247: return buf.toString();
248: }
249: */
250:
251: /**
252: * Add left-over unrecognized option/argument.
253: *
254: * @param arg the unrecognised option/argument.
255: */
256: void addArg(String arg) {
257: args.add(arg);
258: }
259:
260: /**
261: * Add an option to the command line. The values of
262: * the option are stored.
263: *
264: * @param opt the processed option
265: */
266: void addOption(Option opt) {
267: options.add(opt);
268: }
269:
270: /**
271: * Returns an iterator over the Option members of CommandLine.
272: *
273: * @return an <code>Iterator</code> over the processed {@link Option}
274: * members of this {@link CommandLine}
275: */
276: public Iterator iterator() {
277: return options.iterator();
278: }
279:
280: /**
281: * Returns an array of the processed {@link Option}s.
282: *
283: * @return an array of the processed {@link Option}s.
284: */
285: public Option[] getOptions() {
286: Collection processed = options;
287:
288: // reinitialise array
289: Option[] optionsArray = new Option[processed.size()];
290:
291: // return the array
292: return (Option[]) processed.toArray(optionsArray);
293: }
294: }
|