@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 testMultiValueOptionArityAloneIsInsufficient() throws Exception { CommandLine.Model.CommandSpec spec = CommandLine.Model.CommandSpec.create(); CommandLine.Model.OptionSpec option = CommandLine.Model.OptionSpec.builder("-c", "--count").arity("3").type(int.class).build(); assertFalse(option.isMultiValue()); spec.addOption(option); spec.parser().collectErrors(true); CommandLine commandLine = new CommandLine(spec); commandLine.parse("-c", "1", "2", "3"); assertEquals(1, commandLine.getParseResult().errors().size()); assertEquals("Unmatched arguments: 2, 3", commandLine.getParseResult().errors().get(0).getMessage()); }
@Test public void testModelUsageHelpWithCustomSeparator() { 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()); spec.addOption(OptionSpec.builder("-f", "--fix").paramLabel("FIXED(=BOOLEAN)").arity("1").hideParamSyntax(true).required(true).description("run with fixed option").build()); CommandLine commandLine = new CommandLine(spec).setSeparator(" "); String actual = usageString(commandLine, Ansi.OFF); String expected = String.format("" + "Usage: <main class> [-hV] [-c COUNT] -f FIXED(=BOOLEAN)%n" + " -c, --count COUNT number of times to execute%n" + " -f, --fix FIXED(=BOOLEAN)%n" + " run with fixed option%n" + " -h, --help show help and exit%n" + " -V, --version show help and exit%n"); assertEquals(expected, actual); }
@Test public void testModelUsageHelp() { 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()); spec.addOption(OptionSpec.builder("-f", "--fix").paramLabel("FIXED(BOOLEAN)").arity("1").hideParamSyntax(true).required(true).description("run with fixed option").build()); CommandLine commandLine = new CommandLine(spec); String actual = usageString(commandLine, Ansi.OFF); String expected = String.format("" + "Usage: <main class> [-hV] [-c=COUNT] -f=FIXED(BOOLEAN)%n" + " -c, --count=COUNT number of times to execute%n" + " -f, --fix=FIXED(BOOLEAN) run with fixed option%n" + " -h, --help show help and exit%n" + " -V, --version show help and exit%n"); assertEquals(expected, actual); }
@Test public void testMultiValueOptionArityAloneIsInsufficient() { CommandSpec spec = CommandSpec.create(); OptionSpec option = OptionSpec.builder("-c", "--count").arity("3").type(int.class).build(); assertFalse(option.isMultiValue()); spec.addOption(option); CommandLine commandLine = new CommandLine(spec); try { commandLine.parse("-c", "1", "2", "3"); fail("Expected exception"); } catch (UnmatchedArgumentException ex) { assertEquals("Unmatched arguments: 2, 3", ex.getMessage()); } }
@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()); }
@Test public void testOptionDefaultTypeIsStringArray_withArityTwo() { assertEquals(String[].class, OptionSpec.builder("-x").arity("2").build().type()); }
@Test public void testOptionDefaultTypeIsString_withArityOne() { assertEquals(String.class, OptionSpec.builder("-x").arity("1").build().type()); }
@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 testOptionCopyBuilder() { OptionSpec option = OptionSpec.builder("-a", "--aaa").arity("1").type(int.class).description("abc").paramLabel("ABC").build(); OptionSpec copy = option.toBuilder().build(); assertEquals(option, copy); assertNotSame(option, copy); }
@Test public void testOptionAuxiliaryTypeOverridesDefaultType() { assertEquals(int.class, OptionSpec.builder("-x").auxiliaryTypes(int.class).build().type()); assertEquals(int.class, OptionSpec.builder("-x").arity("0").auxiliaryTypes(int.class).build().type()); assertEquals(int.class, OptionSpec.builder("-x").arity("1").auxiliaryTypes(int.class).build().type()); assertEquals(int.class, OptionSpec.builder("-x").arity("0..1").auxiliaryTypes(int.class).build().type()); assertEquals(int.class, OptionSpec.builder("-x").arity("2").auxiliaryTypes(int.class).build().type()); assertEquals(int.class, OptionSpec.builder("-x").arity("0..2").auxiliaryTypes(int.class).build().type()); assertEquals(int.class, OptionSpec.builder("-x").arity("*").auxiliaryTypes(int.class).build().type()); }
@Test public void testOptionDefaultTypDependsOnArity() { assertEquals(boolean.class, OptionSpec.builder("-x").arity("0").build().type()); assertEquals(String.class, OptionSpec.builder("-x").arity("1").build().type()); assertEquals(String.class, OptionSpec.builder("-x").arity("0..1").build().type()); assertEquals(String[].class, OptionSpec.builder("-x").arity("2").build().type()); assertEquals(String[].class, OptionSpec.builder("-x").arity("0..2").build().type()); assertEquals(String[].class, OptionSpec.builder("-x").arity("*").build().type()); }
@Test public void testOptionDefaultTypeIsBoolean_withArityZero() { assertEquals(boolean.class, OptionSpec.builder("-x").arity("0").build().type()); }
@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 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
@Test public void testOptionInteractiveIfSet() { assertTrue(OptionSpec.builder("-x").interactive(true).interactive()); assertTrue(OptionSpec.builder("-x").arity("1").interactive(true).build().interactive()); }
@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()); }