@Test public void testNegativeOption() throws Exception { String[] args = new String[]{"-b", "-1"}; cli.addOption(new TypedOption<Boolean>().setType(Boolean.class).setSingleValued(false).setShortName("1")); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat(getStringOption(evaluated, "b")).isEqualTo("-1"); assertThat(getBooleanOption(evaluated, "1")).isFalse(); evaluated = cli.parse(Collections.singletonList("-1")); assertThat(getBooleanOption(evaluated, "1")).isTrue(); assertThat(getStringOption(evaluated, "b")).isNull(); }
@Test public void testShortWithEqual() throws Exception { String[] args = new String[]{"-f=bar"}; cli.addOption(new TypedOption<String>().setType(String.class).setSingleValued(true) .setLongName("foo").setShortName("f")); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat(getStringOption(evaluated, "foo")).isEqualTo("bar"); }
@Test public void testShortWithoutEqual() throws Exception { String[] args = new String[]{"-fbar"}; cli.addOption(new TypedOption<String>().setType(String.class).setSingleValued(true) .setLongName("foo").setShortName("f")); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat(getStringOption(evaluated, "foo")).isEqualTo("bar"); }
@Test public void testLongWithEqualSingleDash() throws Exception { String[] args = new String[]{"-foo=bar"}; cli.addOption(new TypedOption<String>().setType(String.class).setSingleValued(true) .setLongName("foo").setShortName("f")); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat(getStringOption(evaluated, "foo")).isEqualTo("bar"); }
@Test public void testLongWithEqualDoubleDash() throws Exception { String[] args = new String[]{"--foo=bar"}; cli.addOption(new TypedOption<String>().setType(String.class).setSingleValued(true) .setLongName("foo").setShortName("f")); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat(getStringOption(evaluated, "foo")).isEqualTo("bar"); }
@Test public void testLongWithoutEqualSingleDash() throws Exception { String[] args = new String[]{"-foobar"}; cli.addOption(new TypedOption<String>().setType(String.class).setSingleValued(true) .setLongName("foo").setShortName("f")); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat(getStringOption(evaluated, "foo")).isEqualTo("bar"); }
@Test public void testMissingValueBecauseOfDoubleDash() throws Exception { cli.addOption(new TypedOption<String>().setType(String.class).setShortName("n").setSingleValued(true)); cli.addOption(new TypedOption<String>().setType(String.class).setShortName("m").setSingleValued(false)); try { cli.parse(Arrays.asList("-n", "--", "-m")); fail("Exception expected"); } catch (MissingValueException e) { assertThat(e.getOption().getShortName()).isEqualTo("n"); } }
@Test public void testPropertiesOption1() throws Exception { String[] args = new String[]{"-Jsource=1.5", "-J", "target", "1.5", "foo"}; TypedOption<String> f = new TypedOption<String>().setShortName("J") .setType(String.class) .setMultiValued(true); cli.addOption(f); CommandLine evaluated = cli.parse(Arrays.asList(args)); List<String> values = evaluated.getOptionValues("J"); assertThat(values).hasSize(4).containsExactly("source=1.5", "target", "1.5", "foo"); }
@Test public void testLongWithoutEqualDoubleDash() throws Exception { String[] args = new String[]{"--foobar"}; TypedOption<String> f = new TypedOption<String>() .setType(String.class).setLongName("foo").setShortName("f").setSingleValued(true); cli.addOption(f); CommandLine evaluated = cli.parse(Arrays.asList(args)); // foo isn't expected to be recognized with a double dash assertThat((String) evaluated.getOptionValue("foo")).isNull(); }
@Test public void testUnambiguousPartialLongOption2() throws Exception { String[] args = new String[]{"-ver"}; TypedOption<Boolean> v = new TypedOption<Boolean>().setLongName("version").setType(Boolean.class) .setSingleValued(false); TypedOption<Boolean> h = new TypedOption<Boolean>().setLongName("help").setType(Boolean.class) .setSingleValued(false); cli.addOption(v).addOption(h); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat(getBooleanOption(evaluated, "version")).isTrue(); }
@Test public void testUnambiguousPartialLongOption1() throws Exception { String[] args = new String[]{"--ver"}; TypedOption<Boolean> v = new TypedOption<Boolean>().setLongName("version").setType(Boolean.class) .setSingleValued(false); TypedOption<Boolean> h = new TypedOption<Boolean>().setLongName("help").setType(Boolean.class) .setSingleValued(false); cli.addOption(v).addOption(h); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat(getBooleanOption(evaluated, "version")).isTrue(); }
@Test public void testPartialLongOptionSingleDash() throws Exception { String[] args = new String[]{"-ver"}; TypedOption<Boolean> v2 = new TypedOption<Boolean>().setLongName("version").setType(Boolean.class) .setSingleValued(false); TypedOption<Integer> v1 = new TypedOption<Integer>().setSingleValued(true) .setShortName("v") .setType(Integer.class); cli.addOption(v1).addOption(v2); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat((Object) evaluated.getOptionValue("v")).isNull(); assertThat((boolean) evaluated.getOptionValue("version")).isTrue(); }
@Test public void testUnambiguousPartialLongOption3() throws Exception { String[] args = new String[]{"--ver=1"}; TypedOption<Integer> v = new TypedOption<Integer>().setLongName("verbose") .setSingleValued(true) .setType(Integer.class); TypedOption<Boolean> h = new TypedOption<Boolean>().setLongName("help").setType(Boolean.class) .setSingleValued(false); cli.addOption(v).addOption(h); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat((int) evaluated.getOptionValue("verbose")).isEqualTo(1); }
@Test public void testUnambiguousPartialLongOption4() throws Exception { String[] args = new String[]{"-ver=1"}; TypedOption<Integer> v = new TypedOption<Integer>().setLongName("verbose") .setSingleValued(true) .setType(Integer.class); TypedOption<Boolean> h = new TypedOption<Boolean>().setLongName("help") .setType(Boolean.class) .setSingleValued(false); cli.addOption(v).addOption(h); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat((int) evaluated.getOptionValue("verbose")).isEqualTo(1); }
@Test public void testAmbiguousPartialLongOption2() throws Exception { String[] args = new String[]{"-ver"}; TypedOption<Integer> v1 = new TypedOption<Integer>().setLongName("verbose") .setSingleValued(true) .setType(Integer.class); TypedOption<Boolean> v2 = new TypedOption<Boolean>().setLongName("version").setType(Boolean.class) .setSingleValued(false); cli.addOption(v1).addOption(v2); try { cli.parse(Arrays.asList(args)); fail("Exception expected"); } catch (AmbiguousOptionException e) { assertThat(e.getToken()).isEqualTo("-ver"); assertThat(e.getOptions()).hasSize(2); } }
@Test public void testLongWithUnexpectedArgument2() throws Exception { String[] args = new String[]{"-foobar"}; TypedOption<Boolean> f = new TypedOption<Boolean>().setLongName("foo").setShortName("f").setType(Boolean.class) .setSingleValued(false) // No value accepted here. ; cli.addOption(f); try { CommandLine evaluated = cli.parse(Arrays.asList(args)); fail("Exception expected"); } catch (InvalidValueException e) { assertThat(e.getOption().getShortName()).isEqualToIgnoringCase("f"); assertThat(e.getValue()).isEqualToIgnoringCase("bar"); } }
@Test public void testShortWithUnexpectedArgument() throws Exception { String[] args = new String[]{"-f=bar"}; TypedOption<Boolean> f = new TypedOption<Boolean>().setLongName("foo").setShortName("f").setType(Boolean.class) .setSingleValued(false) // No value accepted here. ; cli.addOption(f); try { CommandLine evaluated = cli.parse(Arrays.asList(args)); fail("Exception expected"); } catch (InvalidValueException e) { assertThat(e.getOption().getShortName()).isEqualToIgnoringCase("f"); assertThat(e.getValue()).isEqualToIgnoringCase("bar"); } }
@Test public void testAmbiguousPartialLongOption3() throws Exception { String[] args = new String[]{"--ver=1"}; TypedOption<Integer> v1 = new TypedOption<Integer>().setLongName("verbose") .setSingleValued(true) .setType(Integer.class); TypedOption<Boolean> v2 = new TypedOption<Boolean>().setLongName("version").setType(Boolean.class) .setSingleValued(false); cli.addOption(v1).addOption(v2); try { cli.parse(Arrays.asList(args)); fail("Exception expected"); } catch (AmbiguousOptionException e) { assertThat(e.getToken()).isEqualTo("--ver"); assertThat(e.getOptions()).hasSize(2); } }
@Before public void setUp() { cli = new DefaultCLI(); cli.setName("test").setHidden(false).setDescription("A test command"); cli.addOption(new TypedOption<Boolean>().setType(Boolean.class).setShortName("a").setLongName("enable-a") .setFlag(true).setDescription("turn [a] on or off")); cli.addOption(new TypedOption<String>().setType(String.class).setShortName("b").setLongName("bfile").setSingleValued(true) .setDescription("set the value of [b]")); cli.addOption(new TypedOption<Boolean>().setType(Boolean.class).setShortName("c").setLongName("copt").setSingleValued(false) .setDescription("turn [c] on or off")); }
@Test public void testMultiValues() throws Exception { String[] args = new String[]{"-e", "one", "two", "-f", "1"}; TypedOption<String> e = new TypedOption<String>().setShortName("e") .setMultiValued(true).setType(String.class); TypedOption<Integer> f = new TypedOption<Integer>().setShortName("f") .setMultiValued(true).setType(Integer.class); cli.addOption(e).addOption(f); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat(evaluated.getOptionValues("e")).contains("one", "two").hasSize(2); assertThat(evaluated.getOptionValues("f")).contains(1).hasSize(1); }