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);
}
}
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");
}
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;
}
Aggregations