@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(); }
private CommandLine parse(CLI cli, String... args) throws CLIException { return cli.parse(Arrays.asList(args)); }