@Test public void testThatDefaultValuesAreHandled() throws CLIException { cli.addArgument(new TypedArgument<String>().setIndex(0).setArgName("1").setType(String.class) .setDefaultValue("hello").setRequired(false)); evaluated = cli.parse(Collections.singletonList("a")); assertThat((String) evaluated.getArgumentValue(0)).isEqualTo("a"); evaluated = cli.parse(Collections.emptyList()); assertThat((String) evaluated.getArgumentValue(0)).isEqualTo("hello"); }
@Test public void testWhenThereAreNoDeclaredArguments() { CLI cli = new DefaultCLI().setName("test"); CommandLine cl = cli.parse(Arrays.asList("a", "b", "c")); assertThat(cl.allArguments()).containsExactly("a", "b", "c"); }
@Test public void testAnnotatedClassWithArgumentReceivingMultipleValues() { CLI cli = CLI.create(CLIUsingMultipleArgument.class); CLIUsingMultipleArgument instance = new CLIUsingMultipleArgument(); CommandLine cl = cli.parse(Arrays.asList("a", "b", "-s=1", "-s=2")); CLIConfigurator.inject(cl, instance); }
@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 testCommandLineValidationWhenValid() { final CLI cli = CLI.create("test") .addArgument(new Argument().setArgName("foo").setRequired(true)); CommandLine commandLine = cli.parse(Collections.singletonList("foo")); assertThat(commandLine.isValid()).isTrue(); }
@Test public void testCommandLineValidationWhenInvalidWithValidationDisabled() { final CLI cli = CLI.create("test") .addArgument(new Argument().setArgName("foo").setRequired(true)); CommandLine commandLine = cli.parse(Collections.<String>emptyList(), false); assertThat(commandLine.isValid()).isEqualTo(false); }
@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")); }
@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 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 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"); }
@Test public void testWithMultipleArgumentReceivingSingleValues() { CLI cli = new DefaultCLI().setName("test"); cli.addArgument(new Argument().setIndex(0)); cli.addArgument(new Argument().setIndex(1).setMultiValued(true)); CommandLine cl = cli.parse(Arrays.asList("a", "b")); assertThat((String) cl.getArgumentValue(0)).isEqualTo("a"); assertThat(cl.getArgumentValues(1)).containsExactly("b"); assertThat((String) cl.getArgumentValue(1)).isEqualTo("b"); }
@Test(expected = MissingValueException.class) public void testCommandLineValidationWhenInvalid() { final CLI cli = CLI.create("test") .addArgument(new Argument().setArgName("foo").setRequired(true)); cli.parse(Collections.<String>emptyList()); }
@Test(expected = MissingOptionException.class) public void testNotFulfilledRequiredOptions() throws CLIException { Option[] options = new Option[]{ new TypedOption<Boolean>().setShortName("f").setLongName("flag").setType(Boolean.class).setRequired(true).setSingleValued(true) }; cli.addOptions(Arrays.asList(options)); CommandLine evaluated = cli.parse(Collections.emptyList()); }
@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 testUnknownOption() throws CLIException { Option[] options = new Option[]{ new TypedOption<Boolean>().setShortName("f").setLongName("flag") .setType(Boolean.class).setRequired(true).setSingleValued(true) }; cli.addOptions(Arrays.asList(options)); CommandLine evaluated = cli.parse(Arrays.asList("-flag=true", "-unknown=x")); assertThat(evaluated.allArguments()).contains("-unknown=x"); }
@Test public void testThatArgumentIndexCanBeGenerated() { CLI cli = new DefaultCLI().setName("test"); cli.addArgument(new Argument()); cli.addArgument(new Argument()); cli.addArgument(new Argument().setMultiValued(true)); CommandLine line = cli.parse(Arrays.asList("a", "b", "c", "d")); assertThat((String) line.getArgumentValue(0)).isEqualToIgnoringCase("a"); assertThat((String) line.getArgumentValue(1)).isEqualToIgnoringCase("b"); assertThat(line.getArgumentValues(2)).containsExactly("c", "d"); }
@Test public void testThatArgumentIndexCanBeGeneratedWithPartiallyNumberedArguments() { CLI cli = new DefaultCLI().setName("test"); cli.addArgument(new Argument()); cli.addArgument(new Argument().setIndex(1)); cli.addArgument(new Argument().setMultiValued(true)); CommandLine line = cli.parse(Arrays.asList("a", "b", "c", "d")); assertThat((String) line.getArgumentValue(0)).isEqualToIgnoringCase("a"); assertThat((String) line.getArgumentValue(1)).isEqualToIgnoringCase("b"); assertThat(line.getArgumentValues(2)).containsExactly("c", "d"); }
@Test(expected = CLIException.class) public void testThatArgumentWithTheSameIndexAreDetected() throws CLIException { cli.addArgument(new TypedArgument<String>().setIndex(0).setType(String.class)); cli.addArgument(new TypedArgument<String>().setIndex(0).setType(String.class)); evaluated = cli.parse(Arrays.asList("a", "b")); }
@Test(expected = CLIException.class) public void testThatOnlyTheLastArgumentCanBeMultivalued() { CLI cli = new DefaultCLI().setName("test"); cli.addArgument(new Argument().setIndex(0)); cli.addArgument(new Argument().setIndex(1).setMultiValued(true)); cli.addArgument(new Argument().setIndex(2)); cli.parse(Arrays.asList("a", "b", "c", "d")); }
@Test(expected = CLIException.class) public void testThatOnlyOneArgumentCanBeMultivalued() { CLI cli = new DefaultCLI().setName("test"); cli.addArgument(new Argument().setIndex(0)); cli.addArgument(new Argument().setIndex(1).setMultiValued(true)); cli.addArgument(new Argument().setIndex(2).setMultiValued(true)); cli.parse(Arrays.asList("a", "b", "c", "d")); }