ParseState nextState = state;
while (!remainingToken.isEmpty()) {
char tokenCharacter = remainingToken.charAt(0);
// is the current token character a single letter option?
OptionMetadata option = findOption(allowedOptions, "-" + tokenCharacter);
if (option == null) {
return null;
}
nextState = nextState.pushContext(Context.OPTION).withOption(option);
// remove current token character
remainingToken = remainingToken.substring(1);
// for no argument options, process the option and remove the character from the token
if (option.getArity() == 0) {
nextState = nextState.withOptionValue(option, Boolean.TRUE).popContext();
continue;
}
if (option.getArity() == 1) {
// we must, consume the current token so we can see the next token
tokens.next();
// if current token has more characters, this is the value; otherwise it is the next token
if (!remainingToken.isEmpty()) {
Object value = TypeConverter.newInstance().convert(option.getTitle(), option.getJavaType(), remainingToken);
nextState = nextState.withOptionValue(option, value).popContext();
}
else if (tokens.hasNext()) {
Object value = TypeConverter.newInstance().convert(option.getTitle(), option.getJavaType(), tokens.next());
nextState = nextState.withOptionValue(option, value).popContext();
}
return nextState;
}
throw new UnsupportedOperationException("Short options style can not be used with option " + option.getAllowedValues());
}
// consume the current token
tokens.next();