this.command = command;
Class<?> actionClass = command.getActionClass();
// Command name completer
Command cmd = actionClass.getAnnotation(Command.class);
String[] names = scoped || Session.SCOPE_GLOBAL.equals(cmd.scope()) ? new String[] { cmd.name() } : new String[] { cmd.name(), cmd.scope() + ":" + cmd.name() };
commandCompleter = new StringsCompleter(names);
// Build options completer
for (Class<?> type = actionClass; type != null; type = type.getSuperclass()) {
for (Field field : type.getDeclaredFields()) {
Option option = field.getAnnotation(Option.class);
if (option != null) {
fields.put(option, field);
options.put(option.name(), option);
String[] aliases = option.aliases();
if (aliases != null) {
for (String alias : aliases) {
options.put(alias, option);
}
}
}
Argument argument = field.getAnnotation(Argument.class);
if (argument != null) {
Integer key = argument.index();
if (arguments.containsKey(key)) {
LOGGER.warn("Duplicate @Argument annotations on class " + type.getName() + " for index: " + key + " see: " + field);
} else {
arguments.put(key, field);
}
}
}
}
options.put(HelpOption.HELP.name(), HelpOption.HELP);
argsCompleters = new ArrayList<>();
optionsCompleter = new StringsCompleter(options.keySet());
boolean multi = false;
for (int key = 0; key < arguments.size(); key++) {
Completer completer = null;
Field field = arguments.get(key);
if (field != null) {
Argument argument = field.getAnnotation(Argument.class);
multi = (argument != null && argument.multiValued());
Completion ann = field.getAnnotation(Completion.class);
if (ann != null) {
Class<?> clazz = ann.value();
String[] value = ann.values();
if (clazz != null) {
if (value.length > 0 && clazz == StringsCompleter.class) {
completer = new StringsCompleter(value, ann.caseSensitive());
} else {
completer = command.getCompleter(clazz);
}
}
} else {
completer = getDefaultCompleter(field, multi);
}
}
if (completer == null) {
completer = NullCompleter.INSTANCE;
}
argsCompleters.add(completer);
}
if (argsCompleters.isEmpty() || !multi) {
argsCompleters.add(NullCompleter.INSTANCE);
}
optionalCompleters = new HashMap<>();
for (Option option : fields.keySet()) {
Completer completer = null;
Field field = fields.get(option);
if (field != null) {
Completion ann = field.getAnnotation(Completion.class);
if (ann != null) {
Class clazz = ann.value();
String[] value = ann.values();
if (clazz != null) {
if (clazz == StringsCompleter.class) {
completer = new StringsCompleter(value, ann.caseSensitive());
} else {
completer = command.getCompleter(clazz);
}
}
} else {