Search in sources :

Example 1 with Alias

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);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) LineParseException(org.fagu.fmv.cli.exception.LineParseException) ConsoleReader(jline.console.ConsoleReader) Completer(jline.console.completer.Completer) Aliases(org.fagu.fmv.cli.annotation.Aliases) AliasCustomCommandFactory(org.fagu.fmv.cli.command.AliasCustomCommandFactory) Project(org.fagu.fmv.core.project.Project) Completion(org.fagu.fmv.cli.annotation.Completion) Alias(org.fagu.fmv.cli.annotation.Alias) CompleterFactory(org.fagu.fmv.cli.completion.CompleterFactory)

Aggregations

ArrayList (java.util.ArrayList)1 ConsoleReader (jline.console.ConsoleReader)1 Completer (jline.console.completer.Completer)1 Alias (org.fagu.fmv.cli.annotation.Alias)1 Aliases (org.fagu.fmv.cli.annotation.Aliases)1 Completion (org.fagu.fmv.cli.annotation.Completion)1 AliasCustomCommandFactory (org.fagu.fmv.cli.command.AliasCustomCommandFactory)1 CompleterFactory (org.fagu.fmv.cli.completion.CompleterFactory)1 LineParseException (org.fagu.fmv.cli.exception.LineParseException)1 Project (org.fagu.fmv.core.project.Project)1