public static String toDescription(OptionMetadata option) { Set<String> options = option.getOptions(); StringBuilder stringBuilder = new StringBuilder(); final String argumentString; if (option.getArity() > 0) { argumentString = Joiner.on(" ").join(Lists.transform(ImmutableList.of(option.getTitle()), new Function<String, String>() { public String apply(@Nullable String argument) { return "<" + argument + ">"; } })); } else { argumentString = null; } Joiner.on(", ").appendTo(stringBuilder, transform(options, new Function<String, String>() { public String apply(@Nullable String option) { if (argumentString != null) { return option + " " + argumentString; } return option; } })); return stringBuilder.toString(); }
argumentString = Joiner.on(" ").join(transform(ImmutableList.of(option.getTitle()), new Function<String, String>()
private ParseState parseLongGnuGetOpt(PeekingIterator<String> tokens, ParseState state, List<OptionMetadata> allowedOptions) { List<String> parts = ImmutableList.copyOf(Splitter.on('=').limit(2).split(tokens.peek())); if (parts.size() != 2) { return null; } OptionMetadata option = findOption(allowedOptions, parts.get(0)); if (option == null || option.getArity() != 1) { // TODO: this is not exactly correct. It should be an error condition return null; } // we have a match so consume the token tokens.next(); // update state state = state.pushContext(Context.OPTION).withOption(option); Object value = TypeConverter.newInstance().convert(option.getTitle(), option.getJavaType(), parts.get(1)); state = state.withOption(option).withOptionValue(option, value).popContext(); return state; }
Object value = TypeConverter.newInstance().convert(option.getTitle(), option.getJavaType(), remainingToken); nextState = nextState.withOptionValue(option, value).popContext(); Object value = TypeConverter.newInstance().convert(option.getTitle(), option.getJavaType(), tokens.next()); nextState = nextState.withOptionValue(option, value).popContext();
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()); } } }