@Test public void testArgumentWithDefaultValue() throws CLIException { CommandWithDefaultValueOnArgument command = new CommandWithDefaultValueOnArgument(); CLI cli = CLIConfigurator.define(command.getClass()).setName("test"); CommandLine evaluatedCLI = parse(cli, "foo"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.x.get()).isEqualTo("foo"); assertThat(command.y.get()).isEqualTo(25); }
cli.addOption(createOption(method)); cli.addArgument(createArgument(method));
private static Object getOptionValue(Method method, String name, CommandLine commandLine) { final io.vertx.core.cli.Option option = commandLine.cli().getOption(name); if (option == null) { return null; } boolean multiple = ReflectionUtils.isMultiple(method); if (multiple) { return createMultiValueContainer(method, commandLine.getOptionValues(name)); } return commandLine.getOptionValue(name); }
Object injected = getOptionValue(method, name, cli); if (injected != null) { method.setAccessible(true); int index = argument.index(); try { Object injected = getArgumentValue(method, index, cli); if (injected != null) { method.setAccessible(true);
/** * @return the {@link CLI} instance by reading the annotation. */ @Override public CLI define() { return CLIConfigurator.define(clazz); } }
/** * @return a new instance of the command by invoking the default constructor of the given class. */ @Override public C create(CommandLine cl) { C c = ReflectionUtils.newInstance(clazz); CLIConfigurator.inject(cl, c); return c; }
/** * Creates an instance of {@link CLI} from the given Java class. It instantiates the {@link CLI} object from the * annotations used in the class. * * @param clazz the annotated class * @return the created instance of {@link CLI} */ @GenIgnore static CLI create(Class<?> clazz) { return CLIConfigurator.define(clazz); }
@Test public void testAnnotatedClassWithArgumentReceivingMultipleValues() { CLI cli = CLI.create(CLIUsingMultipleArgument.class); CLIUsingMultipleArgument instance = new CLIUsingMultipleArgument(); CommandLine cl = cli.parse(Arrays.asList("a", "b", "-s=1", "-s=2")); CLIConfigurator.inject(cl, instance); }
Object injected = getOptionValue(method, name, cli); if (injected != null) { method.setAccessible(true); int index = argument.index(); try { Object injected = getArgumentValue(method, index, cli); if (injected != null) { method.setAccessible(true);
@Test public void testArgumentInjectionWithConvertedByAndDefaultValue() throws CLIException { CommandForConvertedValueTest command = new CommandForConvertedValueTest(); CLI cli = CLIConfigurator.define(command.getClass()).setName("test"); CommandLine evaluatedCLI = parse(cli, "Bob,Morane"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.reference.get().first).isEqualTo("Bob"); assertThat(command.reference.get().last).isEqualTo("Morane"); evaluatedCLI = parse(cli); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.reference.get().first).isEqualTo("Bill"); assertThat(command.reference.get().last).isEqualTo("Balantine"); }
@Test(expected = IllegalArgumentException.class) public void testThatCLINeedsAName() { CLIConfigurator.define(MyCommandWithoutName.class); }
CLIConfigurator.inject(evaluated, cmd);
cli.addOption(createOption(method)); cli.addArgument(createArgument(method));
private static Object getArgumentValue(Method method, int index, CommandLine commandLine) { final io.vertx.core.cli.Argument argument = commandLine.cli().getArgument(index); if (argument == null) { return null; } boolean multiple = ReflectionUtils.isMultiple(method); if (multiple) { return createMultiValueContainer(method, commandLine.getArgumentValues(argument.getIndex())); } return commandLine.getArgumentValue(argument.getIndex()); }
@Test public void testArgumentInjection() throws CLIException { CommandForArgumentInjectionTest command = new CommandForArgumentInjectionTest(); CLI cli = CLIConfigurator.define(command.getClass()).setName("test"); CommandLine evaluatedCLI = parse(cli, "foo"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.reference.get()).isEqualTo("foo"); }
@Test(expected = IllegalArgumentException.class) public void testThatCLINeedsANonEmptyName() { CLIConfigurator.define(MyCommandWithoutEmptyName.class); }
/** * @return a new instance of the command by invoking the default constructor of the given class. */ @Override public C create(CommandLine cl) { C c = ReflectionUtils.newInstance(clazz); CLIConfigurator.inject(cl, c); return c; }
private static Object getOptionValue(Method method, String name, CommandLine commandLine) { final io.vertx.core.cli.Option option = commandLine.cli().getOption(name); if (option == null) { return null; } boolean multiple = ReflectionUtils.isMultiple(method); if (multiple) { return createMultiValueContainer(method, commandLine.getOptionValues(name)); } return commandLine.getOptionValue(name); }
@Test public void testArgumentInjectionWithSeveralArguments() throws CLIException { CommandForMultipleArgumentTest command = new CommandForMultipleArgumentTest(); CLI cli = CLIConfigurator.define(command.getClass()).setName("test"); CommandLine evaluatedCLI = parse(cli, "foo", "1"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.x.get()).isEqualTo("foo"); assertThat(command.y.get()).isEqualTo(1); }
@Test public void testOptionsWithChoicesUsingAnnotations() { CLI cli = CLIConfigurator.define(CLIUsingAEnumOption.class); StringBuilder builder = new StringBuilder(); cli.usage(builder); assertThat(builder.toString()) .contains("[--retention {CLASS, RUNTIME, SOURCE}]") // Usage line .contains(" --retention {CLASS, RUNTIME, SOURCE}"); // options CommandLine line = cli.parse(Arrays.asList("--retention", "CLASS", "--foo", "bar")); assertThat((RetentionPolicy) line.getOptionValue("retention")).isEqualTo(RetentionPolicy.CLASS); try { cli.parse(Collections.singletonList("--retention=nope")); fail("Invalid value expected"); } catch (InvalidValueException e) { // OK } }