001: package jimm.util;
002:
003: import java.util.HashMap;
004:
005: /**
006: * Getopts is similar to the UN*X getopt() system call. It parses an array of
007: * Strings (usually the command line), looking for specified option flags and
008: * values.
009: * <p>
010: * An instance of Getopts parses the whole args list at once, and stores the
011: * option flags and values that it finds.
012: *
013: * @author Jim Menard,
014: * <a href="mailto:jimm@io.com">jimm@io.com</a>
015: */
016: public class Getopts {
017: String[] argv;
018: HashMap options = new HashMap();
019: boolean errorFlag = false;
020:
021: /**
022: * This constructor takes a list of legal options and a list of (usually
023: * command line) arguments. Each option in optionListString may be followed
024: * by a ':' to signify that option takes an argument.
025: *
026: * @param optionListString option chars with optional ':' specifying arg.
027: * For example, "ab:c" specifies three options, a, b, and c. Option b takes
028: * a (required) argument.
029: * @param args array of command line arguments
030: */
031: public Getopts(String optionListString, String[] args) {
032: String optChoices = optionListString;
033:
034: for (int index = 0; index < args.length; ++index) {
035: String arg = args[index];
036: if (arg.startsWith("-")) {
037: char optionChar = arg.charAt(1);
038: int optionLoc = optChoices.indexOf(optionChar);
039: if (optionLoc == -1)
040: errorFlag = true;
041: else {
042: // Look for argument, if any
043: boolean hasArgument = optChoices.length() > optionLoc + 1
044: && optChoices.charAt(optionLoc + 1) == ':';
045: if (hasArgument) {
046: String optarg = arg.substring(2);
047: if (optarg.equals("")) {
048: ++index;
049: try {
050: optarg = args[index];
051: } catch (Exception e) { // Catch ArrayOutOfBounds
052: optarg = "";
053: errorFlag = true;
054: }
055: }
056: options.put(new Character(optionChar), optarg);
057: } else {
058: // No arg, store empty string
059: options.put(new Character(optionChar), "");
060: }
061: }
062: } else { // End of options. Store rest of args
063: argv = new String[args.length - index];
064: int offset = index;
065: while (index < args.length) {
066: argv[index - offset] = args[index];
067: ++index;
068: }
069: break;
070: }
071: }
072: }
073:
074: /**
075: *
076: * Return true if there was an error while parsing the command line.
077: */
078: public boolean error() {
079: return errorFlag;
080: }
081:
082: /**
083: * Returns existence of an option.
084: *
085: * @return true of option 'c' exists, else return false.
086: * @param c any character
087: */
088: public boolean hasOption(char c) {
089: if (options == null)
090: return false;
091: return options.containsKey(new Character(c));
092: }
093:
094: /**
095: * Return an option or, if missing, the empty string.
096: *
097: * @return option string, or "" if error or option has no argument
098: * @param c the option whose value is returned
099: */
100: public String option(char c) {
101: return option(c, "");
102: }
103:
104: /**
105: * Return an option or, if missing, a default value.
106: *
107: * @return option string, or defaultValue if error or option has no argument
108: * @param c the option whose value is returned
109: * @param defaultValue the value to return if there is no such option
110: */
111: public String option(char c, String defaultValue) {
112: if (options == null)
113: return defaultValue;
114:
115: String s;
116: try {
117: Object o = options.get(new Character(c));
118: if (o == null || !(o instanceof String))
119: s = defaultValue;
120: else
121: s = (String) o;
122: } catch (Exception e) {
123: s = defaultValue;
124: }
125: return s;
126: }
127:
128: /**
129: * Return the remaining command-line arguments.
130: *
131: * @return an array of Strings
132: * @see #argc
133: * @see #argv
134: */
135: public String[] args() {
136: return argv;
137: }
138:
139: /**
140: * Return the number of non-option args.
141: */
142: public int argc() {
143: if (argv == null)
144: return 0;
145: return argv.length;
146: }
147:
148: /**
149: * Return a command line argument or "" if <var>argv</var> is
150: * <code>null</code>. Index starts at 0.
151: *
152: * @param index which argument to return
153: * @return the index'th arg or "" if <var>argv</var> is <code>null</code>
154: */
155: public String argv(int index) {
156: if (argv == null)
157: return "";
158:
159: return argv[index];
160: }
161: }
|