@Override public TypedOption<T> setSingleValued(boolean acceptSingleValue) { super.setSingleValued(acceptSingleValue); return this; }
/** * Configures the current {@link Option} to be a flag. It will be evaluated to {@code true} if it's found in * the command line. If you need a flag that may receive a value, use, in this order: * <code><pre> * option.setFlag(true).setSingleValued(true) * </pre></code> * * @param flag whether or not the option is a flag. * @return the current {@link Option} */ public Option setFlag(boolean flag) { this.flag = flag; setSingleValued(false); return this; }
case "singleValued": if (member.getValue() instanceof Boolean) { obj.setSingleValued((Boolean)member.getValue());
@Test public void testTheDifferentFormatForShortOption() 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((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); evaluated = cli.parse(Collections.singletonList("-f=hello.txt")); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); evaluated = cli.parse(Collections.singletonList("-fhello.txt")); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); }
@Test public void testTheDifferentFormatForLongOption() 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((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); evaluated = cli.parse(Collections.singletonList("--file=hello.txt")); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); evaluated = cli.parse(Collections.singletonList("-filehello.txt")); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); evaluated = cli.parse(Arrays.asList("--FILE", "hello.txt")); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); }
@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.toString()).startsWith("Usage: test [-f <value>]"); assertThat(usage.toString()).contains("-f,--file <value>"); }
@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 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.toString()).startsWith("Usage: test [-f <value>]"); }
@Override public TypedOption<T> setSingleValued(boolean acceptSingleValue) { super.setSingleValued(acceptSingleValue); return this; }
@Test public void testTheDifferentFormatForShortOption() 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((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); evaluated = cli.parse(Collections.singletonList("-f=hello.txt")); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); evaluated = cli.parse(Collections.singletonList("-fhello.txt")); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); }
@Test public void testTheDifferentFormatForLongOption() 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((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); evaluated = cli.parse(Collections.singletonList("--file=hello.txt")); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); evaluated = cli.parse(Collections.singletonList("-filehello.txt")); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); evaluated = cli.parse(Arrays.asList("--FILE", "hello.txt")); assertThat((String) evaluated.getOptionValue("file")).isEqualTo("hello.txt"); }
@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 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 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.toString()).startsWith("Usage: test [-f <value>]"); }
@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"); }
.setRequired(TestUtils.randomBoolean()) .setShortName(TestUtils.randomAlphaString(100)) .setSingleValued(TestUtils.randomBoolean());