Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/OptionParser.java
r14441 r14640 42 42 public OptionParser addShortAlias(String optionName, String shortName) { 43 43 if (!shortName.matches("\\w")) { 44 throw new IllegalArgumentException("Short name " + shortName + " must be one character"); 44 throw new IllegalArgumentException("Short name '" + shortName + "' must be one character"); 45 45 } 46 46 if (availableOptions.containsKey("-" + shortName)) { 47 throw new IllegalArgumentException("Short name " + shortName + " is already used"); 47 throw new IllegalArgumentException("Short name '" + shortName + "' is already used"); 48 48 } 49 49 AvailableOption longDefinition = availableOptions.get("--" + optionName); … … 70 70 private void checkOptionName(String optionName) { 71 71 if (!optionName.matches("\\w([\\w-]*\\w)?")) { 72 throw new IllegalArgumentException("Illegal option name: " + optionName); 72 throw new IllegalArgumentException("Illegal option name: '" + optionName + "'"); 73 73 } 74 74 if (availableOptions.containsKey("--" + optionName)) { 75 throw new IllegalArgumentException("The option --" + optionName + " is already registered"); 75 throw new IllegalArgumentException("The option '--" + optionName + "' is already registered"); 76 76 } 77 77 } … … 164 164 } else { 165 165 if (toHandle.isEmpty() || "--".equals(toHandle.getFirst())) { 166 throw new OptionParseException(tr("{0}: option ''{1}'' requires an argument", program)); 166 throw new OptionParseException(tr("{0}: option ''{1}'' requires an argument", program, optionName)); 167 167 } 168 168 parameter = toHandle.removeFirst(); … … 179 179 availableOptions.values().stream().distinct().forEach(def -> { 180 180 long count = options.stream().filter(p -> def.equals(p.option)).count(); 181 String optionName = availableOptions.entrySet().stream() 182 .filter(entry -> def.equals(entry.getValue())) 183 .map(Entry::getKey) 184 .findFirst() 185 .orElse("?"); 181 186 if (count < def.getRequiredCount().min) { 182 187 // min may be 0 or 1 at the moment 183 throw new OptionParseException(tr("{0}: option ''{1}'' is required")); 188 throw new OptionParseException(tr("{0}: option ''{1}'' is required", program, optionName)); 184 189 } else if (count > def.getRequiredCount().max) { 185 190 // max may be 1 or MAX_INT at the moment 186 throw new OptionParseException(tr("{0}: option ''{1}'' may not appear multiple times")); 191 throw new OptionParseException(tr("{0}: option ''{1}'' may not appear multiple times", program, optionName)); 187 192 } 188 193 }); … … 222 227 return alternatives.get(0); 223 228 } else if (alternatives.size() > 1) { 224 throw new OptionParseException(tr("{0}: option ''{1}'' is ambiguous", program)); 229 throw new OptionParseException(tr("{0}: option ''{1}'' is ambiguous", program, optionName)); 225 230 } 226 231 } -
trunk/test/unit/org/openstreetmap/josm/tools/OptionParserTest.java
r14435 r14640 12 12 import java.util.concurrent.atomic.AtomicReference; 13 13 14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 15 import org.junit.Rule; 14 16 import org.junit.Test; 17 import org.junit.rules.ExpectedException; 18 import org.openstreetmap.josm.testutils.JOSMTestRules; 15 19 import org.openstreetmap.josm.tools.OptionParser.OptionCount; 16 20 import org.openstreetmap.josm.tools.OptionParser.OptionParseException; … … 22 26 public class OptionParserTest { 23 27 28 /** 29 * Rule used for tests throwing exceptions. 30 */ 31 @Rule 32 public ExpectedException thrown = ExpectedException.none(); 33 34 /** 35 * Setup test. 36 */ 37 @Rule 38 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 39 public JOSMTestRules test = new JOSMTestRules().i18n(); 40 24 41 // A reason for moving to jupiter... 25 @Test (expected = OptionParseException.class)42 @Test 26 43 public void testEmptyParserRejectsLongopt() { 44 thrown.expect(OptionParseException.class); 45 thrown.expectMessage("test: unrecognized option '--long'"); 27 46 new OptionParser("test").parseOptions(Arrays.asList("--long")); 28 47 } 29 48 30 @Test (expected = OptionParseException.class)49 @Test 31 50 public void testEmptyParserRejectsShortopt() { 51 thrown.expect(OptionParseException.class); 52 thrown.expectMessage("test: unrecognized option '-s'"); 32 53 new OptionParser("test").parseOptions(Arrays.asList("-s")); 33 54 } 34 55 35 @Test (expected = OptionParseException.class)56 @Test 36 57 public void testParserRejectsWrongShortopt() { 58 thrown.expect(OptionParseException.class); 59 thrown.expectMessage("test: unrecognized option '-s'"); 37 60 new OptionParser("test").addFlagParameter("test", this::nop).addShortAlias("test", "t") 38 61 .parseOptions(Arrays.asList("-s")); 39 62 } 40 63 41 @Test (expected = OptionParseException.class)64 @Test 42 65 public void testParserRejectsWrongLongopt() { 66 thrown.expect(OptionParseException.class); 67 thrown.expectMessage("test: unrecognized option '--wrong'"); 43 68 new OptionParser("test").addFlagParameter("test", this::nop).parseOptions(Arrays.asList("--wrong")); 44 69 } … … 54 79 } 55 80 56 @Test (expected = OptionParseException.class)81 @Test 57 82 public void testParserOptionFailsIfMissing() { 83 thrown.expect(OptionParseException.class); 84 thrown.expectMessage("test: unrecognized option '--test2'"); 58 85 AtomicReference<String> argFound = new AtomicReference<>(); 59 86 OptionParser parser = new OptionParser("test") … … 63 90 } 64 91 65 @Test (expected = OptionParseException.class)92 @Test 66 93 public void testParserOptionFailsIfMissingArgument() { 94 thrown.expect(OptionParseException.class); 95 thrown.expectMessage("test: unrecognized option '--test2'"); 67 96 AtomicReference<String> argFound = new AtomicReference<>(); 68 97 OptionParser parser = new OptionParser("test") … … 72 101 } 73 102 74 @Test (expected = OptionParseException.class)103 @Test 75 104 public void testParserOptionFailsIfMissing2() { 105 thrown.expect(OptionParseException.class); 106 thrown.expectMessage("test: option '--test' is required"); 76 107 AtomicReference<String> argFound = new AtomicReference<>(); 77 108 OptionParser parser = new OptionParser("test") … … 81 112 } 82 113 83 @Test (expected = OptionParseException.class)114 @Test 84 115 public void testParserOptionFailsIfTwice() { 116 thrown.expect(OptionParseException.class); 117 thrown.expectMessage("test: option '--test' may not appear multiple times"); 85 118 AtomicReference<String> argFound = new AtomicReference<>(); 86 119 OptionParser parser = new OptionParser("test") … … 90 123 } 91 124 92 @Test (expected = OptionParseException.class)125 @Test 93 126 public void testParserOptionFailsIfTwiceForAlias() { 127 thrown.expect(OptionParseException.class); 128 thrown.expectMessage("test: option '-t' may not appear multiple times"); 94 129 AtomicReference<String> argFound = new AtomicReference<>(); 95 130 OptionParser parser = new OptionParser("test") … … 100 135 } 101 136 102 @Test (expected = OptionParseException.class)137 @Test 103 138 public void testOptionalOptionFailsIfTwice() { 139 thrown.expect(OptionParseException.class); 140 thrown.expectMessage("test: option '--test' may not appear multiple times"); 104 141 OptionParser parser = new OptionParser("test") 105 142 .addFlagParameter("test", this::nop); … … 107 144 } 108 145 109 @Test (expected = OptionParseException.class)146 @Test 110 147 public void testOptionalOptionFailsIfTwiceForAlias() { 148 thrown.expect(OptionParseException.class); 149 thrown.expectMessage("test: option '-t' may not appear multiple times"); 111 150 OptionParser parser = new OptionParser("test") 112 151 .addFlagParameter("test", this::nop) … … 115 154 } 116 155 117 @Test (expected = OptionParseException.class)156 @Test 118 157 public void testOptionalOptionFailsIfTwiceForAlias2() { 158 thrown.expect(OptionParseException.class); 159 thrown.expectMessage("test: option '-t' may not appear multiple times"); 119 160 OptionParser parser = new OptionParser("test") 120 161 .addFlagParameter("test", this::nop) … … 145 186 } 146 187 147 @Test (expected = OptionParseException.class)188 @Test 148 189 public void testLongArgumentsMissingOption() { 190 thrown.expect(OptionParseException.class); 191 thrown.expectMessage("test: option '--test' requires an argument"); 149 192 OptionParser parser = new OptionParser("test") 150 193 .addArgumentParameter("test", OptionCount.REQUIRED, this::nop); … … 153 196 } 154 197 155 @Test (expected = OptionParseException.class)198 @Test 156 199 public void testLongArgumentsMissingOption2() { 200 thrown.expect(OptionParseException.class); 201 thrown.expectMessage("test: option '--test' requires an argument"); 157 202 OptionParser parser = new OptionParser("test") 158 203 .addArgumentParameter("test", OptionCount.REQUIRED, this::nop); … … 161 206 } 162 207 163 @Test (expected = OptionParseException.class)208 @Test 164 209 public void testShortArgumentsMissingOption() { 210 thrown.expect(OptionParseException.class); 211 thrown.expectMessage("test: option '-t' requires an argument"); 165 212 OptionParser parser = new OptionParser("test") 166 213 .addArgumentParameter("test", OptionCount.REQUIRED, this::nop) … … 170 217 } 171 218 172 @Test (expected = OptionParseException.class)219 @Test 173 220 public void testShortArgumentsMissingOption2() { 221 thrown.expect(OptionParseException.class); 222 thrown.expectMessage("test: option '-t' requires an argument"); 174 223 OptionParser parser = new OptionParser("test") 175 224 .addArgumentParameter("test", OptionCount.REQUIRED, this::nop) … … 179 228 } 180 229 181 @Test (expected = OptionParseException.class)230 @Test 182 231 public void testLongFlagHasOption() { 232 thrown.expect(OptionParseException.class); 233 thrown.expectMessage("test: option '--test' does not allow an argument"); 183 234 OptionParser parser = new OptionParser("test") 184 235 .addFlagParameter("test", this::nop); … … 187 238 } 188 239 189 @Test (expected = OptionParseException.class)240 @Test 190 241 public void testShortFlagHasOption() { 242 thrown.expect(OptionParseException.class); 243 thrown.expectMessage("test: option '-t' does not allow an argument"); 191 244 OptionParser parser = new OptionParser("test") 192 245 .addFlagParameter("test", this::nop) … … 251 304 } 252 305 253 @Test (expected = OptionParseException.class)306 @Test 254 307 public void testAmbiguousAlternatives() { 308 thrown.expect(OptionParseException.class); 309 thrown.expectMessage("test: option '--fl' is ambiguous"); 255 310 AtomicReference<String> argFound = new AtomicReference<>(); 256 311 AtomicBoolean usedFlag = new AtomicBoolean(); … … 265 320 } 266 321 267 @Test (expected = OptionParseException.class)322 @Test 268 323 public void testMultipleShort() { 324 thrown.expect(OptionParseException.class); 325 thrown.expectMessage("test: unrecognized option '-ft'"); 269 326 AtomicReference<String> argFound = new AtomicReference<>(); 270 327 AtomicBoolean usedFlag = new AtomicBoolean(); … … 300 357 } 301 358 302 @Test (expected = IllegalArgumentException.class)359 @Test 303 360 public void testIllegalOptionName() { 361 thrown.expect(IllegalArgumentException.class); 362 thrown.expectMessage("Illegal option name: ''"); 304 363 new OptionParser("test").addFlagParameter("", this::nop); 305 364 } 306 365 307 @Test (expected = IllegalArgumentException.class)366 @Test 308 367 public void testIllegalOptionName2() { 368 thrown.expect(IllegalArgumentException.class); 369 thrown.expectMessage("Illegal option name: '-'"); 309 370 new OptionParser("test").addFlagParameter("-", this::nop); 310 371 } 311 372 312 @Test (expected = IllegalArgumentException.class)373 @Test 313 374 public void testIllegalOptionName3() { 375 thrown.expect(IllegalArgumentException.class); 376 thrown.expectMessage("Illegal option name: '-test'"); 314 377 new OptionParser("test").addFlagParameter("-test", this::nop); 315 378 } 316 379 317 @Test (expected = IllegalArgumentException.class)380 @Test 318 381 public void testIllegalOptionName4() { 382 thrown.expect(IllegalArgumentException.class); 383 thrown.expectMessage("Illegal option name: '$'"); 319 384 new OptionParser("test").addFlagParameter("$", this::nop); 320 385 } 321 386 322 @Test (expected = IllegalArgumentException.class)387 @Test 323 388 public void testDuplicateOptionName() { 389 thrown.expect(IllegalArgumentException.class); 390 thrown.expectMessage("The option '--test' is already registered"); 324 391 new OptionParser("test").addFlagParameter("test", this::nop).addFlagParameter("test", this::nop); 325 392 } 326 393 327 @Test (expected = IllegalArgumentException.class)394 @Test 328 395 public void testDuplicateOptionName2() { 396 thrown.expect(IllegalArgumentException.class); 397 thrown.expectMessage("The option '--test' is already registered"); 329 398 new OptionParser("test").addFlagParameter("test", this::nop) 330 399 .addArgumentParameter("test", OptionCount.OPTIONAL, this::nop); 331 400 } 332 401 333 @Test (expected = IllegalArgumentException.class)402 @Test 334 403 public void testInvalidShortAlias() { 404 thrown.expect(IllegalArgumentException.class); 405 thrown.expectMessage("Short name '$' must be one character"); 335 406 new OptionParser("test").addFlagParameter("test", this::nop).addShortAlias("test", "$"); 336 407 } 337 408 338 @Test (expected = IllegalArgumentException.class)409 @Test 339 410 public void testInvalidShortAlias2() { 411 thrown.expect(IllegalArgumentException.class); 412 thrown.expectMessage("Short name '' must be one character"); 340 413 new OptionParser("test").addFlagParameter("test", this::nop).addShortAlias("test", ""); 341 414 } 342 415 343 @Test (expected = IllegalArgumentException.class)416 @Test 344 417 public void testInvalidShortAlias3() { 418 thrown.expect(IllegalArgumentException.class); 419 thrown.expectMessage("Short name 'xx' must be one character"); 345 420 new OptionParser("test").addFlagParameter("test", this::nop).addShortAlias("test", "xx"); 346 421 } 347 422 348 @Test (expected = IllegalArgumentException.class)423 @Test 349 424 public void testDuplicateShortAlias() { 425 thrown.expect(IllegalArgumentException.class); 426 thrown.expectMessage("Short name 't' is already used"); 350 427 new OptionParser("test").addFlagParameter("test", this::nop) 351 428 .addFlagParameter("test2", this::nop) … … 354 431 } 355 432 356 @Test (expected = IllegalArgumentException.class)433 @Test 357 434 public void testInvalidShortNoLong() { 435 thrown.expect(IllegalArgumentException.class); 436 thrown.expectMessage("No long definition for test2 was defined. " + 437 "Define the long definition first before creating a short definition for it."); 358 438 new OptionParser("test").addFlagParameter("test", this::nop).addShortAlias("test2", "t"); 359 439 }
Note:
See TracChangeset
for help on using the changeset viewer.