@Test public void testCompleteAllowsNormalValues() { List<CharSequence> candidates = new ArrayList<CharSequence>(); AutoComplete.complete(CommandSpec.create().addOption(OptionSpec.builder("-x").build()), new String[] {"-x"}, 0, 0, 0, candidates); assertFalse(candidates.isEmpty()); }
@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 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_DefaultValue_array_replacedByCommandLineValue() { CommandSpec cmd = CommandSpec.create().addOption(OptionSpec .builder("-x").defaultValue("1,2,3").splitRegex(",").type(int[].class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs("-x", "4,5,6"); assertArrayEquals(new int[]{4, 5, 6}, parseResult.matchedOptionValue('x', new int[0])); }
@Test public void testOptionSpec_DefaultValue_single_replacedByCommandLineValue() { CommandSpec cmd = CommandSpec.create().addOption(OptionSpec.builder("-x").defaultValue("123").type(int.class).build()); ParseResult parseResult = new CommandLine(cmd).parseArgs("-x", "456"); assertEquals(Integer.valueOf(456), parseResult.matchedOptionValue('x', -1)); }
@Test(expected = IllegalArgumentException.class) public void testCompleteDisallowsTooLargePositionInArg() { AutoComplete.complete(CommandSpec.create().addOption(OptionSpec.builder("-x").build()), new String[] {"-x"}, 0, 3, 0, new ArrayList<CharSequence>()); }
@Test(expected = NullPointerException.class) public void testCompleteDisallowsNullArgs() { AutoComplete.complete(CommandSpec.create().addOption(OptionSpec.builder("-x").build()), null, 0, 0, 0, new ArrayList<CharSequence>()); }
@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 testUsageHelp_abbreviateSynopsisWithoutPositional() throws UnsupportedEncodingException { CommandSpec spec = CommandSpec.create(); spec.usageMessage().abbreviateSynopsis(true).requiredOptionMarker('!').sortOptions(false); spec.addOption(OptionSpec.builder("-x").required(true).description("required").build()); CommandLine commandLine = new CommandLine(spec); String actual = usageString(commandLine, Ansi.OFF); String expected = String.format("" + "Usage: <main class> [OPTIONS]%n" + "! -x required%n"); assertEquals(expected, actual); }
@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 testInitializingDefaultsShouldNotAddOptionToParseResult() { CommandSpec spec = CommandSpec.create() .addOption(OptionSpec.builder("-x").type(String.class).defaultValue("xyz").build()); CommandLine cmd = new CommandLine(spec); ParseResult parseResult = cmd.parseArgs(); assertFalse(parseResult.hasMatchedOption('x')); }
@Test public void testBashify() { CommandSpec cmd = CommandSpec.create().addOption( OptionSpec.builder("-x") .type(String.class) .paramLabel("_A\tB C") .completionCandidates(Arrays.asList("1")).build()); String actual = AutoComplete.bash("bashify", new CommandLine(cmd)); String expected = format(loadTextFromClasspath("/bashify_completion.bash"), CommandLine.VERSION); assertEquals(expected, actual); }
@Test public void testRawOptionValueForBooleanOptions_ReturnsStringTrue() { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-V", "--verbose").build()); CommandLine commandLine = new CommandLine(spec); ParseResult pr = commandLine.parseArgs("--verbose"); assertTrue(pr.hasMatchedOption("--verbose")); // as specified on command line assertTrue(pr.hasMatchedOption('V')); // single-character alias works too assertTrue(pr.hasMatchedOption("verbose")); // command name without hyphens assertTrue(pr.matchedOptionValue("verbose", Boolean.FALSE)); assertEquals("true", pr.matchedOption("verbose").stringValues().get(0)); }
@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 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 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 testHelpCreateDetailedSynopsisOptionsText() { Help help = new Help(CommandSpec.create().addOption(OptionSpec.builder("xx").build()), new ColorScheme(Help.Ansi.OFF)); Text text = help.createDetailedSynopsisOptionsText(null, true); assertEquals(" [xx]", text.toString()); }