private void compact() { globalOptions = mergeOptionSet(globalOptions); groupOptions = mergeOptionSet(groupOptions); commandOptions = mergeOptionSet(commandOptions); if (arguments.size() > 1) { arguments = ImmutableList.of(new ArgumentsMetadata(arguments)); } } }
public ArgumentsMetadata(Iterable<ArgumentsMetadata> arguments) { Preconditions.checkNotNull(arguments, "arguments is null"); Preconditions.checkArgument(!Iterables.isEmpty(arguments), "arguments is empty"); ArgumentsMetadata first = arguments.iterator().next(); this.title = first.title; this.description = first.description; this.usage = first.usage; this.required = first.required; Set<Accessor> accessors = newHashSet(); for (ArgumentsMetadata other : arguments) { Preconditions.checkArgument(first.equals(other), "Conflicting arguments definitions: %s, %s", first, other); accessors.addAll(other.getAccessors()); } this.accessors = ImmutableSet.copyOf(accessors); }
public static String toUsage(ArgumentsMetadata arguments) { if (!arguments.getUsage().isEmpty()) { return arguments.getUsage(); } boolean required = arguments.isRequired(); StringBuilder stringBuilder = new StringBuilder(); if (!required) { stringBuilder.append('['); } stringBuilder.append("<").append(arguments.getTitle()).append(">"); if (arguments.isMultiValued()) { stringBuilder.append("..."); } if (!required) { stringBuilder.append(']'); } return stringBuilder.toString(); }
private ParseState parseArg(ParseState state, PeekingIterator<String> tokens, ArgumentsMetadata arguments) { if (arguments != null) { state = state.withArgument(TypeConverter.newInstance().convert(arguments.getTitle(), arguments.getJavaType(), tokens.next())); } else { state = state.withUnparsedInput(tokens.next()); } return state; }
private void validate(ParseState state) { CommandMetadata command = state.getCommand(); if (command == null) { List<String> unparsedInput = state.getUnparsedInput(); if (unparsedInput.isEmpty()) { throw new ParseCommandMissingException(); } else { throw new ParseCommandUnrecognizedException(unparsedInput); } } ArgumentsMetadata arguments = command.getArguments(); if (state.getParsedArguments().isEmpty() && arguments != null && arguments.isRequired()) { throw new ParseArgumentsMissingException(arguments.getTitle()); } if (!state.getUnparsedInput().isEmpty()) { throw new ParseArgumentsUnexpectedException(state.getUnparsedInput()); } if (state.getLocation() == Context.OPTION) { throw new ParseOptionMissingValueException(state.getCurrentOption().getTitle()); } for (OptionMetadata option : command.getAllOptions()) { if (option.isRequired() && !state.getParsedOptions().containsKey(option)) { throw new ParseOptionMissingException(option.getOptions().iterator().next()); } } }
for (Accessor accessor : arguments.getAccessors()) { accessor.addValues(commandInstance, parsedArguments);
descriptionPrinter.append(arguments.getDescription()).newline(); descriptionPrinter.newline();
boolean required = argumentsAnnotation.required(); injectionMetadata.arguments.add(new ArgumentsMetadata(title, description, usage, required, path));