Get the next option. @return The short designator for the next argument. If there are no more arguments than the special designator CmdLineParser.EndOfOpts will be returned. @throws ParseException if an unknown option is found or an option that expects a value does not have one or a value that does
()
| 87 | * one. |
| 88 | */ |
| 89 | public char getNextOpt() throws ParseException |
| 90 | { |
| 91 | if (mArgNum >= mArgs.length) return EndOfOpts; |
| 92 | |
| 93 | int offset = 1; |
| 94 | mVal = null; |
| 95 | try { |
| 96 | String arg = mArgs[mArgNum]; |
| 97 | // If it doesn't start with a dash, we'll assume we've reached the end of the |
| 98 | // arguments. We need to decrement mArgNum because the finally at the end is |
| 99 | // going to increment it. |
| 100 | if (arg.charAt(0) != '-') { |
| 101 | mArgNum--; |
| 102 | return EndOfOpts; |
| 103 | } |
| 104 | |
| 105 | // Strip any dashes off of the beginning |
| 106 | for (int i = 1; i < arg.length() && arg.charAt(i) == '-'; i++) offset++; |
| 107 | |
| 108 | // If they passed us a - or -- then quit |
| 109 | if (offset == arg.length()) return EndOfOpts; |
| 110 | |
| 111 | Character cc = null; |
| 112 | if (arg.substring(offset).length() == 1) { |
| 113 | cc = Character.valueOf(arg.substring(offset).charAt(0)); |
| 114 | } else { |
| 115 | cc = mLong.get(arg.substring(offset)); |
| 116 | if (cc == null) { |
| 117 | Integer ii = Integer.valueOf(mArgNum + 1); |
| 118 | String errMsg = "Found unknown option (" + arg + ") at position " + |
| 119 | ii.toString(); |
| 120 | throw new ParseException(errMsg, mArgNum); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | ValueExpected ve = mShort.get(cc); |
| 125 | if (ve == null) { |
| 126 | Integer ii = Integer.valueOf(mArgNum + 1); |
| 127 | String errMsg = "Found unknown option (" + arg + ") at position " + |
| 128 | ii.toString(); |
| 129 | throw new ParseException(errMsg, mArgNum); |
| 130 | } |
| 131 | |
| 132 | switch (ve) { |
| 133 | case NOT_ACCEPTED: |
| 134 | return cc.charValue(); |
| 135 | |
| 136 | case REQUIRED: |
| 137 | // If it requires an option, make sure there is one. |
| 138 | if (mArgNum + 1 >= mArgs.length || mArgs[mArgNum + 1].charAt(0) == '-') { |
| 139 | String errMsg = "Option " + arg + |
| 140 | " requires a value but you did not provide one."; |
| 141 | throw new ParseException(errMsg, mArgNum); |
| 142 | } |
| 143 | mVal = mArgs[++mArgNum]; |
| 144 | return cc.charValue(); |
| 145 | |
| 146 | case OPTIONAL: |