Search in sources :

Example 6 with CliHandler

use of act.handler.CliHandler in project actframework by actframework.

the class CliDispatcher method handler.

public CliHandler handler(String command) {
    String command0 = command;
    command = shortCuts.get(command);
    if (null == command) {
        command = command0;
    }
    CliHandler handler = registry.get(command);
    if (null == handler && !command.startsWith("act.")) {
        handler = registry.get("act." + command);
    }
    Act.Mode mode = Act.mode();
    if (null != handler && handler.appliedIn(mode)) {
        return handler;
    }
    return null;
}
Also used : Act(act.Act) CliHandler(act.handler.CliHandler)

Example 7 with CliHandler

use of act.handler.CliHandler in project actframework by actframework.

the class Help method showHelp.

public boolean showHelp(String command, CliContext context) {
    CliDispatcher dispatcher = context.app().cliDispatcher();
    CliHandler handler = dispatcher.handler(command);
    if (null == handler) {
        // context.println("Unrecongized command: %s", command);
        return false;
    }
    List<String> lines = new ArrayList<>();
    List<String> names = dispatcher.names(handler);
    T2<String, String> commandLine = handler.commandLine();
    lines.add("@|bold Usage|@: " + names.get(0));
    lines.add(commandLine._2);
    String summary = handler.summary();
    if (S.notBlank(summary)) {
        lines.add("");
        lines.add(summary);
    }
    List<T2<String, String>> options = handler.options();
    if (!options.isEmpty()) {
        lines.add("");
        lines.add("@|bold Options|@:");
        int maxLen = 0;
        for (T2<String, String> t2 : options) {
            maxLen = Math.max(maxLen, t2._1.length());
        }
        String fmt = "  %-" + (maxLen + 4) + "s %s";
        for (T2<String, String> t2 : options) {
            lines.add(S.fmt(fmt, t2._1, t2._2));
        }
    }
    if (names.size() > 1) {
        lines.add("");
        lines.add("@|Aliases|@: " + S.join(", ", C.list(names).tail()));
    }
    List<String> shortCuts = dispatcher.shortCuts(handler);
    if (shortCuts.size() > 0) {
        lines.add("");
        lines.add("@|bold Shortcuts|@: " + S.join(", ", shortCuts));
    }
    context.println(Ansi.ansi().render(S.join("\n", lines)).toString());
    return true;
}
Also used : CliDispatcher(act.cli.CliDispatcher) ArrayList(java.util.ArrayList) CliHandler(act.handler.CliHandler) T2(org.osgl.Osgl.T2)

Aggregations

CliHandler (act.handler.CliHandler)7 Act (act.Act)2 ArrayList (java.util.ArrayList)2 C (org.osgl.util.C)2 CliContext (act.cli.CliContext)1 CliDispatcher (act.cli.CliDispatcher)1 CliOverHttpContext (act.cli.CliOverHttpContext)1 PropertySpec (act.util.PropertySpec)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 List (java.util.List)1 Test (org.junit.Test)1 Osgl (org.osgl.Osgl)1 T2 (org.osgl.Osgl.T2)1 PostAction (org.osgl.mvc.annotation.PostAction)1 InstanceWithReturnType (testapp.cli.InstanceWithReturnType)1