@Test
  public void testOptionsWithDescription() {
    CLI cli = CLIConfigurator.define(CommandForDescriptionTest.class);

    assertThat(cli.getOptions()).hasSize(1);
    assertThat(find(cli.getOptions(), "option").getDescription())
        .isEqualTo("This option is awesome");
  }
  @Test
  public void testOptionsWithDefaultValue() {
    CLI cli = CLIConfigurator.define(CommandForDefaultValueTest.class);

    assertThat(cli.getOptions()).hasSize(1);
    assertThat(find(cli.getOptions(), "option").getDefaultValue()).isEqualTo("bar");
    assertThat(find(cli.getOptions(), "option").getName()).isEqualTo("option");
  }
  @Test
  public void testInjectionOfString() throws CLIException {
    HelloClI command = new HelloClI();
    CLI cli = CLIConfigurator.define(HelloClI.class);
    CommandLine evaluatedCLI = cli.parse(Arrays.asList("--name", "vert.x"));
    CLIConfigurator.inject(evaluatedCLI, command);

    assertThat(command.run()).isEqualToIgnoringCase("Hello vert.x");
    assertThat(command.name).isEqualToIgnoringCase("vert.x");
  }
 @Test
 public void testOptionsParsedAsList() {
   CLI command = CLIConfigurator.define(CommandForParsedAsList.class);
   assertThat(command.getOptions()).hasSize(1);
   assertThat(((TypedOption) find(command.getOptions(), "option")).getListSeparator())
       .isEqualTo(":");
   assertThat(find(command.getOptions(), "option").isMultiValued()).isTrue();
   assertThat(((TypedOption) find(command.getOptions(), "option")).getType())
       .isEqualTo(String.class);
 }
 @Test
 public void testUsage() {
   CLI command = CLIConfigurator.define(HelloClI.class);
   StringBuilder builder = new StringBuilder();
   command.usage(builder);
   assertThat(builder)
       .containsIgnoringCase("Usage: hello -n <name>")
       .containsIgnoringCase("A command saying hello.")
       .containsIgnoringCase("A simple cli to wish you a good day. Pass your name with `--name`")
       .containsIgnoringCase(" -n,--name <name>   your name");
 }
  @Test
  public void testTypeExtraction() {
    CLI command = CLIConfigurator.define(CommandForTypeExtractTest.class);

    assertThat(command.getOptions()).hasSize(6);
    TypedOption model = (TypedOption) find(command.getOptions(), "list");
    assertThat(model.getType()).isEqualTo(String.class);
    assertThat(model.isMultiValued()).isTrue();

    model = (TypedOption) find(command.getOptions(), "set");
    assertThat(model.getType()).isEqualTo(Character.class);
    assertThat(model.isMultiValued()).isTrue();

    model = (TypedOption) find(command.getOptions(), "collection");
    assertThat(model.getType()).isEqualTo(Integer.class);
    assertThat(model.isMultiValued()).isTrue();

    model = (TypedOption) find(command.getOptions(), "tree");
    assertThat(model.getType()).isEqualTo(String.class);
    assertThat(model.isMultiValued()).isTrue();

    model = (TypedOption) find(command.getOptions(), "al");
    assertThat(model.getType()).isEqualTo(String.class);
    assertThat(model.isMultiValued()).isTrue();

    model = (TypedOption) find(command.getOptions(), "array");
    assertThat(model.getType()).isEqualTo(Integer.TYPE);
    assertThat(model.isMultiValued()).isTrue();
  }
  @Test
  public void testHelloCLIFromClass() {
    CLI command = CLIConfigurator.define(HelloClI.class);

    assertThat(command.getOptions()).hasSize(1);
    TypedOption option = (TypedOption) find(command.getOptions(), "name");
    assertThat(option.getLongName()).isEqualToIgnoringCase("name");
    assertThat(option.getShortName()).isEqualToIgnoringCase("n");
    assertThat(option.getType()).isEqualTo(String.class);
    assertThat(option.getArgName()).isEqualTo("name");
    assertThat(option.getDescription()).isEqualToIgnoringCase("your name");
    assertThat(option.getDefaultValue()).isNull();
    assertThat(option.acceptValue()).isTrue();
    assertThat(option.isMultiValued()).isFalse();
    assertThat(option.isRequired()).isTrue();
  }
 @Override
 public <T> T getArgumentValue(String name) {
   Argument arg = cli.getArgument(name);
   if (arg == null) {
     return null;
   }
   return getArgumentValue(arg.getIndex());
 }
 /**
  * Checks whether or not the user has passed a "help" option and is asking for help.
  *
  * @return {@code true} if the user command line has enabled a "Help" option, {@link false}
  *     otherwise.
  */
 @Override
 public boolean isAskingForHelp() {
   for (Option option : cli.getOptions()) {
     if (option.isHelp() && isSeenInCommandLine(option)) {
       return true;
     }
   }
   return false;
 }
 @Override
 public boolean isFlagEnabled(String name) {
   Option option = cli.getOption(name);
   if (option == null) {
     throw new IllegalArgumentException("Cannot find the option '" + name + "'");
   }
   if (option.isFlag()) {
     return optionsSeenInCommandLine.contains(option);
   } else {
     throw new IllegalStateException(
         "Cannot retrieve the flag value on a non-flag option (" + name + ")");
   }
 }
 @Override
 @SuppressWarnings("unchecked")
 public <T> T getOptionValue(String name) {
   Option option = cli.getOption(name);
   if (option == null) {
     return null;
   }
   if (option instanceof TypedOption) {
     return getValue((TypedOption<T>) option);
   } else {
     return (T) getRawValueForOption(option);
   }
 }
 @Override
 @SuppressWarnings("unchecked")
 public <T> T getArgumentValue(int index) {
   Argument arg = cli.getArgument(index);
   if (arg == null) {
     return null;
   }
   if (arg instanceof TypedArgument) {
     return create(getRawValueForArgument(arg), (TypedArgument<T>) arg);
   } else {
     return (T) getRawValueForArgument(arg);
   }
 }
 /**
  * Gets the values of an argument with the matching index.
  *
  * @param index the index
  * @return the values, {@code null} if not set
  * @see #getArgumentValue(int)
  * @see #getRawValueForArgument(Argument)
  */
 @Override
 @SuppressWarnings("unchecked")
 public <T> List<T> getArgumentValues(int index) {
   Argument argument = cli.getArgument(index);
   if (argument == null) {
     return null;
   }
   if (argument instanceof TypedArgument) {
     TypedArgument<T> typed = (TypedArgument<T>) argument;
     return getRawValuesForArgument(typed)
         .stream()
         .map(s -> create(s, typed))
         .collect(Collectors.toList());
   } else {
     return (List<T>) getRawValuesForArgument(argument);
   }
 }
 @Override
 @SuppressWarnings("unchecked")
 public <T> List<T> getOptionValues(String name) {
   Option option = cli.getOption(name);
   if (option == null) {
     return null;
   }
   if (option instanceof TypedOption) {
     TypedOption<T> typed = (TypedOption<T>) option;
     if (typed.isParsedAsList()) {
       return createFromList(getRawValueForOption(option), typed);
     } else {
       return getRawValuesForOption(option)
           .stream()
           .map(s -> create(s, typed))
           .collect(Collectors.toList());
     }
   } else {
     return (List<T>) getRawValuesForOption(option);
   }
 }
Beispiel #15
0
 @DocAnnotation$annotation$(
     description =
         " Creates an instance of [CLI](../cli/CLI.type.html) using the default implementation.\n")
 @TypeInfo("io.vertx.ceylon.core.cli::CLI")
 public CLI create(
     final @TypeInfo("ceylon.language::String") @Name("name") @DocAnnotation$annotation$(
             description = "the name of the CLI (must not be <code>null</code>)\n") ceylon.language
                 .String
             name) {
   java.lang.String arg_0 = io.vertx.lang.ceylon.ToJava.String.safeConvert(name);
   CLI ret =
       io.vertx
           .ceylon
           .core
           .cli
           .CLI
           .TO_CEYLON
           .converter()
           .safeConvert(io.vertx.core.cli.CLI.create(arg_0));
   return ret;
 }
 private CommandLine parse(CLI cli, String... args) throws CLIException {
   return cli.parse(Arrays.asList(args));
 }