Map<Argument, Field> arguments = new HashMap<Argument, Field>();
List<Argument> orderedArguments = new ArrayList<Argument>();
for (Class<?> type = action.getClass(); type != null; type = type.getSuperclass()) {
for (Field field : type.getDeclaredFields()) {
Option option = field.getAnnotation(Option.class);
if (option != null) {
options.put(option, field);
}
Argument argument = field.getAnnotation(Argument.class);
if (argument != null) {
argument = replaceDefaultArgument(field, argument);
arguments.put(argument, field);
int index = argument.index();
while (orderedArguments.size() <= index) {
orderedArguments.add(null);
}
if (orderedArguments.get(index) != null) {
throw new IllegalArgumentException("Duplicate argument index: " + index + " on Action " + action.getClass().getName());
}
orderedArguments.set(index, argument);
}
}
}
assertIndexesAreCorrect(action.getClass(), orderedArguments);
String commandErrorSt = COLOR_RED + "Error executing command " + command.scope() + ":" + INTENSITY_BOLD + command.name() + INTENSITY_NORMAL + COLOR_DEFAULT + ": ";
for (Iterator<Object> it = params.iterator(); it.hasNext(); ) {
Object param = it.next();
if (HelpOption.HELP.name().equals(param)) {
int termWidth = session.getTerminal() != null ? session.getTerminal().getWidth() : 80;
boolean globalScope = NameScoping.isGlobalScope(session, command.scope());
printUsage(action, options, arguments, System.out, globalScope, termWidth);
return false;
}
}
// Populate
Map<Option, Object> optionValues = new HashMap<Option, Object>();
Map<Argument, Object> argumentValues = new HashMap<Argument, Object>();
boolean processOptions = true;
int argIndex = 0;
for (Iterator<Object> it = params.iterator(); it.hasNext(); ) {
Object param = it.next();
if (processOptions && param instanceof String && ((String) param).startsWith("-")) {
boolean isKeyValuePair = ((String) param).indexOf('=') != -1;
String name;
Object value = null;
if (isKeyValuePair) {
name = ((String) param).substring(0, ((String) param).indexOf('='));
value = ((String) param).substring(((String) param).indexOf('=') + 1);
} else {
name = (String) param;
}
Option option = null;
for (Option opt : options.keySet()) {
if (name.equals(opt.name()) || Arrays.asList(opt.aliases()).contains(name)) {
option = opt;
break;
}
}
if (option == null) {
throw new CommandException(commandErrorSt
+ "undefined option " + INTENSITY_BOLD + param + INTENSITY_NORMAL + "\n"
+ "Try <command> --help' for more information.",
"Undefined option: " + param);
}
Field field = options.get(option);
if (value == null && (field.getType() == boolean.class || field.getType() == Boolean.class)) {
value = Boolean.TRUE;
}
if (value == null && it.hasNext()) {
value = it.next();
}
if (value == null) {
throw new CommandException(commandErrorSt
+ "missing value for option " + INTENSITY_BOLD + param + INTENSITY_NORMAL,
"Missing value for option: " + param
);
}
if (option.multiValued()) {
@SuppressWarnings("unchecked")
List<Object> l = (List<Object>) optionValues.get(option);
if (l == null) {
l = new ArrayList<Object>();
optionValues.put(option, l);
}
l.add(value);
} else {
optionValues.put(option, value);
}
} else {
processOptions = false;
if (argIndex >= orderedArguments.size()) {
throw new CommandException(commandErrorSt +
"too many arguments specified",
"Too many arguments specified"
);
}
Argument argument = orderedArguments.get(argIndex);
if (!argument.multiValued()) {
argIndex++;
}
if (argument.multiValued()) {
@SuppressWarnings("unchecked")
List<Object> l = (List<Object>) argumentValues.get(argument);
if (l == null) {
l = new ArrayList<Object>();
argumentValues.put(argument, l);
}
l.add(param);
} else {
argumentValues.put(argument, param);
}
}
}
// Check required arguments / options
for (Option option : options.keySet()) {
if (option.required() && optionValues.get(option) == null) {
throw new CommandException(commandErrorSt +
"option " + INTENSITY_BOLD + option.name() + INTENSITY_NORMAL + " is required",
"Option " + option.name() + " is required"
);
}
}
for (Argument argument : orderedArguments) {
if (argument.required() && argumentValues.get(argument) == null) {