Search in sources :

Example 1 with Option

use of io.vertx.core.cli.Option in project vert.x by eclipse.

the class DefaultParserTest method testMissingValue.

@Test(expected = MissingValueException.class)
public void testMissingValue() throws CLIException {
    Option[] options = new Option[] { new Option().setShortName("f").setLongName("file").setSingleValued(true) };
    cli.addOptions(Arrays.asList(options));
    cli.parse(Collections.singletonList("--file"));
}
Also used : Option(io.vertx.core.cli.Option) Test(org.junit.Test)

Example 2 with Option

use of io.vertx.core.cli.Option in project vert.x by eclipse.

the class DefaultParserTest method testOptionsWithChoices.

@Test
public void testOptionsWithChoices() {
    CLI cli = new DefaultCLI().setName("test");
    cli.addOption(new Option().setLongName("color").addChoice("red").addChoice("blue").addChoice("green"));
    StringBuilder builder = new StringBuilder();
    cli.usage(builder);
    assertThat(builder).contains(// Usage line
    "[--color {blue, green, red}]").contains(// options
    "  --color {blue, green, red}");
    CommandLine line = cli.parse(Arrays.asList("--color", "blue"));
    assertThat((String) line.getOptionValue("color")).isEqualTo("blue");
    try {
        cli.parse(Collections.singletonList("--color=black"));
        fail("Invalid value expected");
    } catch (InvalidValueException e) {
    // OK
    }
}
Also used : Option(io.vertx.core.cli.Option) Test(org.junit.Test)

Example 3 with Option

use of io.vertx.core.cli.Option in project vert.x by eclipse.

the class DefaultParserTest method testHelpOption.

@Test
public void testHelpOption() {
    CLI cli = new DefaultCLI().setName("test");
    cli.addOption(new Option().setLongName("foo").setRequired(true));
    cli.addOption(new Option().setLongName("help").setShortName("h").setHelp(true).setFlag(true));
    CommandLine line = cli.parse(Collections.singletonList("--foo=bar"));
    assertThat(line.isValid()).isTrue();
    assertThat((String) line.getOptionValue("foo")).isEqualTo("bar");
    assertThat(line.isAskingForHelp()).isFalse();
    line = cli.parse(Arrays.asList("--foo=bar", "-h"));
    assertThat(line.isValid()).isTrue();
    assertThat((String) line.getOptionValue("foo")).isEqualTo("bar");
    assertThat(line.isFlagEnabled("help")).isTrue();
    assertThat(line.isAskingForHelp()).isTrue();
    line = cli.parse(Collections.singletonList("-h"));
    assertThat(line.isValid()).isFalse();
    assertThat(line.isFlagEnabled("help")).isTrue();
    assertThat(line.isAskingForHelp()).isTrue();
    line = cli.parse(Collections.singletonList("-h"), false);
    assertThat(line.isValid()).isFalse();
    assertThat(line.isFlagEnabled("help")).isTrue();
    assertThat(line.isAskingForHelp()).isTrue();
    line = cli.parse(Arrays.asList("--foo=bar", "-h"), false);
    assertThat(line.isValid()).isTrue();
    assertThat((String) line.getOptionValue("foo")).isEqualTo("bar");
    assertThat(line.isFlagEnabled("help")).isTrue();
    assertThat(line.isAskingForHelp()).isTrue();
    try {
        cli.parse(Collections.<String>emptyList());
        fail("Exception expected");
    } catch (MissingOptionException e) {
    // OK
    }
}
Also used : Option(io.vertx.core.cli.Option) Test(org.junit.Test)

Example 4 with Option

use of io.vertx.core.cli.Option in project vert.x by eclipse.

the class DefaultParserTest method testWithOneLongOptionUsingSpace.

@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).startsWith("Usage: test [-f <value>]");
}
Also used : Option(io.vertx.core.cli.Option) Test(org.junit.Test)

Example 5 with Option

use of io.vertx.core.cli.Option in project vert.x by eclipse.

the class DefaultParserTest method testWithOneLongOption.

@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).startsWith("Usage: test [-f <value>]");
    assertThat(usage).contains("-f,--file <value>");
}
Also used : Option(io.vertx.core.cli.Option) Test(org.junit.Test)

Aggregations

Option (io.vertx.core.cli.Option)13 Test (org.junit.Test)11 CLI (io.vertx.core.cli.CLI)2 Argument (io.vertx.core.cli.Argument)1 CommandLine (io.vertx.core.cli.CommandLine)1