Search in sources :

Example 1 with CommandDispatcher

use of henplus.CommandDispatcher in project henplus by neurolabs.

the class HelpCommand method execute.

/**
     * execute the command given.
     */
@Override
public int execute(final SQLSession session, final String cmdstr, String param) {
    final StringTokenizer st = new StringTokenizer(param);
    if (st.countTokens() > 1) {
        return SYNTAX_ERROR;
    }
    param = st.hasMoreElements() ? (String) st.nextElement() : null;
    /*
         * nothing given: provide generic help.
         */
    if (param == null) {
        final Iterator<Command> it = HenPlus.getInstance().getDispatcher().getRegisteredCommands();
        while (it.hasNext()) {
            final Command cmd = it.next();
            final String description = cmd.getShortDescription();
            if (description == null) {
                continue;
            }
            final StringBuilder cmdPrint = new StringBuilder(" ");
            final String[] cmds = cmd.getCommandList();
            final String firstSynopsis = cmd.getSynopsis(cmds[0]);
            /*
                 * either print a list of known commands or the complete
                 * synopsis, if there is only one command.
                 */
            if (cmds.length > 1 || firstSynopsis == null) {
                for (int i = 0; i < cmds.length; ++i) {
                    if (i != 0) {
                        cmdPrint.append(" | ");
                    }
                    cmdPrint.append(cmds[i]);
                }
            } else {
                cmdPrint.append(firstSynopsis.length() < INDENT ? firstSynopsis : cmds[0]);
            }
            HenPlus.msg().print(cmdPrint.toString());
            for (int i = cmdPrint.length(); i < INDENT; ++i) {
                HenPlus.msg().print(" ");
            }
            HenPlus.msg().print(": ");
            HenPlus.msg().println(description);
        }
        HenPlus.msg().println("Full documentation at http://henplus.sf.net/");
        HenPlus.msg().println("config read from [" + HenPlus.getInstance().getConfigurationDirectoryInfo() + "]");
    } else {
        final CommandDispatcher disp = HenPlus.getInstance().getDispatcher();
        final String cmdString = disp.getCommandNameFrom(param);
        final Command c = disp.getCommandFrom(param);
        if (c == null) {
            HenPlus.msg().println("Help: unknown command '" + param + "'");
            return EXEC_FAILED;
        }
        printDescription(cmdString, c);
    }
    return SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) AbstractCommand(henplus.AbstractCommand) Command(henplus.Command) CommandDispatcher(henplus.CommandDispatcher)

Aggregations

AbstractCommand (henplus.AbstractCommand)1 Command (henplus.Command)1 CommandDispatcher (henplus.CommandDispatcher)1 StringTokenizer (java.util.StringTokenizer)1