/** * Creates an instance of {@link CLI} using the default implementation. * * @param name the name of the CLI (must not be {@code null}) * @return the created instance of {@link CLI} */ static CLI create(String name) { return new DefaultCLI().setName(name); }
@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(); }
@Override public CLI setOptions(List<Option> options) { Objects.requireNonNull(options); this.options = new ArrayList<>(); return addOptions(options); }
@Test public void testOptionAndRequiredOption() throws Exception { String[] args = new String[]{"-a", "-b", "file"}; TypedOption<String> b = new TypedOption<String>().setShortName("b").setLongName("bfile").setSingleValued(true) .setDescription("set the value of [b]").setType(String.class).setRequired(true); cli.removeOption("b").addOption(b); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat(getBooleanOption(evaluated, "a")).isTrue(); assertThat(getStringOption(evaluated, "b")).isEqualTo("file"); assertThat(evaluated.allArguments()).isEmpty(); }
@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 testArgumentStartingWithHyphen() throws Exception { String[] args = new String[]{"-b", "-foo"}; CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat(getStringOption(evaluated, "b")).isEqualTo("-foo"); }
CLI cli = new DefaultCLI();
@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 testWithRequiredOption() throws Exception { String[] args = new String[]{"-b", "file"}; TypedOption<String> b = new TypedOption<String>().setShortName("b").setLongName("bfile").setSingleValued(true) .setDescription("set the value of [b]").setType(String.class).setRequired(true); cli.removeOption("b").addOption(b); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat(getBooleanOption(evaluated, "a")).isFalse(); assertThat((String) evaluated.getOptionValue("b")).isEqualTo("file"); assertThat(evaluated.allArguments()).isEmpty(); }
@Test public void testNegativeArgument() throws Exception { String[] args = new String[]{"-b", "-1"}; CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat(getStringOption(evaluated, "b")).isEqualTo("-1"); }
@Before public void setUp() { cli = new DefaultCLI().setName("test"); }
@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 testAmbiguousLongWithoutEqualSingleDash() throws Exception { String[] args = new String[]{"-b", "-foobar"}; TypedOption<String> f = new TypedOption<String>().setType(String.class).setLongName("foo").setShortName("f").setSingleValued(true); TypedOption<Boolean> b = new TypedOption<Boolean>().setType(Boolean.class).setLongName("bar").setShortName("b") .setFlag(true); cli.removeOption("b").addOption(f).addOption(b); CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat((boolean) evaluated.getOptionValue("bar")).isTrue(); assertThat((String) evaluated.getOptionValue("foo")).isEqualTo("bar"); }
@Test public void testArgumentsInTheMiddle() throws Exception { String[] args = new String[]{"-c", "foobar", "-b", "toast"}; CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat(getBooleanOption(evaluated, "c")).isTrue(); assertThat(getStringOption(evaluated, "b")).isEqualTo("toast"); assertThat(evaluated.allArguments()).contains("foobar").hasSize(1); }
@Override public CLI setOptions(List<Option> options) { Objects.requireNonNull(options); this.options = new ArrayList<>(); return addOptions(options); }
@Before public void setUp() { cli = new DefaultCLI().setName("test"); }
@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 testMissingRequiredOption() throws Exception { String[] args = new String[]{"-a"}; TypedOption<String> b = new TypedOption<String>().setShortName("b").setLongName("bfile") .setSingleValued(true) .setDescription("set the value of [b]").setType(String.class).setRequired(true); cli.removeOption("b").addOption(b); try { cli.parse(Arrays.asList(args)); fail("exception expected"); } catch (MissingOptionException e) { assertThat(e.getExpected()).hasSize(1); } }
@Test public void testSingleDash() throws Exception { String[] args = new String[]{"--copt", "-b", "-", "-a", "-"}; CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat(getBooleanOption(evaluated, "a")).isTrue(); assertThat(getStringOption(evaluated, "b")).isEqualTo("-"); assertThat(getBooleanOption(evaluated, "c")).isTrue(); assertThat(evaluated.allArguments()).contains("-").hasSize(1); }
@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"); }