@Test public void testClearMapOptionOldValueBeforeParse() { CommandSpec cmd = CommandSpec.create(); Map<String, String> map = new HashMap<String, String>(); map.put("ABC", "XYZ"); cmd.addOption(OptionSpec.builder("-x").type(Map.class).initialValue(map).build()); CommandLine cl = new CommandLine(cmd); cl.parseArgs("-x", "A=1", "-x", "B=2", "-x", "C=3"); Map<String, String> expected = new LinkedHashMap<String, String>(); expected.put("A", "1"); expected.put("B", "2"); expected.put("C", "3"); assertEquals(expected, cmd.findOption("x").getValue()); cl.parseArgs("-x", "D=4", "-x", "E=5"); expected = new LinkedHashMap<String, String>(); expected.put("D", "4"); expected.put("E", "5"); assertEquals(expected, cmd.findOption("x").getValue()); cl.parseArgs(); expected = new LinkedHashMap<String, String>(); assertEquals(map, cmd.findOption("x").getValue()); }
@Test public void testMultiValueOptionWithMapWithoutAuxTypes() { CommandSpec spec = CommandSpec.create(); OptionSpec option = OptionSpec.builder("-c", "--count").arity("3").type(Map.class).build(); assertTrue(option.isMultiValue()); spec.addOption(option); CommandLine commandLine = new CommandLine(spec); commandLine.parse("-c", "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.optionsMap().get("-c").getValue()); }
@Test public void testMultiValueOptionWithMapAndAuxTypes() { CommandSpec spec = CommandSpec.create(); OptionSpec option = OptionSpec.builder("-c", "--count").arity("3").type(Map.class).auxiliaryTypes(Integer.class, Double.class).build(); assertTrue(option.isMultiValue()); spec.addOption(option); CommandLine commandLine = new CommandLine(spec); commandLine.parse("-c", "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.optionsMap().get("-c").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()); }
/** see <a href="https://github.com/remkop/picocli/issues/279">issue #279</a> */ @Test public void testSingleValueFieldWithOptionalParameter_279() { @Command(name="sample") class Sample { @Option(names="--foo", arity="0..1") String foo; } List<CommandLine> parsed1 = new CommandLine(new Sample()).parse();// not specified OptionSpec option1 = parsed1.get(0).getCommandSpec().optionsMap().get("--foo"); assertNull("optional option is null when option not specified", option1.getValue()); assertTrue("optional option has no string value when option not specified", option1.stringValues().isEmpty()); assertTrue("optional option has no typed value when option not specified", option1.typedValues().isEmpty()); List<CommandLine> parsed2 = new CommandLine(new Sample()).parse("--foo");// specified without value OptionSpec option2 = parsed2.get(0).getCommandSpec().optionsMap().get("--foo"); assertEquals("optional option is empty string when specified without args", "", option2.getValue()); assertEquals("optional option string value when specified without args", "", option2.stringValues().get(0)); assertEquals("optional option typed value when specified without args", "", option2.typedValues().get(0)); List<CommandLine> parsed3 = new CommandLine(new Sample()).parse("--foo", "value");// specified with value OptionSpec option3 = parsed3.get(0).getCommandSpec().optionsMap().get("--foo"); assertEquals("optional option is empty string when specified without args", "value", option3.getValue()); assertEquals("optional option string value when specified without args", "value", option3.stringValues().get(0)); assertEquals("optional option typed value when specified without args", "value", option3.typedValues().get(0)); }
/** see <a href="https://github.com/remkop/picocli/issues/279">issue #279</a> */ @Test public void testSingleValueFieldWithOptionalParameterFollowedByOption_279() { @Command(name="sample") class Sample { @Option(names = "-x") boolean x; @Option(names="--foo", arity="0..1") String foo; } Sample sample = new Sample(); List<CommandLine> parsed3 = new CommandLine(sample).parse("--foo", "-x");// specified without value OptionSpec option3 = parsed3.get(0).getCommandSpec().optionsMap().get("--foo"); assertEquals("optional option is empty string when specified without args", "", option3.getValue()); assertEquals("optional option string value when specified without args", "", option3.stringValues().get(0)); assertEquals("optional option typed value when specified without args", "", option3.typedValues().get(0)); assertEquals("", sample.foo); assertEquals(true, sample.x); }
@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 testClearArrayOptionOldValueBeforeParse() { CommandSpec cmd = CommandSpec.create(); cmd.addOption(OptionSpec.builder("-x").arity("2..3").initialValue(new String[] {"ABC"}).build()); CommandLine cl = new CommandLine(cmd); cl.parseArgs("-x", "1", "2", "3"); assertArrayEquals(new String[] {"1", "2", "3"}, (String[]) cmd.findOption("x").getValue()); assertArrayEquals(new String[] {"1", "2", "3"}, (String[]) cmd.findOption('x').getValue()); cl.parseArgs("-x", "4", "5"); assertArrayEquals(new String[] {"4", "5"}, (String[]) cmd.findOption("x").getValue()); assertArrayEquals(new String[] {"4", "5"}, (String[]) cmd.findOption('x').getValue()); cl.parseArgs(); assertArrayEquals(new String[] {"ABC"}, (String[]) cmd.findOption("x").getValue()); assertArrayEquals(new String[] {"ABC"}, (String[]) cmd.findOption('x').getValue()); }
/** Returns the command line argument value of the specified option, converted to the {@linkplain OptionSpec#type() type} of the option, or the specified default value if the specified option is {@code null}. */ @SuppressWarnings("unchecked") private <T> T matchedOptionValue(OptionSpec option, T defaultValue) { return option == null ? defaultValue : (T) option.getValue(); }
@Test public void testDontClearScalarOptionOldValueBeforeParseIfInitialValueFalse() { CommandSpec cmd = CommandSpec.create(); cmd.addOption(OptionSpec.builder("-x").type(String.class).initialValue(null).hasInitialValue(false).build()); CommandLine cl = new CommandLine(cmd); cl.parseArgs("-x", "1"); assertEquals("1", cmd.findOption("x").getValue()); cl.parseArgs("-x", "2"); assertEquals("2", cmd.findOption("x").getValue()); cl.parseArgs(); assertEquals("2", cmd.findOption("x").getValue()); }
@Test public void testOptionConvertersOverridesRegisteredTypeConverter() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-c", "--count").paramLabel("COUNT").arity("1").type(int.class).description("number of times to execute").build()); spec.addOption(OptionSpec.builder("-s", "--sql").paramLabel("SQLTYPE").type(int.class).converters( new CommandLineTypeConversionTest.SqlTypeConverter()).description("sql type converter").build()); CommandLine commandLine = new CommandLine(spec); commandLine.parse("-c", "33", "-s", "BLOB"); assertEquals(Integer.valueOf(33), spec.optionsMap().get("-c").getValue()); assertEquals(Integer.valueOf(Types.BLOB), spec.optionsMap().get("-s").getValue()); } @Test
@Test public void testDontClearListOptionOldValueBeforeParseIfInitialValueFalse() { CommandSpec cmd = CommandSpec.create(); cmd.addOption(OptionSpec.builder("-x").type(List.class).initialValue(Arrays.asList("ABC")).hasInitialValue(false).build()); CommandLine cl = new CommandLine(cmd); cl.parseArgs("-x", "1", "-x", "2", "-x", "3"); assertEquals(Arrays.asList("1", "2", "3"), cmd.findOption("x").getValue()); cl.parseArgs("-x", "4", "-x", "5"); assertEquals(Arrays.asList("4", "5"), cmd.findOption("x").getValue()); cl.parseArgs(); assertEquals(Arrays.asList("4", "5"), cmd.findOption("x").getValue()); }
@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 testMultiValueOptionWithListWithoutAuxTypes() { CommandSpec spec = CommandSpec.create(); OptionSpec option = OptionSpec.builder("-c", "--count").arity("3").type(List.class).build(); assertTrue(option.isMultiValue()); spec.addOption(option); CommandLine commandLine = new CommandLine(spec); commandLine.parse("-c", "1", "2", "3"); assertEquals(Arrays.asList("1", "2", "3"), spec.optionsMap().get("-c").getValue()); }
@Test public void testMultiValueOptionWithListAndAuxTypes() { CommandSpec spec = CommandSpec.create(); OptionSpec option = OptionSpec.builder("-c", "--count").arity("3").type(List.class).auxiliaryTypes(Integer.class).build(); assertTrue(option.isMultiValue()); spec.addOption(option); CommandLine commandLine = new CommandLine(spec); commandLine.parse("-c", "1", "2", "3"); assertEquals(Arrays.asList(1, 2, 3), spec.optionsMap().get("-c").getValue()); }
@Test public void testMultiValueOptionWithArray() { CommandSpec spec = CommandSpec.create(); OptionSpec option = OptionSpec.builder("-c", "--count").arity("3").type(int[].class).build(); assertTrue(option.isMultiValue()); spec.addOption(option); CommandLine commandLine = new CommandLine(spec); commandLine.parse("-c", "1", "2", "3"); assertArrayEquals(new int[] {1, 2, 3}, (int[]) spec.optionsMap().get("-c").getValue()); }
@Test public void testDontClearArrayOptionOldValueBeforeParse() { CommandSpec cmd = CommandSpec.create(); cmd.addOption(OptionSpec.builder("-x").arity("2..3").initialValue(new String[] {"ABC"}).hasInitialValue(false).build()); CommandLine cl = new CommandLine(cmd); cl.parseArgs("-x", "1", "2", "3"); assertArrayEquals(new String[] {"1", "2", "3"}, (String[]) cmd.findOption("x").getValue()); cl.parseArgs("-x", "4", "5"); assertArrayEquals(new String[] {"4", "5"}, (String[]) cmd.findOption("x").getValue()); cl.parseArgs(); assertArrayEquals(new String[] {"4", "5"}, (String[]) cmd.findOption("x").getValue()); }
@Test public void testClearScalarOptionOldValueBeforeParse() { CommandSpec cmd = CommandSpec.create(); cmd.addOption(OptionSpec.builder("-x").type(String.class).initialValue(null).build()); CommandLine cl = new CommandLine(cmd); cl.parseArgs("-x", "1"); assertEquals("1", cmd.findOption("x").getValue()); cl.parseArgs("-x", "2"); assertEquals("2", cmd.findOption("x").getValue()); cl.parseArgs(); assertNull(cmd.findOption("x").getValue()); }
@Test public void testClearListOptionOldValueBeforeParse() { CommandSpec cmd = CommandSpec.create(); cmd.addOption(OptionSpec.builder("-x").type(List.class).initialValue(Arrays.asList("ABC")).build()); CommandLine cl = new CommandLine(cmd); cl.parseArgs("-x", "1", "-x", "2", "-x", "3"); assertEquals(Arrays.asList("1", "2", "3"), cmd.findOption("x").getValue()); cl.parseArgs("-x", "4", "-x", "5"); assertEquals(Arrays.asList("4", "5"), cmd.findOption("x").getValue()); cl.parseArgs(); assertEquals(Arrays.asList("ABC"), cmd.findOption("x").getValue()); }
@Test public void testModelParse() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-h", "--help").usageHelp(true).description("show help and exit").build()); spec.addOption(OptionSpec.builder("-V", "--version").versionHelp(true).description("show help and exit").build()); spec.addOption(OptionSpec.builder("-c", "--count").paramLabel("COUNT").arity("1").type(int.class).description("number of times to execute").build()); CommandLine commandLine = new CommandLine(spec); commandLine.parse("-c", "33"); assertEquals(Integer.valueOf(33), spec.optionsMap().get("-c").getValue()); } // TODO parse method should return an object offering only the options/positionals that were matched