private static boolean isMixedIn(OptionSpec option, CommandSpec spec) { for (CommandSpec mixin : spec.mixins().values()) { if (mixin.findOption(option.longestName()) != null) { return true; } } return false; }
@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 testMethodCommandsAreSubcommandsOfAnnotatedClass() { CommandLine cmd = new CommandLine(new AnnotatedClassWithoutAnnotatedFields()); assertNull(cmd.getCommandSpec().findOption('x')); assertEquals(2, cmd.getSubcommands().size()); assertEquals(set("cmd1", "cmd2"), cmd.getSubcommands().keySet()); String expected = String.format("" + "Usage: <main class> [COMMAND]%n" + "Commands:%n" + " cmd1%n" + " cmd2%n"); assertEquals(expected, cmd.getUsageMessage()); }
/** Returns the option with the specified name, or {@code null} if no option with that name was matched on the command line. * <p>Use {@link OptionSpec#getValue() getValue} on the returned {@code OptionSpec} to get the matched value (or values), * converted to the type of the option. Alternatively, use {@link OptionSpec#stringValues() stringValues} * to get the matched String values after they were {@linkplain OptionSpec#splitRegex() split} into parts, or * {@link OptionSpec#originalStringValues() originalStringValues} to get the original String values that were * matched on the command line, before any processing. * </p><p>To get the {@linkplain OptionSpec#defaultValue() default value} of an option that was * {@linkplain #hasMatchedOption(String) <em>not</em> matched} on the command line, use * {@code parseResult.commandSpec().findOption(String).getValue()}. </p> * @see CommandSpec#findOption(String) * @param name used to search the matched options. May be an alias of the option name that was actually specified on the command line. * The specified name may include option name prefix characters or not. */ public OptionSpec matchedOption(String name) { return CommandSpec.findOption(name, matchedOptions); }
/** Returns the option with the specified name, or {@code null} if no option with that name is defined for this command. * @param name used to search the options. May include option name prefix characters or not. */ public OptionSpec findOption(String name) { return findOption(name, options()); }
/** Returns the option with the specified short name, or {@code null} if no option with that name is defined for this command. */ public OptionSpec findOption(char shortName) { return findOption(shortName, options()); } /** Returns the option with the specified name, or {@code null} if no option with that name is defined for this command.
@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 testCompletionCandidatesValues_forOptionSpec() { CommandSpec spec = CommandSpec.create(); spec.add(OptionSpec.builder("-x").completionCandidates(Arrays.asList("x", "y", "z")).build()); assertEquals(Arrays.asList("x", "y", "z"), extract(spec.findOption("x").completionCandidates())); } @Test
@Test public void testCompletionCandidatesEnumValues_forOption() { class App { @Option(names = "-x") MyEfgEnum x; } App app = new App(); CommandLine cmd = new CommandLine(app); assertEquals(Arrays.asList("E", "F", "G"), cmd.getCommandSpec().findOption("x").completionCandidates()); } @Test
@Test public void testCompletionCandidatesPriority_forOption() { class App { @Option(names = "-x", completionCandidates = MyAbcdCandidates.class) MyEfgEnum x; } App app = new App(); CommandLine cmd = new CommandLine(app); assertEquals(Arrays.asList("A", "B", "C", "D"), cmd.getCommandSpec().findOption("x").completionCandidates()); } @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 testBuildMixinForField_valid() { CommandSpec commandSpec = CommandSpec.forAnnotatedObject(new MixeeInstantiated()); assertNotNull(commandSpec.findOption("h")); }
@Test public void testOptionInteractiveReadFromAnnotation() { class App { @Option(names = "-x", interactive = true) int x; @Option(names = "-y", interactive = false) int y; @Option(names = "-z") int z; } CommandLine cmd = new CommandLine(new App()); assertTrue(cmd.getCommandSpec().findOption("x").interactive()); assertFalse(cmd.getCommandSpec().findOption("y").interactive()); assertFalse(cmd.getCommandSpec().findOption("z").interactive()); }
@Test public void testMethodCommandsAreNotSubcommandsOfNonAnnotatedClassWithAnnotatedFields() { CommandLine cmd = new CommandLine(new UnAnnotatedClassWithAnnotatedField()); assertNotNull(cmd.getCommandSpec().findOption('y')); assertTrue(cmd.getSubcommands().isEmpty()); assertNull(cmd.getCommandSpec().findOption('x')); }
@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()); }
/** Returns the option with the specified short name, or {@code null} if no option with that name was matched * on the command line. * <p>Use {@link OptionSpec#getValue() getValue} on the returned {@code OptionSpec} to get the matched value (or values), * converted to the type of the option. Alternatively, use {@link OptionSpec#stringValues() stringValues} * to get the matched String values after they were {@linkplain OptionSpec#splitRegex() split} into parts, or * {@link OptionSpec#originalStringValues() originalStringValues} to get the original String values that were * matched on the command line, before any processing. * </p><p>To get the {@linkplain OptionSpec#defaultValue() default value} of an option that was * {@linkplain #hasMatchedOption(char) <em>not</em> matched} on the command line, use * {@code parseResult.commandSpec().findOption(shortName).getValue()}. </p> * @see CommandSpec#findOption(char) */ public OptionSpec matchedOption(char shortName) { return CommandSpec.findOption(shortName, matchedOptions); }
@Test public void testDefaultCompletionCandidatesNull() { class App { @Option(names = "-x") int x; } CommandLine cmd = new CommandLine(new App()); Iterable<String> candidates = cmd.getCommandSpec().findOption('x').completionCandidates(); assertNull(candidates); }
@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 testCompletionCandidatesValues_forOption() { class App { @Option(names = "-x", completionCandidates = MyAbcdCandidates.class) String x; } CommandLine cmd = new CommandLine(new App()); assertEquals(Arrays.asList("A", "B", "C", "D"), extract(cmd.getCommandSpec().findOption("x").completionCandidates())); } @Test