help command support completion

This commit is contained in:
hengyunabc committed 2018-11-18 23:43:49 +08:00
1 parent eaae4eb72a
commit ec6456c299
4 files changed
+33 -8

No files matched your search

@@ -1,9 +1,12 @@
package com.taobao.arthas.core.command.basic1000;
import com.taobao.arthas.core.shell.cli.Completion;
import com.taobao.arthas.core.shell.cli.CompletionUtils;
import com.taobao.arthas.core.shell.command.AnnotatedCommand;
import com.taobao.arthas.core.shell.command.Command;
import com.taobao.arthas.core.shell.command.CommandProcess;
import com.taobao.arthas.core.shell.command.CommandResolver;
import com.taobao.arthas.core.shell.session.Session;
import com.taobao.arthas.core.util.usage.StyledUsageFormatter;
import com.taobao.middleware.cli.CLI;
import com.taobao.middleware.cli.annotations.Argument;
@@ -42,12 +45,7 @@ public class HelpCommand extends AnnotatedCommand {
@Override
public void process(CommandProcess process) {
List<CommandResolver> commandResolvers = process.session().getCommandResolvers();
List<Command> commands = new ArrayList<Command>();
for (CommandResolver commandResolver : commandResolvers) {
commands.addAll(commandResolver.commands());
}
List<Command> commands = allCommands(process.session());
Command targetCmd = findCommand(commands);
String message;
if (targetCmd == null) {
@@ -59,6 +57,21 @@ public class HelpCommand extends AnnotatedCommand {
process.end();
}
@Override
public void complete(Completion completion) {
List<Command> commands = allCommands(completion.session());
List<String> names = new ArrayList<String>(commands.size());
for (Command command : commands) {
CLI cli = command.cli();
if (cli == null || cli.isHidden()) {
continue;
}
names.add(command.name());
}
CompletionUtils.complete(completion, names);
}
private static String commandHelp(Command command, int width) {
return StyledUsageFormatter.styledUsage(command.cli(), width);
}
@@ -77,6 +90,15 @@ public class HelpCommand extends AnnotatedCommand {
return table;
}
private List<Command> allCommands(Session session) {
List<CommandResolver> commandResolvers = session.getCommandResolvers();
List<Command> commands = new ArrayList<Command>();
for (CommandResolver commandResolver : commandResolvers) {
commands.addAll(commandResolver.commands());
}
return commands;
}
private Command findCommand(List<Command> commands) {
for (Command command : commands) {
if (command.name().equals(cmd)) {
@@ -25,7 +25,7 @@ import com.taobao.text.util.RenderUtil;
*/
@Name("sysenv")
@Summary("Display the system env.")
@Description(Constants.EXAMPLE + "sysenv\n" + "sysenv USER\n" + Constants.WIKI + Constants.WIKI_HOME + "sysenv")
@Description(Constants.EXAMPLE + " sysenv\n" + " sysenv USER\n" + Constants.WIKI + Constants.WIKI_HOME + "sysenv")
public class SystemEnvCommand extends AnnotatedCommand {
private String envName;
@@ -23,7 +23,7 @@ import static com.taobao.text.ui.Element.label;
*/
@Name("sysprop")
@Summary("Display, and change the system properties.")
@Description(Constants.EXAMPLE + "sysprop\n"+ "sysprop file.encoding\n" + "sysprop production.mode true\n" +
@Description(Constants.EXAMPLE + " sysprop\n"+ " sysprop file.encoding\n" + " sysprop production.mode true\n" +
Constants.WIKI + Constants.WIKI_HOME + "sysprop")
public class SystemPropertyCommand extends AnnotatedCommand {
@@ -28,6 +28,9 @@ public class StyledUsageFormatter extends UsageMessageFormatter {
}
public static String styledUsage(CLI cli, int width) {
if(cli == null) {
return "";
}
StringBuilder usageBuilder = new StringBuilder();
UsageMessageFormatter formatter = new StyledUsageFormatter(Color.green);
formatter.setWidth(width);