System.out.println(getVersionString()); else if (parseResult.isUsageHelpRequested()) { commandLine.usage(System.out);
assertFalse(result.isUsageHelpRequested()); assertFalse(result.isVersionHelpRequested());
/** * TokenTypes.properties generator entry point. * @param args the command line arguments * @throws CheckstyleException if parser or lexer failed or if there is an IO problem **/ public static void main(String... args) throws CheckstyleException { final CliOptions cliOptions = new CliOptions(); final CommandLine cmd = new CommandLine(cliOptions).setUsageHelpWidth(USAGE_HELP_WIDTH); try { final ParseResult parseResult = cmd.parseArgs(args); if (parseResult.isUsageHelpRequested()) { cmd.usage(System.out); } else { writePropertiesFile(cliOptions); } } catch (ParameterException ex) { System.err.println(ex.getMessage()); ex.getCommandLine().usage(System.err); } }
System.out.println(getVersionString()); else if (parseResult.isUsageHelpRequested()) { commandLine.usage(System.out);
assertFalse(result.isUsageHelpRequested()); assertFalse(result.isVersionHelpRequested());
@Test public void testBasicUsage() { class App { @Option(names = {"-t", "-ttt"}) boolean boolVal; @Option(names = {"-i", "-int"}) int intVal; @Parameters String[] positional; } ParseResult result = new CommandLine(new App()).parseArgs("-t", "-i", "1", "a", "b"); assertEquals(Arrays.asList("-t", "-i", "1", "a", "b"), result.originalArgs()); assertSame(result.commandSpec().findOption("-t"), result.tentativeMatch.get(0)); assertSame(result.commandSpec().findOption("-i"), result.tentativeMatch.get(1)); assertSame(result.originalArgs().get(2), result.tentativeMatch.get(2)); assertSame(result.commandSpec().positionalParameters().get(0), result.tentativeMatch.get(3)); assertSame(result.commandSpec().positionalParameters().get(0), result.tentativeMatch.get(4)); assertTrue(result.unmatched().isEmpty()); assertFalse(result.hasSubcommand()); assertFalse(result.isUsageHelpRequested()); assertFalse(result.isVersionHelpRequested()); assertTrue(result.hasMatchedOption("-ttt")); assertTrue(result.hasMatchedOption("-t")); assertTrue(result.hasMatchedOption("-i")); assertTrue(result.hasMatchedOption("-int")); assertFalse(result.hasMatchedOption("-unknown")); assertTrue(result.hasMatchedPositional(0)); assertTrue(result.hasMatchedPositional(1)); } @Test
/** * TokenTypes.properties generator entry point. * @param args the command line arguments * @throws CheckstyleException if parser or lexer failed or if there is an IO problem **/ public static void main(String... args) throws CheckstyleException { final CliOptions cliOptions = new CliOptions(); final CommandLine cmd = new CommandLine(cliOptions).setUsageHelpWidth(USAGE_HELP_WIDTH); try { final ParseResult parseResult = cmd.parseArgs(args); if (parseResult.isUsageHelpRequested()) { cmd.usage(System.out); } else { writePropertiesFile(cliOptions); } } catch (ParameterException ex) { System.err.println(ex.getMessage()); ex.getCommandLine().usage(System.err); } }
@Test public void testIsUsageHelpRequested() { @Command(mixinStandardHelpOptions = true) class App { @Option(names = "-x") String x; } CommandLine cmd = new CommandLine(new App()); ParseResult parseResult = cmd.parseArgs("-h"); assertTrue(parseResult.isUsageHelpRequested()); assertFalse(parseResult.isVersionHelpRequested()); assertSame(cmd.getCommandSpec().optionsMap().get("-h"), parseResult.matchedOption('h')); assertTrue(parseResult.unmatched().isEmpty()); assertTrue(parseResult.matchedPositionals().isEmpty()); }
@Test public void testIsVersionHelpRequested() { @Command(mixinStandardHelpOptions = true) class App { @Option(names = "-x") String x; } CommandLine cmd = new CommandLine(new App()); ParseResult parseResult = cmd.parseArgs("--version"); assertFalse(parseResult.isUsageHelpRequested()); assertTrue(parseResult.isVersionHelpRequested()); assertSame(cmd.getCommandSpec().optionsMap().get("--version"), parseResult.matchedOption('V')); }
@Test public void testBooleanObjectUsageHelpOptions() { CommandSpec cmd = CommandSpec.create().add(OptionSpec.builder("-z").type(Boolean.class).usageHelp(true).build()); assertTrue(new CommandLine(cmd).parseArgs("-z").isUsageHelpRequested()); }
@Test public void testBasicUsage() { class App { @Option(names = {"-t", "-ttt"}) boolean boolVal; @Option(names = {"-i", "-int"}) int intVal; @Parameters String[] positional; } ParseResult result = new CommandLine(new App()).parseArgs("-t", "-i", "1", "a", "b"); assertEquals(Arrays.asList("-t", "-i", "1", "a", "b"), result.originalArgs()); assertSame(result.commandSpec().findOption("-t"), result.tentativeMatch.get(0)); assertSame(result.commandSpec().findOption("-i"), result.tentativeMatch.get(1)); assertSame(result.originalArgs().get(2), result.tentativeMatch.get(2)); assertSame(result.commandSpec().positionalParameters().get(0), result.tentativeMatch.get(3)); assertSame(result.commandSpec().positionalParameters().get(0), result.tentativeMatch.get(4)); assertTrue(result.unmatched().isEmpty()); assertFalse(result.hasSubcommand()); assertFalse(result.isUsageHelpRequested()); assertFalse(result.isVersionHelpRequested()); assertTrue(result.hasMatchedOption("-ttt")); assertTrue(result.hasMatchedOption("-t")); assertTrue(result.hasMatchedOption("-i")); assertTrue(result.hasMatchedOption("-int")); assertFalse(result.hasMatchedOption("-unknown")); assertTrue(result.hasMatchedPositional(0)); assertTrue(result.hasMatchedPositional(1)); } @Test
@Test public void testIsUsageHelpRequested() { @Command(mixinStandardHelpOptions = true) class App { @Option(names = "-x") String x; } CommandLine cmd = new CommandLine(new App()); ParseResult parseResult = cmd.parseArgs("-h"); assertTrue(parseResult.isUsageHelpRequested()); assertFalse(parseResult.isVersionHelpRequested()); assertSame(cmd.getCommandSpec().optionsMap().get("-h"), parseResult.matchedOption('h')); assertTrue(parseResult.unmatched().isEmpty()); assertTrue(parseResult.matchedPositionals().isEmpty()); }
@Test public void testIsVersionHelpRequested() { @Command(mixinStandardHelpOptions = true) class App { @Option(names = "-x") String x; } CommandLine cmd = new CommandLine(new App()); ParseResult parseResult = cmd.parseArgs("--version"); assertFalse(parseResult.isUsageHelpRequested()); assertTrue(parseResult.isVersionHelpRequested()); assertSame(cmd.getCommandSpec().optionsMap().get("--version"), parseResult.matchedOption('V')); }
@Test public void testBooleanObjectUsageHelpOptions() { CommandSpec cmd = CommandSpec.create().add(OptionSpec.builder("-z").type(Boolean.class).usageHelp(true).build()); assertTrue(new CommandLine(cmd).parseArgs("-z").isUsageHelpRequested()); }