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 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 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 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 testMultiValues() throws Exception { String[] args = new String[]{"-e", "one", "two", "-f", "1"}; TypedOption<String> e = new TypedOption<String>().setShortName("e") .setMultiValued(true).setType(String.class); TypedOption<Integer> f = new TypedOption<Integer>().setShortName("f") .setMultiValued(true).setType(Integer.class); cli.addOption(e).addOption(f); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat(evaluated.getOptionValues("e")).contains("one", "two").hasSize(2); assertThat(evaluated.getOptionValues("f")).contains(1).hasSize(1); }
@Test public void testPropertiesOption1() throws Exception { String[] args = new String[]{"-Jsource=1.5", "-J", "target", "1.5", "foo"}; TypedOption<String> f = new TypedOption<String>().setShortName("J") .setType(String.class) .setMultiValued(true); cli.addOption(f); CommandLine evaluated = cli.parse(Arrays.asList(args)); List<String> values = evaluated.getOptionValues("J"); assertThat(values).hasSize(4).containsExactly("source=1.5", "target", "1.5", "foo"); }
List<String> classpath = evaluated.getOptionValues("classpath"); assertThat(instances).isEqualTo(4); assertThat(classpath).containsExactly(".", "my.jar");
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 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 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 testMultiValues() throws Exception { String[] args = new String[]{"-e", "one", "two", "-f", "1"}; TypedOption<String> e = new TypedOption<String>().setShortName("e") .setMultiValued(true).setType(String.class); TypedOption<Integer> f = new TypedOption<Integer>().setShortName("f") .setMultiValued(true).setType(Integer.class); cli.addOption(e).addOption(f); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat(evaluated.getOptionValues("e")).contains("one", "two").hasSize(2); assertThat(evaluated.getOptionValues("f")).contains(1).hasSize(1); }
@Test public void testPropertiesOption1() throws Exception { String[] args = new String[]{"-Jsource=1.5", "-J", "target", "1.5", "foo"}; TypedOption<String> f = new TypedOption<String>().setShortName("J") .setType(String.class) .setMultiValued(true); cli.addOption(f); CommandLine evaluated = cli.parse(Arrays.asList(args)); List<String> values = evaluated.getOptionValues("J"); assertThat(values).hasSize(4).containsExactly("source=1.5", "target", "1.5", "foo"); }
@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"); }