130 } |
130 } |
131 } |
131 } |
132 throw new IllegalArgumentException(msg.toString()); |
132 throw new IllegalArgumentException(msg.toString()); |
133 } |
133 } |
134 |
134 |
|
135 Object value = parseOptionValue(desc, uncheckedValue); |
|
136 |
|
137 desc.getOptionKey().update(values, value); |
|
138 } |
|
139 |
|
140 /** Parses a given option value with a known descriptor. */ |
|
141 public static Object parseOptionValue(OptionDescriptor desc, Object uncheckedValue) { |
135 Class<?> optionType = desc.getOptionValueType(); |
142 Class<?> optionType = desc.getOptionValueType(); |
136 Object value; |
143 Object value; |
137 if (!(uncheckedValue instanceof String)) { |
144 if (!(uncheckedValue instanceof String)) { |
138 if (optionType != uncheckedValue.getClass()) { |
145 if (optionType != uncheckedValue.getClass()) { |
139 String type = optionType.getSimpleName(); |
146 String type = optionType.getSimpleName(); |
140 throw new IllegalArgumentException(type + " option '" + name + "' must have " + type + " value, not " + uncheckedValue.getClass() + " [toString: " + uncheckedValue + "]"); |
147 throw new IllegalArgumentException(type + " option '" + desc.getName() + "' must have " + type + " value, not " + uncheckedValue.getClass() + " [toString: " + uncheckedValue + "]"); |
141 } |
148 } |
142 value = uncheckedValue; |
149 value = uncheckedValue; |
143 } else { |
150 } else { |
144 String valueString = (String) uncheckedValue; |
151 String valueString = (String) uncheckedValue; |
145 if (optionType == Boolean.class) { |
152 if (optionType == Boolean.class) { |
146 if ("true".equals(valueString)) { |
153 if ("true".equals(valueString)) { |
147 value = Boolean.TRUE; |
154 value = Boolean.TRUE; |
148 } else if ("false".equals(valueString)) { |
155 } else if ("false".equals(valueString)) { |
149 value = Boolean.FALSE; |
156 value = Boolean.FALSE; |
150 } else { |
157 } else { |
151 throw new IllegalArgumentException("Boolean option '" + name + "' must have value \"true\" or \"false\", not \"" + uncheckedValue + "\""); |
158 throw new IllegalArgumentException("Boolean option '" + desc.getName() + "' must have value \"true\" or \"false\", not \"" + uncheckedValue + "\""); |
152 } |
159 } |
153 } else if (optionType == String.class) { |
160 } else if (optionType == String.class) { |
154 value = valueString; |
161 value = valueString; |
155 } else if (Enum.class.isAssignableFrom(optionType)) { |
162 } else if (Enum.class.isAssignableFrom(optionType)) { |
156 value = ((EnumOptionKey<?>) desc.getOptionKey()).valueOf(valueString); |
163 value = ((EnumOptionKey<?>) desc.getOptionKey()).valueOf(valueString); |
157 } else { |
164 } else { |
158 if (valueString.isEmpty()) { |
165 if (valueString.isEmpty()) { |
159 throw new IllegalArgumentException("Non empty value required for option '" + name + "'"); |
166 throw new IllegalArgumentException("Non empty value required for option '" + desc.getName() + "'"); |
160 } |
167 } |
161 try { |
168 try { |
162 if (optionType == Float.class) { |
169 if (optionType == Float.class) { |
163 value = Float.parseFloat(valueString); |
170 value = Float.parseFloat(valueString); |
164 } else if (optionType == Double.class) { |
171 } else if (optionType == Double.class) { |
166 } else if (optionType == Integer.class) { |
173 } else if (optionType == Integer.class) { |
167 value = Integer.valueOf((int) parseLong(valueString)); |
174 value = Integer.valueOf((int) parseLong(valueString)); |
168 } else if (optionType == Long.class) { |
175 } else if (optionType == Long.class) { |
169 value = Long.valueOf(parseLong(valueString)); |
176 value = Long.valueOf(parseLong(valueString)); |
170 } else { |
177 } else { |
171 throw new IllegalArgumentException("Wrong value for option '" + name + "'"); |
178 throw new IllegalArgumentException("Wrong value for option '" + desc.getName() + "'"); |
172 } |
179 } |
173 } catch (NumberFormatException nfe) { |
180 } catch (NumberFormatException nfe) { |
174 throw new IllegalArgumentException("Value for option '" + name + "' has invalid number format: " + valueString); |
181 throw new IllegalArgumentException("Value for option '" + desc.getName() + "' has invalid number format: " + valueString); |
175 } |
182 } |
176 } |
183 } |
177 } |
184 } |
178 |
185 return value; |
179 desc.getOptionKey().update(values, value); |
|
180 } |
186 } |
181 |
187 |
182 private static long parseLong(String v) { |
188 private static long parseLong(String v) { |
183 String valueString = v.toLowerCase(); |
189 String valueString = v.toLowerCase(); |
184 long scale = 1; |
190 long scale = 1; |