/** * @return the {@link CLI}. */ public CLI cli() { return commandLine.cli(); }
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); }
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 testWithOneLongOption() throws CLIException { Option[] options = new Option[]{ new Option().setShortName("f").setLongName("file").setSingleValued(true) }; cli.addOptions(Arrays.asList(options)); CommandLine evaluated = cli.parse(Collections.singletonList("--file=hello.txt")); assertThat(evaluated.cli().getOptions()).hasSize(1); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); assertThat(evaluated.getOptionValues("f")).containsExactly("hello.txt"); StringBuilder usage = new StringBuilder(); cli.usage(usage); assertThat(usage.toString()).startsWith("Usage: test [-f <value>]"); assertThat(usage.toString()).contains("-f,--file <value>"); }
@Test public void testQuotedValues() throws CLIException { Option[] options = new Option[]{ new Option().setShortName("f").setLongName("file").setSingleValued(true) }; cli.addOptions(Arrays.asList(options)); CommandLine evaluated = cli.parse(Arrays.asList("--file", "\"hello.txt\"")); assertThat(evaluated.cli().getOptions()).hasSize(1); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); assertThat(evaluated.getOptionValues("f")).containsExactly("hello.txt"); }
@Test public void testWithOneShortOption() throws CLIException { Option[] options = new Option[]{ new Option().setShortName("f").setLongName("file").setSingleValued(true) }; cli.addOptions(Arrays.asList(options)); CommandLine evaluated = cli.parse(Collections.singletonList("-f=hello.txt")); assertThat(evaluated.cli().getOptions()).hasSize(1); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); assertThat(evaluated.getOptionValues("f")).containsExactly("hello.txt"); }
@Test public void testWithOneShortOptionUsingSpace() throws CLIException { Option[] options = new Option[]{ new Option().setShortName("f").setLongName("file").setSingleValued(true) }; cli.addOptions(Arrays.asList(options)); CommandLine evaluated = cli.parse(Arrays.asList("-f", "hello.txt")); assertThat(evaluated.cli().getOptions()).hasSize(1); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); assertThat(evaluated.getOptionValues("f")).containsExactly("hello.txt"); }
@Test public void testWithOneLongOptionUsingSpace() throws CLIException { Option[] options = new Option[]{ new Option().setShortName("f").setLongName("file") .setSingleValued(true) }; cli.addOptions(Arrays.asList(options)); CommandLine evaluated = cli.parse(Arrays.asList("--file", "hello.txt")); assertThat(evaluated.cli().getOptions()).hasSize(1); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); assertThat(evaluated.getOptionValues("f")).containsExactly("hello.txt"); StringBuilder usage = new StringBuilder(); cli.usage(usage); assertThat(usage.toString()).startsWith("Usage: test [-f <value>]"); }
@Test public void testWithMultipleValues() throws CLIException { Option[] options = new Option[]{ new Option().setShortName("f").setLongName("file") .setMultiValued(true) }; cli.addOptions(Arrays.asList(options)); CommandLine evaluated = cli.parse(Arrays.asList("-f=hello.txt", "--file=hello2.txt")); assertThat(evaluated.cli().getOptions()).hasSize(1); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); assertThat(evaluated.getOptionValues("f")) .containsExactly("hello.txt", "hello2.txt"); }
@Test public void testWithList() throws CLIException { CLI cli = new DefaultCLI().setName("test"); Option[] options = new Option[]{ new TypedOption<String>().setShortName("f").setLongName("file") .setParsedAsList(true).setType(String.class) }; cli.addOptions(Arrays.asList(options)); CommandLine evaluated = cli.parse(Collections.singletonList("-f=hello.txt,hello2.txt")); assertThat(evaluated.cli().getOptions()).hasSize(1); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt,hello2.txt"); assertThat(evaluated.getOptionValues("f")) .containsExactly("hello.txt", "hello2.txt"); }
@Test public void testNegativeNumbers() throws CLIException { CLI cli = new DefaultCLI().setName("test"); Option[] options = new Option[]{ new TypedOption<Double>().setLongName("num").setSingleValued(true) .setType(Double.class) }; cli.addOptions(Arrays.asList(options)); CommandLine evaluated = cli.parse(Arrays.asList("--num", "-1.5")); assertThat(evaluated.cli().getOptions()).hasSize(1); assertThat(cli.getArguments()).isEmpty(); assertThat((double) evaluated.getOptionValue("num")).isEqualTo(-1.5d); evaluated = cli.parse(Collections.singletonList("--num=-1.5")); assertThat(evaluated.cli().getOptions()).hasSize(1); assertThat(cli.getArguments()).isEmpty(); assertThat((double) evaluated.getOptionValue("num")).isEqualTo(-1.5d); }
@Test public void testWithDashD() throws CLIException { CLI cli = new DefaultCLI().setName("test"); Option[] options = new Option[]{ new TypedOption<String>().setShortName("D").setLongName("systemProperty") .setMultiValued(true).setType(String.class), new TypedOption<Boolean>().setShortName("F").setLongName("flag") .setFlag(true) .setType(Boolean.class) }; cli.addOptions(Arrays.asList(options)); CommandLine evaluated = cli.parse(Arrays.asList("-Dx=y", "-F")); assertThat(evaluated.cli().getOptions()).hasSize(2); assertThat(evaluated.getRawValueForOption(evaluated.cli().getOption("systemProperty"))) .isEqualTo("x=y"); assertThat((boolean) evaluated.getOptionValue("flag")).isTrue(); }
/** * @return the {@link CLI}. */ public CLI cli() { return commandLine.cli(); }
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); }
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 testWithOneShortOptionUsingSpace() throws CLIException { Option[] options = new Option[]{ new Option().setShortName("f").setLongName("file").setSingleValued(true) }; cli.addOptions(Arrays.asList(options)); CommandLine evaluated = cli.parse(Arrays.asList("-f", "hello.txt")); assertThat(evaluated.cli().getOptions()).hasSize(1); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); assertThat(evaluated.getOptionValues("f")).containsExactly("hello.txt"); }
@Test public void testWithOneShortOption() throws CLIException { Option[] options = new Option[]{ new Option().setShortName("f").setLongName("file").setSingleValued(true) }; cli.addOptions(Arrays.asList(options)); CommandLine evaluated = cli.parse(Collections.singletonList("-f=hello.txt")); assertThat(evaluated.cli().getOptions()).hasSize(1); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); assertThat(evaluated.getOptionValues("f")).containsExactly("hello.txt"); }
@Test public void testQuotedValues() throws CLIException { Option[] options = new Option[]{ new Option().setShortName("f").setLongName("file").setSingleValued(true) }; cli.addOptions(Arrays.asList(options)); CommandLine evaluated = cli.parse(Arrays.asList("--file", "\"hello.txt\"")); assertThat(evaluated.cli().getOptions()).hasSize(1); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); assertThat(evaluated.getOptionValues("f")).containsExactly("hello.txt"); }
@Test public void testWithMultipleValues() throws CLIException { Option[] options = new Option[]{ new Option().setShortName("f").setLongName("file") .setMultiValued(true) }; cli.addOptions(Arrays.asList(options)); CommandLine evaluated = cli.parse(Arrays.asList("-f=hello.txt", "--file=hello2.txt")); assertThat(evaluated.cli().getOptions()).hasSize(1); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); assertThat(evaluated.getOptionValues("f")) .containsExactly("hello.txt", "hello2.txt"); }
@Test public void testWithList() throws CLIException { CLI cli = new DefaultCLI().setName("test"); Option[] options = new Option[]{ new TypedOption<String>().setShortName("f").setLongName("file") .setParsedAsList(true).setType(String.class) }; cli.addOptions(Arrays.asList(options)); CommandLine evaluated = cli.parse(Collections.singletonList("-f=hello.txt,hello2.txt")); assertThat(evaluated.cli().getOptions()).hasSize(1); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt,hello2.txt"); assertThat(evaluated.getOptionValues("f")) .containsExactly("hello.txt", "hello2.txt"); }