@Test public void testMultiValuePositionalParamWithMapWithoutAuxTypes() { CommandSpec spec = CommandSpec.create(); PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(Map.class).build(); assertTrue(positional.isMultiValue()); spec.addPositional(positional); CommandLine commandLine = new CommandLine(spec); commandLine.parse("1=1.0", "2=2.0", "3=3.0"); Map<String, String> expected = new LinkedHashMap<String, String>(); expected.put("1", "1.0"); expected.put("2", "2.0"); expected.put("3", "3.0"); assertEquals(expected, spec.positionalParameters().get(0).getValue()); }
@Test public void testMultiValuePositionalParamWithMapAndAuxTypes() { CommandSpec spec = CommandSpec.create(); PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(Map.class).auxiliaryTypes(Integer.class, Double.class).build(); assertTrue(positional.isMultiValue()); spec.addPositional(positional); CommandLine commandLine = new CommandLine(spec); commandLine.parse("1=1.0", "2=2.0", "3=3.0"); Map<Integer, Double> expected = new LinkedHashMap<Integer, Double>(); expected.put(1, 1.0); expected.put(2, 2.0); expected.put(3, 3.0); assertEquals(expected, spec.positionalParameters().get(0).getValue()); }
assertEquals(Range.valueOf("1..*"), result.matchedPositionals().get(2).index()); assertArrayEquals(args, (String[]) result.matchedPositionals().get(1).getValue()); assertArrayEquals(new String[]{"a", "b"}, (String[]) result.matchedPositionals().get(0).getValue()); assertArrayEquals(new String[]{"b", "c", "d", "e"}, (String[]) result.matchedPositionals().get(2).getValue());
@Test public void testArgSpecGetterWrapNonPicocliException() { final Exception expected = new Exception("boom"); IGetter getter = new IGetter() { public <T> T get() throws Exception { throw expected; } }; PositionalParamSpec positional = PositionalParamSpec.builder().getter(getter).build(); try { positional.getValue(); } catch (CommandLine.PicocliException ex) { assertSame(expected, ex.getCause()); } }
@Test public void testArgSpecGetterRethrowsPicocliException() { final CommandLine.PicocliException expected = new CommandLine.PicocliException("boom"); IGetter getter = new IGetter() { public <T> T get() { throw expected; } }; PositionalParamSpec positional = PositionalParamSpec.builder().getter(getter).build(); try { positional.getValue(); } catch (CommandLine.PicocliException ex) { assertSame(expected, ex); } }
@Test public void testMultiValuePositionalParamWithListWithoutAuxTypes() { CommandSpec spec = CommandSpec.create(); PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(List.class).build(); assertTrue(positional.isMultiValue()); spec.addPositional(positional); CommandLine commandLine = new CommandLine(spec); commandLine.parse("1", "2", "3"); assertEquals(Arrays.asList("1", "2", "3"), spec.positionalParameters().get(0).getValue()); }
@Test public void testMultiValuePositionalParamWithArray() { CommandSpec spec = CommandSpec.create(); PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(int[].class).build(); assertTrue(positional.isMultiValue()); spec.addPositional(positional); CommandLine commandLine = new CommandLine(spec); commandLine.parse("1", "2", "3"); assertArrayEquals(new int[] {1, 2, 3}, (int[]) spec.positionalParameters().get(0).getValue()); }
@Test public void testMultiValuePositionalParamWithListAndAuxTypes() { CommandSpec spec = CommandSpec.create(); PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(List.class).auxiliaryTypes(Integer.class).build(); assertTrue(positional.isMultiValue()); spec.addPositional(positional); CommandLine commandLine = new CommandLine(spec); commandLine.parse("1", "2", "3"); assertEquals(Arrays.asList(1, 2, 3), spec.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 testPositionalConvertersOverridesRegisteredTypeConverter() { CommandSpec spec = CommandSpec.create(); spec.addPositional(PositionalParamSpec.builder().paramLabel("COUNT").index("0").type(int.class).description("number of times to execute").build()); spec.addPositional(PositionalParamSpec.builder().paramLabel("SQLTYPE").index("1").type(int.class).converters( new CommandLineTypeConversionTest.SqlTypeConverter()).description("sql type converter").build()); CommandLine commandLine = new CommandLine(spec); commandLine.parse("33", "BLOB"); assertEquals(Integer.valueOf(33), spec.positionalParameters().get(0).getValue()); assertEquals(Integer.valueOf(Types.BLOB), spec.positionalParameters().get(1).getValue()); }
@Test public void testParseResetsRawAndOriginalStringValues() { CommandSpec spec = CommandSpec.create() .addOption(OptionSpec.builder("-x").type(String.class).build()) .addPositional(PositionalParamSpec.builder().build()); CommandLine cmd = new CommandLine(spec); ParseResult parseResult = cmd.parseArgs("-x", "XVAL", "POSITIONAL"); assertEquals("XVAL", parseResult.matchedOption('x').getValue()); assertEquals(Arrays.asList("XVAL"), parseResult.matchedOption('x').stringValues()); assertEquals(Arrays.asList("XVAL"), parseResult.matchedOption('x').originalStringValues()); assertEquals("POSITIONAL", parseResult.matchedPositional(0).getValue()); assertEquals(Arrays.asList("POSITIONAL"), parseResult.matchedPositional(0).stringValues()); assertEquals(Arrays.asList("POSITIONAL"), parseResult.matchedPositional(0).originalStringValues()); ParseResult parseResult2 = cmd.parseArgs("-x", "222", "$$$$"); assertEquals("222", parseResult2.matchedOption('x').getValue()); assertEquals(Arrays.asList("222"), parseResult2.matchedOption('x').stringValues()); assertEquals(Arrays.asList("222"), parseResult2.matchedOption('x').originalStringValues()); assertEquals("$$$$", parseResult2.matchedPositional(0).getValue()); assertEquals(Arrays.asList("$$$$"), parseResult2.matchedPositional(0).stringValues()); assertEquals(Arrays.asList("$$$$"), parseResult2.matchedPositional(0).originalStringValues()); }
@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 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}. */ @SuppressWarnings("unchecked") private <T> T matchedPositionalValue(PositionalParamSpec positional, T defaultValue) { return positional == null ? defaultValue : (T) positional.getValue(); }
@Test public void testArgSpecGetterWrapNonPicocliException() { final Exception expected = new Exception("boom"); IGetter getter = new IGetter() { public <T> T get() throws Exception { throw expected; } }; PositionalParamSpec positional = PositionalParamSpec.builder().getter(getter).build(); try { positional.getValue(); } catch (CommandLine.PicocliException ex) { assertSame(expected, ex.getCause()); } }
@Test public void testArgSpecGetterRethrowsPicocliException() { final CommandLine.PicocliException expected = new CommandLine.PicocliException("boom"); IGetter getter = new IGetter() { public <T> T get() { throw expected; } }; PositionalParamSpec positional = PositionalParamSpec.builder().getter(getter).build(); try { positional.getValue(); } catch (CommandLine.PicocliException ex) { assertSame(expected, ex); } }
@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 testMultiValuePositionalParamWithListWithoutAuxTypes() { CommandSpec spec = CommandSpec.create(); PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(List.class).build(); assertTrue(positional.isMultiValue()); spec.addPositional(positional); CommandLine commandLine = new CommandLine(spec); commandLine.parse("1", "2", "3"); assertEquals(Arrays.asList("1", "2", "3"), spec.positionalParameters().get(0).getValue()); }
@Test public void testMultiValuePositionalParamWithListAndAuxTypes() { CommandSpec spec = CommandSpec.create(); PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(List.class).auxiliaryTypes(Integer.class).build(); assertTrue(positional.isMultiValue()); spec.addPositional(positional); CommandLine commandLine = new CommandLine(spec); commandLine.parse("1", "2", "3"); assertEquals(Arrays.asList(1, 2, 3), spec.positionalParameters().get(0).getValue()); }
@Test public void testPositionalConvertersOverridesRegisteredTypeConverter() { CommandSpec spec = CommandSpec.create(); spec.addPositional(PositionalParamSpec.builder().paramLabel("COUNT").index("0").type(int.class).description("number of times to execute").build()); spec.addPositional(PositionalParamSpec.builder().paramLabel("SQLTYPE").index("1").type(int.class).converters( new CommandLineTypeConversionTest.SqlTypeConverter()).description("sql type converter").build()); CommandLine commandLine = new CommandLine(spec); commandLine.parse("33", "BLOB"); assertEquals(Integer.valueOf(33), spec.positionalParameters().get(0).getValue()); assertEquals(Integer.valueOf(Types.BLOB), spec.positionalParameters().get(1).getValue()); }
@Test public void testMultiValuePositionalParamWithArray() { CommandSpec spec = CommandSpec.create(); PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(int[].class).build(); assertTrue(positional.isMultiValue()); spec.addPositional(positional); CommandLine commandLine = new CommandLine(spec); commandLine.parse("1", "2", "3"); assertArrayEquals(new int[] {1, 2, 3}, (int[]) spec.positionalParameters().get(0).getValue()); }