@Test public void testHasMatchedPositionalByPositionalSpec() { class App { @Option(names = "-x") String x; @Parameters(index = "0", arity = "0..1") int index0 = -1; @Parameters(index = "1", arity = "0..1") int index1 = -1; @Parameters(index = "2", arity = "0..1") int index2 = -1; } CommandLine cmd = new CommandLine(new App()); ParseResult result = cmd.parseArgs("-x", "xval", "0", "1"); assertSame(result.commandSpec().findOption("-x"), result.tentativeMatch.get(0)); assertSame(result.originalArgs().get(1), result.tentativeMatch.get(1)); assertSame(result.commandSpec().positionalParameters().get(0), result.tentativeMatch.get(2)); assertSame(result.commandSpec().positionalParameters().get(1), result.tentativeMatch.get(3)); List<PositionalParamSpec> all = cmd.getCommandSpec().positionalParameters(); assertTrue(result.hasMatchedPositional(all.get(0))); assertTrue(result.hasMatchedPositional(all.get(1))); assertFalse(result.hasMatchedPositional(all.get(2))); }
assertTrue(result.hasMatchedPositional(i)); assertEquals(args[i], result.matchedPositional(i).stringValues().get(i)); assertFalse(result.hasMatchedPositional(args.length));
@Test public void testPositionalParamSpec_defaultValue_overwritesInitialValue() { class Params { @Parameters int num = 12345; } CommandLine cmd = new CommandLine(new Params()); PositionalParamSpec x = cmd.getCommandSpec().positionalParameters().get(0).toBuilder().defaultValue("54321").build(); cmd = new CommandLine(CommandSpec.create().add(x)); ParseResult parseResult = cmd.parseArgs(); // default not in the parse result assertFalse(parseResult.hasMatchedPositional(0)); assertEquals(Integer.valueOf(-1), parseResult.matchedPositionalValue(0, -1)); // but positional spec does have the default value assertEquals(Integer.valueOf(54321), parseResult.commandSpec().positionalParameters().get(0).getValue()); }
assertEquals("xval", parseResult.matchedOption("-x").stringValues().get(0)); assertEquals("xval", parseResult.matchedOptionValue("-x", "xval")); assertFalse(parseResult.hasMatchedPositional(0)); assertEquals(Arrays.asList("-x", "xval", "sub", "1", "2", "3"), subResult.originalArgs()); // TODO should subresult.originalArgs include the args consumed by the parent? assertTrue(subResult.hasMatchedPositional(0)); assertTrue(subResult.hasMatchedPositional(1)); assertTrue(subResult.hasMatchedPositional(2)); assertFalse(subResult.hasMatchedPositional(3)); assertEquals("1", subResult.matchedPositional(0).stringValues().get(0)); assertEquals("2", subResult.matchedPositional(1).stringValues().get(1));
assertTrue(result.hasMatchedPositional(i)); assertEquals(args[i], result.matchedPositional(i).stringValues().get(i)); assertFalse(result.hasMatchedPositional(args.length));
@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 testHasMatchedPositionalByPositionalSpec() { class App { @Option(names = "-x") String x; @Parameters(index = "0", arity = "0..1") int index0 = -1; @Parameters(index = "1", arity = "0..1") int index1 = -1; @Parameters(index = "2", arity = "0..1") int index2 = -1; } CommandLine cmd = new CommandLine(new App()); ParseResult result = cmd.parseArgs("-x", "xval", "0", "1"); assertSame(result.commandSpec().findOption("-x"), result.tentativeMatch.get(0)); assertSame(result.originalArgs().get(1), result.tentativeMatch.get(1)); assertSame(result.commandSpec().positionalParameters().get(0), result.tentativeMatch.get(2)); assertSame(result.commandSpec().positionalParameters().get(1), result.tentativeMatch.get(3)); List<PositionalParamSpec> all = cmd.getCommandSpec().positionalParameters(); assertTrue(result.hasMatchedPositional(all.get(0))); assertTrue(result.hasMatchedPositional(all.get(1))); assertFalse(result.hasMatchedPositional(all.get(2))); }
@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 testPositionalParamSpec_defaultValue_overwritesInitialValue() { class Params { @Parameters int num = 12345; } CommandLine cmd = new CommandLine(new Params()); PositionalParamSpec x = cmd.getCommandSpec().positionalParameters().get(0).toBuilder().defaultValue("54321").build(); cmd = new CommandLine(CommandSpec.create().add(x)); ParseResult parseResult = cmd.parseArgs(); // default not in the parse result assertFalse(parseResult.hasMatchedPositional(0)); assertEquals(Integer.valueOf(-1), parseResult.matchedPositionalValue(0, -1)); // but positional spec does have the default value assertEquals(Integer.valueOf(54321), parseResult.commandSpec().positionalParameters().get(0).getValue()); }
@Test public void testPositionalParamSpec_setsDefaultValue_ifNotMatched() { CommandSpec cmd = CommandSpec.create().add(PositionalParamSpec.builder().defaultValue("123").type(int.class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs(); assertFalse(parseResult.hasMatchedPositional(0)); // TODO this method should be renamed to matchedPositionalValue assertEquals(Integer.valueOf(-1), parseResult.matchedPositionalValue(0, -1)); // TODO positionalValue should return the value of the option, matched or not //assertEquals(Integer.valueOf(123), parseResult.positionalValue(0)); assertEquals(Integer.valueOf(123), parseResult.commandSpec().positionalParameters().get(0).getValue()); }
@Test public void testInitializingDefaultsShouldNotAddPositionalToParseResult() { CommandSpec spec = CommandSpec.create() .addPositional(PositionalParamSpec.builder().defaultValue("xyz").build()); CommandLine cmd = new CommandLine(spec); ParseResult parseResult = cmd.parseArgs(); assertFalse(parseResult.hasMatchedPositional(0)); }
assertEquals("xval", parseResult.matchedOption("-x").stringValues().get(0)); assertEquals("xval", parseResult.matchedOptionValue("-x", "xval")); assertFalse(parseResult.hasMatchedPositional(0)); assertEquals(Arrays.asList("-x", "xval", "sub", "1", "2", "3"), subResult.originalArgs()); // TODO should subresult.originalArgs include the args consumed by the parent? assertTrue(subResult.hasMatchedPositional(0)); assertTrue(subResult.hasMatchedPositional(1)); assertTrue(subResult.hasMatchedPositional(2)); assertFalse(subResult.hasMatchedPositional(3)); assertEquals("1", subResult.matchedPositional(0).stringValues().get(0)); assertEquals("2", subResult.matchedPositional(1).stringValues().get(1));
@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 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 testPositionalParamSpec_setsDefaultValue_ifNotMatched() { CommandSpec cmd = CommandSpec.create().add(PositionalParamSpec.builder().defaultValue("123").type(int.class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs(); assertFalse(parseResult.hasMatchedPositional(0)); // TODO this method should be renamed to matchedPositionalValue assertEquals(Integer.valueOf(-1), parseResult.matchedPositionalValue(0, -1)); // TODO positionalValue should return the value of the option, matched or not //assertEquals(Integer.valueOf(123), parseResult.positionalValue(0)); assertEquals(Integer.valueOf(123), parseResult.commandSpec().positionalParameters().get(0).getValue()); }
@Test public void testInitializingDefaultsShouldNotAddPositionalToParseResult() { CommandSpec spec = CommandSpec.create() .addPositional(PositionalParamSpec.builder().defaultValue("xyz").build()); CommandLine cmd = new CommandLine(spec); ParseResult parseResult = cmd.parseArgs(); assertFalse(parseResult.hasMatchedPositional(0)); }