Search in sources :

Example 1 with CommandFactory

use of org.fagu.fmv.cli.CommandFactory 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 CommandFactory

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

the class Alias method run.

/**
 * @see org.fagu.fmv.cli.Command#run(java.lang.String[])
 */
@Override
public void run(String[] args) {
    if (args.length == 0) {
        // list
        for (CommandFactory commandFactory : commandBuilder.getCommandFactories()) {
            if (isAlias(commandFactory)) {
                AliasCustomCommandFactory aliasCCF = (AliasCustomCommandFactory) commandFactory;
                StringBuilder buf = new StringBuilder(20);
                buf.append("alias ").append(aliasCCF.getCommandName()).append("='").append(aliasCCF.getLine()).append('\'');
                println(buf.toString());
            }
        }
        return;
    }
    String line = StringUtils.join(args, ' ');
    int pos = line.indexOf('=');
    if (pos < 1) {
        // TODO display();
        System.out.println("beurk !");
        return;
    }
    String varName = line.substring(0, pos).trim();
    String varValue = line.substring(pos + 1).trim();
    varValue = StringUtils.strip(varValue, "\"");
    commandBuilder.addAlias(varName, varValue);
}
Also used : CommandFactory(org.fagu.fmv.cli.CommandFactory)

Example 3 with CommandFactory

use of org.fagu.fmv.cli.CommandFactory 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 CommandFactory

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

the class Unalias method run.

/**
 * @see org.fagu.fmv.cli.Command#run(java.lang.String[])
 */
@Override
public void run(String[] args) {
    CommandLine cmdLine = parse(args);
    List<String> names;
    if (cmdLine.hasOption('a')) {
        names = new ArrayList<>();
        for (CommandFactory commandFactory : commandBuilder.getCommandFactories()) {
            if (commandFactory instanceof AliasCustomCommandFactory) {
                names.add(commandFactory.getCommandName());
            }
        }
    } else {
        names = Arrays.asList(args);
    }
    for (String name : names) {
        commandBuilder.remove(name);
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) CommandFactory(org.fagu.fmv.cli.CommandFactory)

Aggregations

CommandFactory (org.fagu.fmv.cli.CommandFactory)4 LineParseException (org.fagu.fmv.cli.exception.LineParseException)2 TreeMap (java.util.TreeMap)1 CommandLine (org.apache.commons.cli.CommandLine)1