@Test public void testRawPositionalValueWithDefault_ReturnsDefaultForNonMatchedPosition() { class App { @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 parseResult = cmd.parseArgs("0", "1"); assertEquals(Integer.valueOf(0), parseResult.matchedPositionalValue(0, Integer.valueOf(123))); assertEquals(Integer.valueOf(1), parseResult.matchedPositionalValue(1, Integer.valueOf(456))); assertEquals(Integer.valueOf(123), parseResult.matchedPositionalValue(2, Integer.valueOf(123))); assertEquals(Integer.valueOf(456), parseResult.matchedPositionalValue(3, Integer.valueOf(456))); }
@Test public void testPositionalParamSpec_DefaultValue_map_replacedByCommandLineValue() { CommandSpec cmd = CommandSpec.create().add(PositionalParamSpec .builder().defaultValue("1=A,2=B,3=C").splitRegex(",").type(Map.class).auxiliaryTypes(Integer.class, String.class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs("4=X,5=Y,6=Z"); Map<Integer, String> expected = new HashMap<Integer, String>(); expected.put(4, "X"); expected.put(5, "Y"); expected.put(6, "Z"); assertEquals(expected, parseResult.matchedPositionalValue(0, Collections.emptyMap())); }
List<File> files = pr.matchedPositionalValue(0, Collections.<File>emptyList()); for (File f : files) { for (int i = 0; i < count; i++) {
@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 testRawPositionalValueWithDefault_ReturnsDefaultForNonMatchedPosition() { class App { @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 parseResult = cmd.parseArgs("0", "1"); assertEquals(Integer.valueOf(0), parseResult.matchedPositionalValue(0, Integer.valueOf(123))); assertEquals(Integer.valueOf(1), parseResult.matchedPositionalValue(1, Integer.valueOf(456))); assertEquals(Integer.valueOf(123), parseResult.matchedPositionalValue(2, Integer.valueOf(123))); assertEquals(Integer.valueOf(456), parseResult.matchedPositionalValue(3, Integer.valueOf(456))); }
/** Returns the command line argument value of the positional parameter at the specified position, converted to the {@linkplain PositionalParamSpec#type() type} of the positional parameter, or the specified default value if no positional parameter was matched at that position. */ public <T> T matchedPositionalValue(int position, T defaultValue) { return matchedPositionalValue(matchedPositional(position), defaultValue); } /** Returns the command line argument value of the specified positional parameter, converted to the {@linkplain PositionalParamSpec#type() type} of the positional parameter, or the specified default value if the specified positional parameter is {@code null}. */
@Test public void testPositionalParamSpec_DefaultValue_map_replacedByCommandLineValue() { CommandSpec cmd = CommandSpec.create().add(PositionalParamSpec .builder().defaultValue("1=A,2=B,3=C").splitRegex(",").type(Map.class).auxiliaryTypes(Integer.class, String.class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs("4=X,5=Y,6=Z"); Map<Integer, String> expected = new HashMap<Integer, String>(); expected.put(4, "X"); expected.put(5, "Y"); expected.put(6, "Z"); assertEquals(expected, parseResult.matchedPositionalValue(0, Collections.emptyMap())); }
@Test public void testPositionalParamSpec_DefaultValue_list_replacedByCommandLineValue() { CommandSpec cmd = CommandSpec.create().add(PositionalParamSpec .builder().defaultValue("1,2,3").splitRegex(",").type(List.class).auxiliaryTypes(Integer.class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs("4,5,6"); assertEquals(Arrays.asList(4, 5, 6), parseResult.matchedPositionalValue(0, Collections.emptyList())); }
@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 testPositionalParamSpec_DefaultValue_array_replacedByCommandLineValue() { CommandSpec cmd = CommandSpec.create().add(PositionalParamSpec .builder().defaultValue("1,2,3").splitRegex(",").type(int[].class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs("4,5,6"); assertArrayEquals(new int[]{4, 5, 6}, parseResult.matchedPositionalValue(0, new int[0])); }
@Test public void testPositionalParamSpec_DefaultValue_single_replacedByCommandLineValue() { CommandSpec cmd = CommandSpec.create().add(PositionalParamSpec.builder().defaultValue("123").type(int.class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs("456"); assertEquals(Integer.valueOf(456), parseResult.matchedPositionalValue(0, -1)); }
@Test public void testMatchedPositionalValue() { class App { @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; } App app = new App(); CommandLine cmd = new CommandLine(app); ParseResult parseResult = cmd.parseArgs("0", "1"); assertEquals( 0, app.index0); assertEquals( 1, app.index1); assertEquals(-1, app.index2); List<PositionalParamSpec> found = parseResult.matchedPositionals(); assertEquals(2, found.size()); assertEquals(Integer.valueOf(0), parseResult.matchedPositionalValue(0, 0)); assertEquals(Integer.valueOf(1), parseResult.matchedPositionalValue(1, 1)); assertNull(parseResult.matchedPositionalValue(2, null)); }
@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()); }
/** Returns the command line argument value of the positional parameter at the specified position, converted to the {@linkplain PositionalParamSpec#type() type} of the positional parameter, or the specified default value if no positional parameter was matched at that position. */ public <T> T matchedPositionalValue(int position, T defaultValue) { return matchedPositionalValue(matchedPositional(position), defaultValue); } /** Returns the command line argument value of the specified positional parameter, converted to the {@linkplain PositionalParamSpec#type() type} of the positional parameter, or the specified default value if the specified positional parameter is {@code null}. */
@Test public void testPositionalParamSpec_DefaultValue_list_replacedByCommandLineValue() { CommandSpec cmd = CommandSpec.create().add(PositionalParamSpec .builder().defaultValue("1,2,3").splitRegex(",").type(List.class).auxiliaryTypes(Integer.class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs("4,5,6"); assertEquals(Arrays.asList(4, 5, 6), parseResult.matchedPositionalValue(0, Collections.emptyList())); }
@Test public void testPositionalParamSpec_DefaultValue_array_replacedByCommandLineValue() { CommandSpec cmd = CommandSpec.create().add(PositionalParamSpec .builder().defaultValue("1,2,3").splitRegex(",").type(int[].class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs("4,5,6"); assertArrayEquals(new int[]{4, 5, 6}, parseResult.matchedPositionalValue(0, new int[0])); }
@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 testPositionalParamSpec_DefaultValue_single_replacedByCommandLineValue() { CommandSpec cmd = CommandSpec.create().add(PositionalParamSpec.builder().defaultValue("123").type(int.class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs("456"); assertEquals(Integer.valueOf(456), parseResult.matchedPositionalValue(0, -1)); }