}
}
if (option == null) {
Command command = action.getClass().getAnnotation(Command.class);
if (command != null) {
throw new CommandException(
Ansi.ansi()
.fg(Ansi.Color.RED)
.a("Error executing command ")
.a(command.scope())
.a(":")
.a(Ansi.Attribute.INTENSITY_BOLD)
.a(command.name())
.a(Ansi.Attribute.INTENSITY_BOLD_OFF)
.a(" undefined option ")
.a(Ansi.Attribute.INTENSITY_BOLD)
.a(param)
.a(Ansi.Attribute.INTENSITY_BOLD_OFF)
.fg(Ansi.Color.DEFAULT)
.toString(),
"Undefined option: " + param
);
} else {
throw new CommandException("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) {
Command command = action.getClass().getAnnotation(Command.class);
if (command != null) {
throw new CommandException(
Ansi.ansi()
.fg(Ansi.Color.RED)
.a("Error executing command ")
.a(command.scope())
.a(":")
.a(Ansi.Attribute.INTENSITY_BOLD)
.a(command.name())
.a(Ansi.Attribute.INTENSITY_BOLD_OFF)
.a(" missing value for option ")
.a(Ansi.Attribute.INTENSITY_BOLD)
.a(param)
.a(Ansi.Attribute.INTENSITY_BOLD_OFF)
.fg(Ansi.Color.DEFAULT)
.toString(),
"Missing value for option: " + param
);
} else {
throw new CommandException("Missing value for option: " + param);
}
}
if (option.multiValued()) {
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()) {
Command command = action.getClass().getAnnotation(Command.class);
if (command != null) {
throw new CommandException(
Ansi.ansi()
.fg(Ansi.Color.RED)
.a("Error executing command ")
.a(command.scope())
.a(":")
.a(Ansi.Attribute.INTENSITY_BOLD)
.a(command.name())
.a(Ansi.Attribute.INTENSITY_BOLD_OFF)
.a(": too many arguments specified")
.fg(Ansi.Color.DEFAULT)
.toString(),
"Too many arguments specified"
);
} else {
throw new CommandException("Too many arguments specified");
}
}
Argument argument = orderedArguments.get(argIndex);
if (!argument.multiValued()) {
argIndex++;
}
if (argument.multiValued()) {
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) {
Command command = action.getClass().getAnnotation(Command.class);
if (command != null) {
throw new CommandException(
Ansi.ansi()
.fg(Ansi.Color.RED)
.a("Error executing command ")
.a(command.scope())
.a(":")
.a(Ansi.Attribute.INTENSITY_BOLD)
.a(command.name())
.a(Ansi.Attribute.INTENSITY_BOLD_OFF)
.a(": option ")
.a(Ansi.Attribute.INTENSITY_BOLD)
.a(option.name())
.a(Ansi.Attribute.INTENSITY_BOLD_OFF)
.a(" is required")
.fg(Ansi.Color.DEFAULT)
.toString(),
"Option " + option.name() + " is required"
);
} else {
throw new CommandException("Option " + option.name() + " is required");
}
}
}
for (Argument argument : arguments.keySet()) {
if (argument.required() && argumentValues.get(argument) == null) {
Command command = action.getClass().getAnnotation(Command.class);
if (command != null) {
throw new CommandException(
Ansi.ansi()
.fg(Ansi.Color.RED)
.a("Error executing command ")
.a(command.scope())
.a(":")
.a(Ansi.Attribute.INTENSITY_BOLD)
.a(command.name())
.a(Ansi.Attribute.INTENSITY_BOLD_OFF)
.a(": argument ")
.a(Ansi.Attribute.INTENSITY_BOLD)
.a(argument.name())
.a(Ansi.Attribute.INTENSITY_BOLD_OFF)
.a(" is required")
.fg(Ansi.Color.DEFAULT)
.toString(),
"Argument " + argument.name() + " is required"
);
} else {
throw new CommandException("Argument " + argument.name() + " is required");
}
}
}
// Convert and inject values
for (Map.Entry<Option, Object> entry : optionValues.entrySet()) {
Field field = options.get(entry.getKey());
Object value;
try {
value = convert(action, session, entry.getValue(), field.getGenericType());
} catch (Exception e) {
Command command = action.getClass().getAnnotation(Command.class);
if (command != null) {
throw new CommandException(
Ansi.ansi()
.fg(Ansi.Color.RED)
.a("Error executing command ")
.a(command.scope())
.a(":")
.a(Ansi.Attribute.INTENSITY_BOLD)
.a(command.name())
.a(Ansi.Attribute.INTENSITY_BOLD_OFF)
.a(": unable to convert option ")
.a(Ansi.Attribute.INTENSITY_BOLD)
.a(entry.getKey().name())
.a(Ansi.Attribute.INTENSITY_BOLD_OFF)
.a(" with value '")
.a(entry.getValue())
.a("' to type ")
.a(new GenericType(field.getGenericType()).toString())
.fg(Ansi.Color.DEFAULT)
.toString(),
"Unable to convert option " + entry.getKey().name() + " with value '"
+ entry.getValue() + "' to type " + new GenericType(field.getGenericType()).toString(),
e
);
} else {
throw new CommandException("Unable to convert option " + entry.getKey().name() + " with value '"
+ entry.getValue() + "' to type " + new GenericType(field.getGenericType()).toString(),
e);
}
}
field.setAccessible(true);
field.set(action, value);
}
for (Map.Entry<Argument, Object> entry : argumentValues.entrySet()) {
Field field = arguments.get(entry.getKey());
Object value;
try {
value = convert(action, session, entry.getValue(), field.getGenericType());
} catch (Exception e) {
Command command = action.getClass().getAnnotation(Command.class);
if (command != null) {
throw new CommandException(
Ansi.ansi()
.fg(Ansi.Color.RED)
.a("Error executing command ")
.a(command.scope())
.a(":")
.a(Ansi.Attribute.INTENSITY_BOLD)
.a(command.name())
.a(Ansi.Attribute.INTENSITY_BOLD_OFF)
.a(": unable to convert argument ")
.a(Ansi.Attribute.INTENSITY_BOLD)
.a(entry.getKey().name())
.a(Ansi.Attribute.INTENSITY_BOLD_OFF)
.a(" with value '")
.a(entry.getValue())
.a("' to type ")
.a(new GenericType(field.getGenericType()).toString())
.fg(Ansi.Color.DEFAULT)
.toString(),
"Unable to convert argument " + entry.getKey().name() + " with value '"
+ entry.getValue() + "' to type " + new GenericType(field.getGenericType()).toString(),
e
);
} else {
throw new CommandException("Unable to convert argument " + entry.getKey().name() + " with value '"
+ entry.getValue() + "' to type " + new GenericType(field.getGenericType()).toString(),
e);
}
}
field.setAccessible(true);