Search in sources :

Example 1 with CliDispatcher

use of act.cli.CliDispatcher in project actframework by actframework.

the class Help method handle.

@Override
public void handle(CliContext context) {
    List<String> args = context.arguments();
    String command = null;
    if (args.size() > 0) {
        command = args.get(0);
        if (showHelp(command, context)) {
            return;
        }
    }
    CommandLineParser parser = context.commandLine();
    boolean sys = parser.getBoolean("-s", "--system");
    boolean app = parser.getBoolean("-a", "--app");
    if (!sys && !app) {
        sys = true;
        app = true;
    }
    CliDispatcher dispatcher = context.app().cliDispatcher();
    List<String> sysCommands = dispatcher.commands(true, false);
    List<String> appCommands = dispatcher.commands(false, true);
    int maxLen;
    if (sys && !app) {
        maxLen = calMaxCmdLen(sysCommands);
    } else if (app && !sys) {
        maxLen = calMaxCmdLen(appCommands);
    } else {
        maxLen = calMaxCmdLen(C.list(sysCommands).append(appCommands));
    }
    String fmt = "%-" + (maxLen + 4) + "s - %s";
    if (sys) {
        list(command, "@|bold System commands|@", fmt, sysCommands, dispatcher, context);
    }
    if (app) {
        if (sys) {
            context.println("");
        }
        list(command, "@|bold Application commands|@", fmt, appCommands, dispatcher, context);
    }
}
Also used : CliDispatcher(act.cli.CliDispatcher) CommandLineParser(act.cli.util.CommandLineParser)

Example 2 with CliDispatcher

use of act.cli.CliDispatcher in project actframework by actframework.

the class CommanderByteCodeScannerTest method setup.

@Before
public void setup() throws Exception {
    super.setup();
    scanner = new CommanderByteCodeScanner();
    scannerManager = mock(AppCodeScannerManager.class);
    jobManager = mock(JobManager.class);
    classLoader = new TestingAppClassLoader(mockApp);
    dispatcher = new CliDispatcher(mockApp);
    when(mockApp.classLoader()).thenReturn(classLoader);
    infoSrc = classLoader.commanderClassMetaInfoManager();
    when(mockApp.classLoader()).thenReturn(classLoader);
    when(mockApp.cliDispatcher()).thenReturn(dispatcher);
    when(mockApp.scannerManager()).thenReturn(scannerManager);
    when(mockApp.jobManager()).thenReturn(jobManager);
    when(mockAppConfig.possibleControllerClass(anyString())).thenReturn(true);
    when(mockRouter.isActionMethod(anyString(), anyString())).thenReturn(false);
    C.List<AppByteCodeScanner> scanners = $.cast(C.listOf(scanner));
    when(scannerManager.byteCodeScanners()).thenReturn(scanners);
    scanner.setApp(mockApp);
    base = new File("./target/test-classes");
}
Also used : CliDispatcher(act.cli.CliDispatcher) AppCodeScannerManager(act.app.AppCodeScannerManager) C(org.osgl.util.C) AppByteCodeScanner(act.app.AppByteCodeScanner) TestingAppClassLoader(act.app.TestingAppClassLoader) JobManager(act.job.JobManager) File(java.io.File) Before(org.junit.Before)

Example 3 with CliDispatcher

use of act.cli.CliDispatcher 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

CliDispatcher (act.cli.CliDispatcher)3 AppByteCodeScanner (act.app.AppByteCodeScanner)1 AppCodeScannerManager (act.app.AppCodeScannerManager)1 TestingAppClassLoader (act.app.TestingAppClassLoader)1 CommandLineParser (act.cli.util.CommandLineParser)1 CliHandler (act.handler.CliHandler)1 JobManager (act.job.JobManager)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Before (org.junit.Before)1 T2 (org.osgl.Osgl.T2)1 C (org.osgl.util.C)1