@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; }
@Override public CLI removeArgument(int index) { for (Argument arg : new TreeSet<>(arguments)) { if (arg.getIndex() == index) { arguments.remove(arg); return this; } } return this; }
@Override public <T> T getArgumentValue(String name) { Argument arg = cli.getArgument(name); if (arg == null) { return null; } return getArgumentValue(arg.getIndex()); }
/** * 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; }
/** * Creates a new instance of {@link InvalidValueException} for the given argument and the given value. This * constructor is used when the argument receives a value that cannot be "converted" to the desired type. * * @param argument the argument * @param value the value * @param cause the cause */ public InvalidValueException(Argument argument, String value, Exception cause) { super("The value '" + value + "' is not accepted by the argument '" + (argument.getArgName() != null ? argument.getArgName() : argument.getIndex()) + "'", cause); this.option = null; this.value = value; this.argument = argument; }
private static Object getArgumentValue(Method method, int index, CommandLine commandLine) { final io.vertx.core.cli.Argument argument = commandLine.cli().getArgument(index); if (argument == null) { return null; } boolean multiple = ReflectionUtils.isMultiple(method); if (multiple) { return createMultiValueContainer(method, commandLine.getArgumentValues(argument.getIndex())); } return commandLine.getArgumentValue(argument.getIndex()); }
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()); } }
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");
@Test public void testThatInvalidValuesAsDefaultValueAreReported() throws CLIException { try { cli.addArgument(new TypedArgument<Integer>() .setIndex(0).setArgName("1").setType(Integer.class).setDefaultValue("a")); } catch (IllegalArgumentException e) { assertThat(e.getCause()).isInstanceOf(InvalidValueException.class); InvalidValueException cause = (InvalidValueException) e.getCause(); assertThat(cause.getArgument().getIndex()).isEqualTo(0); assertThat(cause.getArgument().getArgName()).isEqualTo("1"); assertThat(cause.getValue()).isEqualTo("a"); } }
@Test public void testThatInvalidValuesAreReported() throws CLIException { cli.addArgument(new TypedArgument<Integer>() .setIndex(0).setArgName("1").setType(Integer.class)); try { evaluated = cli.parse(Collections.singletonList("a")); evaluated.getArgumentValue(0); fail("Exception expected"); } catch (CLIException e) { assertThat(e).isInstanceOf(InvalidValueException.class); InvalidValueException cause = (InvalidValueException) e; assertThat(cause.getArgument().getIndex()).isEqualTo(0); assertThat(cause.getArgument().getArgName()).isEqualTo("1"); assertThat(cause.getValue()).isEqualTo("a"); } }
@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; }
@Override public CLI removeArgument(int index) { for (Argument arg : new TreeSet<>(arguments)) { if (arg.getIndex() == index) { arguments.remove(arg); return this; } } return this; }
@Override public <T> T getArgumentValue(String name) { Argument arg = cli.getArgument(name); if (arg == null) { return null; } return getArgumentValue(arg.getIndex()); }
/** * 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; }
/** * Creates a new instance of {@link InvalidValueException} for the given argument and the given value. This * constructor is used when the argument receives a value that cannot be "converted" to the desired type. * * @param argument the argument * @param value the value * @param cause the cause */ public InvalidValueException(Argument argument, String value, Exception cause) { super("The value '" + value + "' is not accepted by the argument '" + (argument.getArgName() != null ? argument.getArgName() : argument.getIndex()) + "'", cause); this.option = null; this.value = value; this.argument = argument; }
private static Object getArgumentValue(Method method, int index, CommandLine commandLine) { final io.vertx.core.cli.Argument argument = commandLine.cli().getArgument(index); if (argument == null) { return null; } boolean multiple = ReflectionUtils.isMultiple(method); if (multiple) { return createMultiValueContainer(method, commandLine.getArgumentValues(argument.getIndex())); } return commandLine.getArgumentValue(argument.getIndex()); }
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()); } }
@Test public void testThatInvalidValuesAreReported() throws CLIException { cli.addArgument(new TypedArgument<Integer>() .setIndex(0).setArgName("1").setType(Integer.class)); try { evaluated = cli.parse(Collections.singletonList("a")); evaluated.getArgumentValue(0); fail("Exception expected"); } catch (CLIException e) { assertThat(e).isInstanceOf(InvalidValueException.class); InvalidValueException cause = (InvalidValueException) e; assertThat(cause.getArgument().getIndex()).isEqualTo(0); assertThat(cause.getArgument().getArgName()).isEqualTo("1"); assertThat(cause.getValue()).isEqualTo("a"); } }
@Test public void testThatInvalidValuesAsDefaultValueAreReported() throws CLIException { try { cli.addArgument(new TypedArgument<Integer>() .setIndex(0).setArgName("1").setType(Integer.class).setDefaultValue("a")); } catch (IllegalArgumentException e) { assertThat(e.getCause()).isInstanceOf(InvalidValueException.class); InvalidValueException cause = (InvalidValueException) e.getCause(); assertThat(cause.getArgument().getIndex()).isEqualTo(0); assertThat(cause.getArgument().getArgName()).isEqualTo("1"); assertThat(cause.getValue()).isEqualTo("a"); } }