private boolean hasOptionWithShortName(String name) { for (Option option : cli.getOptions()) { if (name.equalsIgnoreCase(option.getShortName())) { return true; } } return false; }
@Override public CLI removeOption(String name) { options = options.stream().filter(o -> !o.getLongName().equals(name) && !o.getShortName().equals(name)) .collect(Collectors.toList()); return this; }
@Override public Option getOption(String name) { Objects.requireNonNull(name); // The option by name look up is a three steps lookup: // first check by long name // then by short name // finally by arg name for (Option option : options) { if (name.equalsIgnoreCase(option.getLongName())) { return option; } } for (Option option : options) { if (name.equalsIgnoreCase(option.getShortName())) { return option; } } for (Option option : options) { if (name.equalsIgnoreCase(option.getArgName())) { return option; } } return null; }
/** * Retrieve the {@link Option} matching the long or short name specified. * The leading hyphens in the name are ignored (up to 2). * * @param opt short or long name of the {@link Option} * @return the option represented by opt */ public Option getOption(String opt) { opt = stripLeadingHyphens(opt); for (Option option : cli.getOptions()) { if (opt.equalsIgnoreCase(option.getShortName()) || opt.equalsIgnoreCase(option.getLongName())) { return option; } } return null; }
if (!isNullOrEmpty(option.getShortName())) { buff.append("-").append(option.getShortName()); } else { buff.append("--").append(option.getLongName()); buff.append(isNullOrEmpty(option.getShortName()) ? getLongOptionSeparator() : " "); buff.append(option.getChoices().stream().collect(Collectors.joining(", ", "{", "}"))); } else { buff.append(isNullOrEmpty(option.getShortName()) ? getLongOptionSeparator() : " "); buff.append("<").append(option.getArgName() != null ? option.getArgName() : getArgName()).append(">");
if (obj.getShortName() != null) { json.put("shortName", obj.getShortName());
if (isNullOrEmpty(option.getShortName())) { buf.append(lpad).append(" ").append(getLongOptionPrefix()).append(option.getLongName()); } else { buf.append(lpad).append(getOptionPrefix()).append(option.getShortName());
@Test public void testMissingArgWithBursting() throws Exception { String[] args = new String[]{"-acb"}; try { CommandLine evaluated = cli.parse(Arrays.asList(args)); fail("exception expected"); } catch (MissingValueException e) { assertThat(e.getOption().getShortName()).isEqualTo("b"); } }
@Test public void testMissingValue() throws Exception { String[] args = new String[]{"-b"}; try { CommandLine evaluated = cli.parse(Arrays.asList(args)); fail("Exception expected"); } catch (MissingValueException e) { assertThat(e.getOption().getShortName()).isEqualToIgnoringCase("b"); } }
@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 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 testLongWithUnexpectedArgument1() throws Exception { String[] args = new String[]{"--foo=bar"}; TypedOption<Boolean> f = new TypedOption<Boolean>().setLongName("foo").setShortName("f") .setType(Boolean.class) .setSingleValued(false) // No value accepted here. .setFlag(true); 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"); } }
@Override public CLI removeOption(String name) { options = options.stream().filter(o -> !o.getLongName().equals(name) && !o.getShortName().equals(name)) .collect(Collectors.toList()); return this; }
private boolean hasOptionWithShortName(String name) { for (Option option : cli.getOptions()) { if (name.equalsIgnoreCase(option.getShortName())) { return true; } } return false; }
/** * Retrieve the {@link Option} matching the long or short name specified. * The leading hyphens in the name are ignored (up to 2). * * @param opt short or long name of the {@link Option} * @return the option represented by opt */ public Option getOption(String opt) { opt = stripLeadingHyphens(opt); for (Option option : cli.getOptions()) { if (opt.equalsIgnoreCase(option.getShortName()) || opt.equalsIgnoreCase(option.getLongName())) { return option; } } return null; }
assertEquals(copy.isMultiValued(), option.isMultiValued()); assertEquals(copy.isRequired(), option.isRequired()); assertEquals(copy.getShortName(), option.getShortName()); assertEquals(copy.isSingleValued(), option.isSingleValued()); assertEquals(copy.isMultiValued(), option.isMultiValued()); assertEquals(copy.isRequired(), option.isRequired()); assertEquals(copy.getShortName(), option.getShortName()); assertEquals(copy.isSingleValued(), option.isSingleValued());
@Test public void testMissingArgWithBursting() throws Exception { String[] args = new String[]{"-acb"}; try { CommandLine evaluated = cli.parse(Arrays.asList(args)); fail("exception expected"); } catch (MissingValueException e) { assertThat(e.getOption().getShortName()).isEqualTo("b"); } }
@Test public void testMissingValue() throws Exception { String[] args = new String[]{"-b"}; try { CommandLine evaluated = cli.parse(Arrays.asList(args)); fail("Exception expected"); } catch (MissingValueException e) { assertThat(e.getOption().getShortName()).isEqualToIgnoringCase("b"); } }
@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"); } }