use of org.fagu.fmv.cli.annotation.Alias in project fmv by f-agu.
the class DefaultCommandBuilder method add.
/**
* @see org.fagu.fmv.cli.CommandBuilder#add(java.lang.Class)
*/
@Override
public void add(final Class<? extends Command> cmdClass) {
final org.fagu.fmv.cli.annotation.Command cmd = cmdClass.getAnnotation(org.fagu.fmv.cli.annotation.Command.class);
if (cmd == null) {
throw new RuntimeException("Annotation @Command not found: " + cmdClass.getName());
}
add(new CommandFactory() {
/**
* @see org.fagu.fmv.cli.CommandFactory#getCommandName()
*/
@Override
public String getCommandName() {
return cmd.value();
}
/**
* @see org.fagu.fmv.cli.CommandFactory#create(jline.console.ConsoleReader, org.fagu.fmv.cli.CommandBuilder,
* org.fagu.fmv.core.project.Project, java.lang.String, String[])
*/
@Override
public Command create(ConsoleReader consoleReader, CommandBuilder commandBuilder, Project project, String executable, String[] args) throws LineParseException {
return build(cmdClass);
}
});
// alias
List<Alias> aliases = new ArrayList<>();
Aliases aliasesAnno = cmdClass.getAnnotation(Aliases.class);
if (aliasesAnno != null) {
aliases.addAll(Arrays.asList(aliasesAnno.value()));
}
Alias aliasAnno = cmdClass.getAnnotation(Alias.class);
if (aliasAnno != null) {
aliases.add(aliasAnno);
}
if (!aliases.isEmpty()) {
for (Alias alias : aliases) {
String aliasCommand = alias.command();
if (StringUtils.isBlank(aliasCommand)) {
aliasCommand = cmd.value();
}
for (String aliasName : alias.value()) {
addAlias(aliasName, aliasCommand);
}
}
}
// completion
Completion completion = cmdClass.getAnnotation(Completion.class);
if (completion != null) {
try {
@SuppressWarnings("unchecked") Class<CompleterFactory> completerFactoryClass = (Class<CompleterFactory>) Class.forName(completion.value());
CompleterFactory completerFactory = completerFactoryClass.newInstance();
Completer completer = completerFactory.create(cmd.value());
completerMap.add(cmd.value(), completer);
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
Aggregations