Search in sources :

Example 1 with LineParseException

use of org.fagu.fmv.cli.exception.LineParseException in project fmv by f-agu.

the class Help method run.

/**
 * @see org.fagu.fmv.cli.Command#run(java.lang.String[])
 */
@Override
public void run(String[] args) {
    if (args.length == 0) {
        listAllCommands();
        return;
    }
    for (String arg : args) {
        CommandFactory commandFactory = commandBuilder.getCommandFactory(arg);
        if (commandFactory == null) {
            println("Command not found: " + arg);
            continue;
        }
        org.fagu.fmv.cli.Command command = null;
        try {
            command = commandFactory.create(consoleReader, commandBuilder, project, arg, args);
        } catch (LineParseException e) {
            continue;
        }
        command.help();
    }
}
Also used : LineParseException(org.fagu.fmv.cli.exception.LineParseException) CommandFactory(org.fagu.fmv.cli.CommandFactory)

Example 2 with LineParseException

use of org.fagu.fmv.cli.exception.LineParseException 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)

Example 3 with LineParseException

use of org.fagu.fmv.cli.exception.LineParseException in project fmv by f-agu.

the class Help method listAllCommands.

// *****************************************
/**
 */
protected void listAllCommands() {
    TreeMap<String, HelpTable> cmdFctMap = new TreeMap<>();
    for (CommandFactory commandFactory : commandBuilder.getCommandFactories()) {
        String commandName = commandFactory.getCommandName();
        if (org.fagu.fmv.cli.command.Alias.isAlias(commandFactory)) {
            String mainCmd = org.fagu.fmv.cli.command.Alias.getAliasCommand(commandFactory);
            cmdFctMap.computeIfAbsent(mainCmd, HelpTable::new).addAlias(commandName);
            continue;
        } else {
            HelpTable helpTable = cmdFctMap.computeIfAbsent(commandName, HelpTable::new);
            org.fagu.fmv.cli.Command cmd;
            try {
                cmd = commandFactory.create(consoleReader, commandBuilder, project, "", null);
            } catch (LineParseException e) {
                throw new RuntimeException(e);
            }
            helpTable.setDescription(cmd.getShortDescription());
        }
    }
    int maxLen = cmdFctMap.values().stream().mapToInt(ht -> ht.getNames().length()).max().orElse(0);
    for (HelpTable helpTable : cmdFctMap.values()) {
        StringBuilder buf = new StringBuilder(40);
        buf.append("  ").append(StringUtils.rightPad(helpTable.getNames(), maxLen)).append("  ").append(helpTable.description);
        println(buf.toString());
    }
}
Also used : LineParseException(org.fagu.fmv.cli.exception.LineParseException) TreeMap(java.util.TreeMap) CommandFactory(org.fagu.fmv.cli.CommandFactory)

Example 4 with LineParseException

use of org.fagu.fmv.cli.exception.LineParseException in project fmv by f-agu.

the class DefaultCommandBuilder method createAndExec.

/**
 * @see org.fagu.fmv.cli.CommandBuilder#createAndExec(java.lang.String)
 */
@Override
public Command createAndExec(String line) throws LineParseException {
    CommandLine parse = new CommandLine("ignore");
    parse.addArguments(line, false);
    String[] arguments = parse.getArguments();
    String executable = arguments[0];
    CommandFactory commandFactory = cmdBuilderMap.get(executable.toLowerCase());
    if (commandFactory == null) {
        // command not found
        throw new LineParseException(line);
    }
    String[] args = new String[arguments.length - 1];
    System.arraycopy(arguments, 1, args, 0, arguments.length - 1);
    Command command = commandFactory.create(consoleReader, this, project, commandFactory.getCommandName(), args);
    Options options = new Options();
    options.addOption("h", "help", false, "");
    CommandLineParser parser = new GnuParser();
    try {
        org.apache.commons.cli.CommandLine commandLine = parser.parse(options, args);
        if (commandLine.hasOption('h')) {
            command.help();
            return command;
        }
    } catch (ParseException e) {
    // ignore
    }
    command.run(args);
    return command;
}
Also used : Options(org.apache.commons.cli.Options) GnuParser(org.apache.commons.cli.GnuParser) AliasCustomCommandFactory(org.fagu.fmv.cli.command.AliasCustomCommandFactory) LineParseException(org.fagu.fmv.cli.exception.LineParseException) CommandLine(org.apache.commons.exec.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) LineParseException(org.fagu.fmv.cli.exception.LineParseException) ParseException(org.apache.commons.cli.ParseException)

Aggregations

LineParseException (org.fagu.fmv.cli.exception.LineParseException)4 CommandFactory (org.fagu.fmv.cli.CommandFactory)2 AliasCustomCommandFactory (org.fagu.fmv.cli.command.AliasCustomCommandFactory)2 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 ConsoleReader (jline.console.ConsoleReader)1 Completer (jline.console.completer.Completer)1 CommandLineParser (org.apache.commons.cli.CommandLineParser)1 GnuParser (org.apache.commons.cli.GnuParser)1 Options (org.apache.commons.cli.Options)1 ParseException (org.apache.commons.cli.ParseException)1 CommandLine (org.apache.commons.exec.CommandLine)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 CompleterFactory (org.fagu.fmv.cli.completion.CompleterFactory)1 Project (org.fagu.fmv.core.project.Project)1