25 * TODO: more types |
25 * TODO: more types |
26 */ |
26 */ |
27 types = Collections.unmodifiableMap(m); |
27 types = Collections.unmodifiableMap(m); |
28 } |
28 } |
29 |
29 |
30 public CLIOptions parseOptions(String[] args) { |
30 public CLIOptions parseOptions(String[] args) throws CLIParserException { |
31 CLIOptions options = new CLIOptions(); |
31 CLIOptions options = new CLIOptions(); |
32 |
32 |
33 List<Integer> numberedTypes = new ArrayList<>(); |
33 List<Integer> numberedTypes = new ArrayList<>(); |
34 Map<String, Integer> namedTypes = new HashMap<>(); |
34 Map<String, Integer> namedTypes = new HashMap<>(); |
35 |
35 |
84 int paramIndex = options.getNumberedParameters().size(); |
84 int paramIndex = options.getNumberedParameters().size(); |
85 int paramType; |
85 int paramType; |
86 try { |
86 try { |
87 paramType = numberedTypes.get(paramIndex); |
87 paramType = numberedTypes.get(paramIndex); |
88 } catch (IndexOutOfBoundsException e) { |
88 } catch (IndexOutOfBoundsException e) { |
89 throw new IllegalArgumentException("Missing type for parameter #" + paramIndex, e); |
89 throw new CLIParserException("Missing type for parameter #" + paramIndex, e); |
90 } catch (NullPointerException e) { |
90 } catch (NullPointerException e) { |
91 throw new IllegalArgumentException("Invalid type definition for parameter #" + paramIndex, e); |
91 throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e); |
92 } |
92 } |
93 parameter = new Parameter(arg, paramType); |
93 parameter = new Parameter(arg, paramType); |
94 } |
94 } |
95 options.addNumberedParameter(parameter); |
95 options.addNumberedParameter(parameter); |
96 } |
96 } |
97 } |
97 } |
98 break; |
98 break; |
99 default: |
99 default: |
100 throw new IllegalArgumentException("Unknown option: " + arg); |
100 throw new CLIParserException("Unknown option: " + arg); |
101 } |
101 } |
102 } |
102 } |
103 return options; |
103 return options; |
104 } |
104 } |
105 |
105 |
106 private String fetchNext(String[] args, int index) { |
106 private String fetchNext(String[] args, int index) throws CLIParserException { |
107 if (index < args.length) { |
107 if (index < args.length) { |
108 return args[index]; |
108 return args[index]; |
109 } else { |
109 } else { |
110 throw new IllegalArgumentException("Expecting value for option: " + args[index - 1]); |
110 throw new CLIParserException("Expecting value for option: " + args[index - 1]); |
111 } |
111 } |
112 } |
112 } |
113 |
113 |
114 public static class Tokens { |
114 public static class Tokens { |
115 |
115 |