Search in sources :

Example 11 with Option

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

the class CLIExamples method example9.

public void example9(PrintStream stream) {
    CLI cli = CLI.create("test").addOption(new Option().setLongName("help").setShortName("h").setFlag(true).setHelp(true)).addOption(new Option().setLongName("mandatory").setRequired(true));
    CommandLine line = cli.parse(Collections.singletonList("-h"));
    // The parsing does not fail and let you do:
    if (!line.isValid() && line.isAskingForHelp()) {
        StringBuilder builder = new StringBuilder();
        cli.usage(builder);
        stream.print(builder.toString());
    }
}
Also used : CLI(io.vertx.core.cli.CLI) CommandLine(io.vertx.core.cli.CommandLine) Option(io.vertx.core.cli.Option)

Example 12 with Option

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

the class DefaultParserTest method testWithOneShortOptionUsingSpace.

@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");
}
Also used : Option(io.vertx.core.cli.Option) Test(org.junit.Test)

Example 13 with Option

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

the class DefaultParserTest method testWithMultipleValues.

@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");
}
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