if (suppressionLineColumnNumber != null || configurationFile != null || propertiesFile != null || outputPath != null || parseResult.hasMatchedOption(OUTPUT_FORMAT_OPTION)) { result.add("Option '-t' cannot be used with other options."); if (configurationFile != null || propertiesFile != null || outputPath != null || parseResult.hasMatchedOption(OUTPUT_FORMAT_OPTION)) { result.add("Option '-s' cannot be used with other options.");
assert pr.hasMatchedOption("--verbose"); // as specified on command line assert pr.hasMatchedOption("-V"); // other aliases work also assert pr.hasMatchedOption('V'); // single-character alias works too assert pr.hasMatchedOption("verbose"); // and, command name without hyphens
@Test public void testMatchedOptions_ReturnsOnlyMatchedOptions() { class App { @Option(names = "-a", arity = "0..1") String a; @Option(names = "-b", arity = "0..1") String b; } CommandLine cmd = new CommandLine(new App()); ParseResult parseResult = cmd.parseArgs("-a"); List<OptionSpec> options = parseResult.matchedOptions(); assertEquals(1, options.size()); Map<String, OptionSpec> optionsMap = cmd.getCommandSpec().optionsMap(); assertTrue(parseResult.hasMatchedOption(optionsMap.get("-a"))); assertFalse(parseResult.hasMatchedOption(optionsMap.get("-b"))); }
@Test public void testInitializingDefaultsShouldNotAddOptionToParseResult() { CommandSpec spec = CommandSpec.create() .addOption(OptionSpec.builder("-x").type(String.class).defaultValue("xyz").build()); CommandLine cmd = new CommandLine(spec); ParseResult parseResult = cmd.parseArgs(); assertFalse(parseResult.hasMatchedOption('x')); }
@Before public void initMocks() { final List<CommandLine> commandLines = new ArrayList<>(); commandLines.add(mockCommandLine); when(mockParseResult.asCommandLineList()).thenReturn(commandLines); final List<String> originalArgs = new ArrayList<>(); originalArgs.add(CONFIG_FILE_OPTION_NAME); when(mockParseResult.originalArgs()).thenReturn(originalArgs); when(mockParseResult.matchedOption(CONFIG_FILE_OPTION_NAME)).thenReturn(mockConfigOptionSpec); when(mockParseResult.hasMatchedOption(CONFIG_FILE_OPTION_NAME)).thenReturn(true); when(mockConfigOptionSpec.getter()).thenReturn(mockConfigOptionGetter); }
@Test public void testHasMatchedOptionByShortName() { class App { @Option(names = "-x") String[] x; @Option(names = "-y") String y; } CommandLine cmd = new CommandLine(new App()); ParseResult parseResult = cmd.parseArgs("-x", "value1", "-x", "value2"); assertTrue(parseResult.hasMatchedOption('x')); assertFalse(parseResult.hasMatchedOption('y')); }
@Test public void testHasMatchedOptionByOptionSpec() { class App { @Option(names = "-x", arity = "0..1") String x; @Option(names = "-y", arity = "0..1") String y; } CommandLine cmd = new CommandLine(new App()); ParseResult parseResult = cmd.parseArgs("-x"); Map<String, OptionSpec> optionsMap = cmd.getCommandSpec().optionsMap(); assertTrue(parseResult.hasMatchedOption(optionsMap.get("-x"))); assertFalse(parseResult.hasMatchedOption(optionsMap.get("-y"))); }
@Test public void testRawOptionValueForBooleanOptions_ReturnsStringTrue() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-V", "--verbose").build()); CommandLine commandLine = new CommandLine(spec); ParseResult pr = commandLine.parseArgs("--verbose"); assertTrue(pr.hasMatchedOption("--verbose")); // as specified on command line assertTrue(pr.hasMatchedOption('V')); // single-character alias works too assertTrue(pr.hasMatchedOption("verbose")); // command name without hyphens assertTrue(pr.matchedOptionValue("verbose", Boolean.FALSE)); assertEquals("true", pr.matchedOption("verbose").stringValues().get(0)); }
@Test public void testOptionSpec_setsDefaultValue_ifNotMatched() { CommandSpec cmd = CommandSpec.create().addOption(OptionSpec.builder("-x").defaultValue("123").type(int.class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs(); assertFalse(parseResult.hasMatchedOption('x')); // TODO this method should be renamed to matchedOptionValue assertEquals(Integer.valueOf(-1), parseResult.matchedOptionValue('x', -1)); // TODO optionValue should return the value of the option, matched or not //assertEquals(Integer.valueOf(123), parseResult.optionValue('x')); assertEquals(Integer.valueOf(123), parseResult.commandSpec().findOption('x').getValue()); }
assertSame(parseResult.subcommand().commandSpec().positionalParameters().get(0), parseResult.tentativeMatch.get(5)); assertTrue(parseResult.hasMatchedOption("-x")); assertEquals("xval", parseResult.matchedOption("-x").stringValues().get(0)); assertEquals("xval", parseResult.matchedOptionValue("-x", "xval"));
@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 testHasMatchedOptionByName_VariousPrefixes() { class App { @Option(names = {"-x", "++XX", "/XXX"}) String[] x; @Option(names = "-y") String y; } CommandLine cmd = new CommandLine(new App()); ParseResult result = cmd.parseArgs("-x", "value1", "-x", "value2"); assertTrue(result.hasMatchedOption("x")); assertTrue(result.hasMatchedOption("-x")); assertTrue(result.hasMatchedOption("XX")); assertTrue(result.hasMatchedOption("++XX")); assertTrue(result.hasMatchedOption("XXX")); assertTrue(result.hasMatchedOption("/XXX")); assertFalse(result.hasMatchedOption("y")); assertFalse(result.hasMatchedOption("-y")); assertSame(result.commandSpec().findOption("-x"), result.tentativeMatch.get(0)); assertSame(result.originalArgs().get(1), result.tentativeMatch.get(1)); assertSame(result.commandSpec().findOption("-x"), result.tentativeMatch.get(2)); assertSame(result.originalArgs().get(3), result.tentativeMatch.get(3)); }
@Test public void testTypedValues() { class App { @Option(names="-x") int x; } ParseResult result1 = new CommandLine(new App()).parseArgs();// not specified assertFalse(result1.hasMatchedOption('x')); assertTrue(result1.commandSpec().findOption('x').typedValues().isEmpty()); ParseResult result2 = new CommandLine(new App()).parseArgs("-x", "123"); assertTrue(result2.hasMatchedOption('x')); assertEquals(Integer.valueOf(123), result2.matchedOptionValue('x', 0)); ParseResult result3 = new CommandLine(new App()) .setOverwrittenOptionsAllowed(true) .parseArgs("-x", "1", "-x", "2", "-x", "3"); assertTrue(result3.hasMatchedOption('x')); assertEquals(Integer.valueOf(3), result3.matchedOptionValue('x', 0)); assertEquals(Arrays.asList("1", "2", "3"), result3.matchedOption('x').stringValues()); assertEquals(Arrays.asList(1, 2, 3), result3.matchedOption('x').typedValues()); }
@Test public void testParserUnmatchedOptionsArePositionalParams_True_unmatchedOptionIsPositionalParam() { class App { @Option(names = "-a") String alpha; @Parameters String[] remainder; } App app = new App(); CommandLine cmd = new CommandLine(app); cmd.setUnmatchedOptionsArePositionalParams(true); ParseResult parseResult = cmd.parseArgs("-x", "-a", "AAA"); assertTrue(parseResult.hasMatchedPositional(0)); assertArrayEquals(new String[]{"-x"}, parseResult.matchedPositionalValue(0, new String[0])); assertTrue(parseResult.hasMatchedOption("a")); assertEquals("AAA", parseResult.matchedOptionValue("a", null)); assertArrayEquals(new String[]{"-x"}, app.remainder); assertEquals("AAA", app.alpha); }
@Test public void testOptionSpec_defaultValue_overwritesInitialValue() { class Params { @Option(names = "-x") int num = 12345; } CommandLine cmd = new CommandLine(new Params()); OptionSpec x = cmd.getCommandSpec().posixOptionsMap().get('x').toBuilder().defaultValue("54321").build(); cmd = new CommandLine(CommandSpec.create().addOption(x)); ParseResult parseResult = cmd.parseArgs(); assertFalse(parseResult.hasMatchedOption('x')); // TODO this method should be renamed to matchedOptionValue assertEquals(Integer.valueOf(-1), parseResult.matchedOptionValue('x', -1)); // TODO optionValue should return the value of the option, matched or not //assertEquals(Integer.valueOf(54321), parseResult.optionValue('x')); assertEquals(Integer.valueOf(54321), parseResult.commandSpec().findOption('x').getValue()); }
@Test public void testInitializingDefaultsShouldNotAddOptionToParseResult() { CommandSpec spec = CommandSpec.create() .addOption(OptionSpec.builder("-x").type(String.class).defaultValue("xyz").build()); CommandLine cmd = new CommandLine(spec); ParseResult parseResult = cmd.parseArgs(); assertFalse(parseResult.hasMatchedOption('x')); }
@Test public void testRawOptionValueForBooleanOptions_ReturnsStringTrue() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-V", "--verbose").build()); CommandLine commandLine = new CommandLine(spec); ParseResult pr = commandLine.parseArgs("--verbose"); assertTrue(pr.hasMatchedOption("--verbose")); // as specified on command line assertTrue(pr.hasMatchedOption('V')); // single-character alias works too assertTrue(pr.hasMatchedOption("verbose")); // command name without hyphens assertTrue(pr.matchedOptionValue("verbose", Boolean.FALSE)); assertEquals("true", pr.matchedOption("verbose").stringValues().get(0)); }
@Test public void testOptionSpec_setsDefaultValue_ifNotMatched() { CommandSpec cmd = CommandSpec.create().addOption(OptionSpec.builder("-x").defaultValue("123").type(int.class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs(); assertFalse(parseResult.hasMatchedOption('x')); // TODO this method should be renamed to matchedOptionValue assertEquals(Integer.valueOf(-1), parseResult.matchedOptionValue('x', -1)); // TODO optionValue should return the value of the option, matched or not //assertEquals(Integer.valueOf(123), parseResult.optionValue('x')); assertEquals(Integer.valueOf(123), parseResult.commandSpec().findOption('x').getValue()); }
@Test public void testHasMatchedOptionByShortName() { class App { @Option(names = "-x") String[] x; @Option(names = "-y") String y; } CommandLine cmd = new CommandLine(new App()); ParseResult parseResult = cmd.parseArgs("-x", "value1", "-x", "value2"); assertTrue(parseResult.hasMatchedOption('x')); assertFalse(parseResult.hasMatchedOption('y')); }
@Test public void testHasMatchedOptionByOptionSpec() { class App { @Option(names = "-x", arity = "0..1") String x; @Option(names = "-y", arity = "0..1") String y; } CommandLine cmd = new CommandLine(new App()); ParseResult parseResult = cmd.parseArgs("-x"); Map<String, OptionSpec> optionsMap = cmd.getCommandSpec().optionsMap(); assertTrue(parseResult.hasMatchedOption(optionsMap.get("-x"))); assertFalse(parseResult.hasMatchedOption(optionsMap.get("-y"))); }