private boolean getBooleanOption(CommandLine evaluatedCLI, String name) { return evaluatedCLI.getOptionValue(name); }
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 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"); }
private String getStringOption(CommandLine evaluatedCLI, String name) { return evaluatedCLI.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>"); }
/** * Executes the command. * * @throws CLIException If anything went wrong. */ @Override public void run() throws CLIException { final String n = executionContext.commandLine().getOptionValue("n"); out.println("Good Bye " + n); } }
@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 testTheDifferentFormatForShortOption() 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((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); evaluated = cli.parse(Collections.singletonList("-f=hello.txt")); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); evaluated = cli.parse(Collections.singletonList("-fhello.txt")); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("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 testTheDifferentFormatForLongOption() 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((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); evaluated = cli.parse(Collections.singletonList("--file=hello.txt")); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); evaluated = cli.parse(Collections.singletonList("-filehello.txt")); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); evaluated = cli.parse(Arrays.asList("--FILE", "hello.txt")); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("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 testPartialLongOptionSingleDash() throws Exception { String[] args = new String[]{"-ver"}; TypedOption<Boolean> v2 = new TypedOption<Boolean>().setLongName("version").setType(Boolean.class) .setSingleValued(false); TypedOption<Integer> v1 = new TypedOption<Integer>().setSingleValued(true) .setShortName("v") .setType(Integer.class); cli.addOption(v1).addOption(v2); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat((Object) evaluated.getOptionValue("v")).isNull(); assertThat((boolean) evaluated.getOptionValue("version")).isTrue(); }
@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 testAmbiguousLongWithoutEqualSingleDash() throws Exception { String[] args = new String[]{"-b", "-foobar"}; TypedOption<String> f = new TypedOption<String>().setType(String.class).setLongName("foo").setShortName("f").setSingleValued(true); TypedOption<Boolean> b = new TypedOption<Boolean>().setType(Boolean.class).setLongName("bar").setShortName("b") .setFlag(true); cli.removeOption("b").addOption(f).addOption(b); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat((boolean) evaluated.getOptionValue("bar")).isTrue(); assertThat((String) evaluated.getOptionValue("foo")).isEqualTo("bar"); }
@Test public void testWithRequiredOption() throws Exception { String[] args = new String[]{"-b", "file"}; TypedOption<String> b = new TypedOption<String>().setShortName("b").setLongName("bfile").setSingleValued(true) .setDescription("set the value of [b]").setType(String.class).setRequired(true); cli.removeOption("b").addOption(b); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat(getBooleanOption(evaluated, "a")).isFalse(); assertThat((String) evaluated.getOptionValue("b")).isEqualTo("file"); assertThat(evaluated.allArguments()).isEmpty(); }
@Test public void testFlag() { final CLI cli = CLI.create("test") .addOption(new Option().setShortName("f").setFlag(true)) .addOption(new Option().setShortName("x")); final CommandLine evaluated = cli.parse(Arrays.asList("-f", "-x", "foo")); assertThat(evaluated.isFlagEnabled("f")).isTrue(); assertThat((String) evaluated.getOptionValue("x")).isEqualToIgnoringCase("foo"); }
@Test public void testMissingFlag() { final CLI cli = CLI.create("test") .addOption(new Option().setShortName("f").setFlag(true)) .addOption(new Option().setShortName("x")); final CommandLine evaluated = cli.parse(Arrays.asList("-x", "foo")); assertThat(evaluated.isFlagEnabled("f")).isFalse(); assertThat((String) evaluated.getOptionValue("x")).isEqualToIgnoringCase("foo"); }
@Test public void testLongWithoutEqualDoubleDash() throws Exception { String[] args = new String[]{"--foobar"}; TypedOption<String> f = new TypedOption<String>() .setType(String.class).setLongName("foo").setShortName("f").setSingleValued(true); cli.addOption(f); CommandLine evaluated = cli.parse(Arrays.asList(args)); // foo isn't expected to be recognized with a double dash assertThat((String) evaluated.getOptionValue("foo")).isNull(); }
@Test public void testUnambiguousPartialLongOption3() throws Exception { String[] args = new String[]{"--ver=1"}; TypedOption<Integer> v = new TypedOption<Integer>().setLongName("verbose") .setSingleValued(true) .setType(Integer.class); TypedOption<Boolean> h = new TypedOption<Boolean>().setLongName("help").setType(Boolean.class) .setSingleValued(false); cli.addOption(v).addOption(h); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat((int) evaluated.getOptionValue("verbose")).isEqualTo(1); }
@Test public void testUnambiguousPartialLongOption4() throws Exception { String[] args = new String[]{"-ver=1"}; TypedOption<Integer> v = new TypedOption<Integer>().setLongName("verbose") .setSingleValued(true) .setType(Integer.class); TypedOption<Boolean> h = new TypedOption<Boolean>().setLongName("help") .setType(Boolean.class) .setSingleValued(false); cli.addOption(v).addOption(h); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat((int) evaluated.getOptionValue("verbose")).isEqualTo(1); }