/** * Tests if the argument are correct or incorrect. Incorrect means, that mandatory arguments are missing or * there are unknown arguments. The argument value itself can also be incorrect, but this * is checked by the {@link ArgumentParser#parse(String[], Class)} method and reported accordingly. * * @param args command line arguments * @param argProxyInterface interface with parameters description * @return true, if arguments are valid */ @SuppressWarnings({"unchecked"}) public static <T> boolean validateArguments(String[] args, Class<T> argProxyInterface) { return validateArguments(args, new Class[]{argProxyInterface}); }
if (!validateArguments(args, argProxyInterface)) throw new IllegalArgumentException("Passed args must be valid!");
@Test(expected = IllegalArgumentException.class) public void testSimpleArgumentsMissingEncoding() { String argsString = "-alphaNumOpt false"; Assert.assertFalse(ArgumentParser.validateArguments(argsString.split(" "), SimpleArguments.class)); ArgumentParser.parse(argsString.split(" "), SimpleArguments.class); }
@Test public void testAllOptionalArgumentsOneArgument() { String argsString = "-alphaNumOpt false"; Assert.assertTrue(ArgumentParser.validateArguments(argsString.split(" "), AllOptionalArguments.class)); ArgumentParser.parse(argsString.split(" "), AllOptionalArguments.class); }
@Test(expected = IllegalArgumentException.class) public void testAllOptionalArgumentsExtraArgument() { String argsString = "-encoding UTF-8"; Assert.assertFalse(ArgumentParser.validateArguments(argsString.split(" "), AllOptionalArguments.class)); ArgumentParser.parse(argsString.split(" "), AllOptionalArguments.class); }
@Test public void testSetEncodingParameter() { Collection<Charset> availableCharset = Charset.availableCharsets().values(); String notTheDefaultCharset = "UTF-8"; for (Charset charset : availableCharset) { if (!charset.equals(Charset.defaultCharset())) { notTheDefaultCharset = charset.name(); break; } } String[] args = ("-something aValue -encoding " + notTheDefaultCharset).split(" "); Assert.assertTrue(ArgumentParser.validateArguments(args, ExtendsEncodingParameter.class)); ExtendsEncodingParameter params = ArgumentParser.parse(args, ExtendsEncodingParameter.class); Assert.assertEquals(Charset.forName(notTheDefaultCharset), params.getEncoding()); } }
@Test public void testAllOptionalArgumentsZeroArguments() { String[] args = {}; Assert.assertTrue(ArgumentParser.validateArguments(args, AllOptionalArguments.class)); ArgumentParser.parse(args, AllOptionalArguments.class); }
@Test public void testDefaultEncodingParameter() { String[] args = "-something aValue".split(" "); Assert.assertTrue(ArgumentParser.validateArguments(args, ExtendsEncodingParameter.class)); ExtendsEncodingParameter params = ArgumentParser.parse(args, ExtendsEncodingParameter.class); Assert.assertEquals(Charset.defaultCharset(), params.getEncoding()); }
/** * Tests if the argument are correct or incorrect. Incorrect means, that mandatory arguments are missing or * there are unknown arguments. The argument value itself can also be incorrect, but this * is checked by the {@link ArgumentParser#parse(String[], Class)} method and reported accordingly. * * @param args command line arguments * @param argProxyInterface interface with parameters description * @return true, if arguments are valid */ @SuppressWarnings({"unchecked"}) public static <T> boolean validateArguments(String[] args, Class<T> argProxyInterface) { return validateArguments(args, new Class[]{argProxyInterface}); }
/** * Tests if the argument are correct or incorrect. Incorrect means, that mandatory arguments are missing or * there are unknown arguments. The argument value itself can also be incorrect, but this * is checked by the {@link ArgumentParser#parse(String[], Class)} method and reported accordingly. * * @param args command line arguments * @param argProxyInterface interface with parameters description * @return true, if arguments are valid */ @SuppressWarnings({"unchecked"}) public static <T> boolean validateArguments(String[] args, Class<T> argProxyInterface) { return validateArguments(args, new Class[]{argProxyInterface}); }
if (!validateArguments(args, argProxyInterface)) throw new IllegalArgumentException("Passed args must be valid!");
if (!validateArguments(args, argProxyInterface)) throw new IllegalArgumentException("Passed args must be valid!");
public void run(String[] args) { if (!ArgumentParser.validateArguments(args, DisambiguatorEvaluatorParams.class)) { System.err.println(getHelp());
public void run(String[] args) { if (!ArgumentParser.validateArguments(args, DisambiguatorToolParams.class)) { System.err.println(getHelp()); throw new TerminateToolException(1); } DisambiguatorToolParams params = ArgumentParser.parse(args, DisambiguatorToolParams.class); WSDisambiguator disambiguator = makeTool(params); PerformanceMonitor perfMon = new PerformanceMonitor(System.err, "sent"); ObjectStream<String> lineStream = new PlainTextByLineStream( new InputStreamReader(System.in)); perfMon.start(); try { String line; while ((line = lineStream.read()) != null) { WSDSample sample = WSDSample.parse(line); WSDHelper.printResults(disambiguator, disambiguator.disambiguate(sample)); perfMon.incrementCounter(); } } catch (IOException e) { CmdLineUtil.handleStdinIoError(e); } perfMon.stopAndPrintFinalResult(); }