@Test public void testWithArgumentReceivingMultipleValues() { CLI cli = new DefaultCLI().setName("test"); cli.addArgument(new Argument().setIndex(0).setArgName("arg").setDescription("argument1")); cli.addArgument(new Argument().setIndex(1).setMultiValued(true).setArgName("m").setDescription("multiple arg")); CommandLine cl = cli.parse(Arrays.asList("a", "b", "c")); assertThat((String) cl.getArgumentValue(0)).isEqualTo("a"); assertThat(cl.getArgumentValues(1)).containsExactly("b", "c"); assertThat((String) cl.getArgumentValue(1)).isEqualTo("b"); StringBuilder builder = new StringBuilder(); cli.usage(builder); assertThat(builder.toString()).contains("test arg m..."); }
static void toJson(Argument obj, java.util.Map<String, Object> json) { if (obj.getArgName() != null) { json.put("argName", obj.getArgName()); } if (obj.getDefaultValue() != null) { json.put("defaultValue", obj.getDefaultValue()); } if (obj.getDescription() != null) { json.put("description", obj.getDescription()); } json.put("hidden", obj.isHidden()); json.put("index", obj.getIndex()); json.put("multiValued", obj.isMultiValued()); json.put("required", obj.isRequired()); } }
case "argName": if (member.getValue() instanceof String) { obj.setArgName((String)member.getValue()); obj.setDefaultValue((String)member.getValue()); obj.setDescription((String)member.getValue()); obj.setHidden((Boolean)member.getValue()); obj.setIndex(((Number)member.getValue()).intValue()); obj.setMultiValued((Boolean)member.getValue()); obj.setRequired((Boolean)member.getValue());
/** * Creates a new instance of {@link MissingValueException} when a mandatory argument is not set in the user command * line. * * @param argument the argument */ public MissingValueException(Argument argument) { super("The argument '" + (argument.getArgName() != null ? argument.getArgName() : argument.getIndex()) + "' is required"); this.option = null; this.argument = argument; }
/** * Appends the usage clause for an Argument to a StringBuilder. * * @param buff the StringBuilder to append to * @param argument the argument to add * @param required whether the Option is required or not */ protected void appendArgument(StringBuilder buff, Argument argument, boolean required) { if (argument.isHidden()) { return; } if (!required) { buff.append("["); } buff.append(argument.getArgName()); if (argument.isMultiValued()) { buff.append("..."); } // if the Option is not a required option if (!required) { buff.append("]"); } }
@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")); }
@Test public void testUsageComputationWhenUsingNotRequiredArgument() { final CLI cli = CLI.create("test") .addArgument(new Argument().setArgName("foo").setRequired(false)); StringBuilder builder = new StringBuilder(); cli.usage(builder); assertThat(builder.toString()) .contains("test [foo]"); }
@Test public void testUsageComputationWhenUsingRequiredOptionAndArgument() { final CLI cli = CLI.create("test") .addOption(new Option().setLongName("file").setShortName("f").setDescription("a file").setRequired(true)) .addArgument(new Argument().setArgName("foo").setDescription("foo").setRequired(true)); StringBuilder builder = new StringBuilder(); cli.usage(builder); assertThat(builder.toString()) .contains("test -f <value> foo") .contains(" -f,--file <value> a file") .contains("<foo> foo"); }
@Test public void testWithMultipleRequiredArgument() { CLI cli = new DefaultCLI().setName("test"); cli.addArgument(new Argument().setIndex(0)); cli.addArgument(new Argument().setIndex(1).setMultiValued(true).setRequired(true)); CommandLine cl = cli.parse(Arrays.asList("a", "b", "c")); assertThat((String) cl.getArgumentValue(0)).isEqualTo("a"); assertThat(cl.getArgumentValues(1)).containsExactly("b", "c"); assertThat((String) cl.getArgumentValue(1)).isEqualTo("b"); 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"); try { cli.parse(Collections.singletonList("a")); fail("required argument not fultilled"); } catch (MissingValueException e) { // OK. } }
@Test(expected = CLIException.class) public void testThatNonUniqueArgumentIndexAreDetected() { CLI cli = new DefaultCLI().setName("test"); cli.addArgument(new Argument().setIndex(0)); cli.addArgument(new Argument().setIndex(1)); cli.addArgument(new Argument().setIndex(1)); // conflict cli.parse(Arrays.asList("a", "b", "c")); }
public static java.util.List<java.lang.String> getRawValuesForArgument(io.vertx.core.cli.CommandLine j_receiver, java.util.Map<String, Object> argument) { return j_receiver.getRawValuesForArgument(argument != null ? new io.vertx.core.cli.Argument(io.vertx.core.impl.ConversionHelper.toJsonObject(argument)) : null) != null ? j_receiver.getRawValuesForArgument(argument != null ? new io.vertx.core.cli.Argument(io.vertx.core.impl.ConversionHelper.toJsonObject(argument)) : null).stream().map(elt -> elt).collect(java.util.stream.Collectors.toList()) : null; } public static java.lang.String getRawValueForOption(io.vertx.core.cli.CommandLine j_receiver, java.util.Map<String, Object> option) {
@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"); }
@Override public Argument getArgument(int index) { if (index < 0) { throw new IllegalArgumentException("Given index cannot be negative"); } for (Argument arg : arguments) { if (index == arg.getIndex()) { return arg; } } return null; }
List<Integer> usedIndexes = new ArrayList<>(); for (Argument argument : cli.getArguments()) { if (usedIndexes.contains(argument.getIndex())) { throw new CLIException("Only one argument can use the index " + argument.getIndex()); usedIndexes.add(argument.getIndex()); if (multiValue) { throw new CLIException("Only the last argument can be multi-valued"); multiValue = argument.isMultiValued(); if (current != null) { commandLine.setRawValue(current, v); if (!current.isMultiValued()) { if (iterator.hasNext()) { current = iterator.next(); if (arg.isRequired() && !commandLine.isArgumentAssigned(arg)) { throw new MissingValueException(arg);
if (argument.isHidden()) { continue; buf.append(lpad).append("<").append(argument.getArgName()).append(">"); if (argument.isHidden()) { continue; if (argument.getDescription() != null) { argBuf.append(argument.getDescription());
@Override public TypedArgument<T> setIndex(int index) { super.setIndex(index); return this; }
@Override public TypedArgument<T> setArgName(String argName) { super.setArgName(argName); return this; }
@Override public TypedArgument<T> setDescription(String description) { super.setDescription(description); return this; }
@Override public Argument getArgument(String name) { Objects.requireNonNull(name); for (Argument arg : arguments) { if (name.equalsIgnoreCase(arg.getArgName())) { return arg; } } return null; }