Skip to content

Commit

Permalink
CommandRegistry: added registry command summary in default
Browse files Browse the repository at this point in the history
commandDescription() method
  • Loading branch information
mattirn committed Mar 21, 2020
1 parent 9ea33be commit 7318cf1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
19 changes: 15 additions & 4 deletions builtins/src/main/java/org/jline/builtins/CommandRegistry.java
Expand Up @@ -12,12 +12,11 @@
import org.jline.builtins.Widgets;
import org.jline.builtins.Options.HelpException;
import org.jline.terminal.Terminal;
import org.jline.utils.AttributedString;

import java.io.InputStream;
import java.io.PrintStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

/**
* Store command information, compile tab completers and execute registered commands.
Expand Down Expand Up @@ -110,7 +109,19 @@ default List<String> commandInfo(String command) {
*/
default Widgets.CmdDesc commandDescription(String command) {
try {
invoke(new CommandSession(), command, new Object[] {"--help"});
if (command != null) {
invoke(new CommandSession(), command, new Object[] {"--help"});
} else {
List<AttributedString> main = new ArrayList<>();
Map<String, List<AttributedString>> options = new HashMap<>();
for (String c : new TreeSet<String>(commandNames())) {
for (String info : commandInfo(c)) {
main.add(HelpException.highlightSyntax(c + " - " + info, HelpException.defaultStyle(), true));
break;
}
}
return new Widgets.CmdDesc(main, Widgets.ArgDesc.doArgNames(Arrays.asList("")), options);
}
} catch (HelpException e) {
return Builtins.compileCommandDescription(e.getMessage());
} catch (Exception e) {
Expand Down
12 changes: 10 additions & 2 deletions builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java
Expand Up @@ -287,8 +287,16 @@ public Widgets.CmdDesc commandDescription(Widgets.CmdLine line) {
if (isCommandOrScript(cmd) && !hasPipes(line.getArgs())) {
if (subcommands.containsKey(cmd)) {
List<String> args = line.getArgs();
String c = args.size() > 1 ? args.get(1) : "help";
out = subcommands.get(cmd).commandDescription(c);
String c = args.size() > 1 ? args.get(1) : null;
if (c == null || subcommands.get(cmd).hasCommand(c)) {
if (c !=null && c.equals("help")) {
out = null;
} else {
out = subcommands.get(cmd).commandDescription(c);
}
} else {
out = new Widgets.CmdDesc();
}
} else {
out = commandDescription(cmd);
}
Expand Down
35 changes: 8 additions & 27 deletions demo/src/main/java/org/jline/demo/Repl.java
Expand Up @@ -103,28 +103,9 @@ private String command(String name) {
return out;
}

@Override
public Widgets.CmdDesc commandDescription(String command) {
Widgets.CmdDesc out = new Widgets.CmdDesc();
if (!hasCommand(command)) {
return out;
}
try {
Object[] args = new Object[] {};
if (!command.equals("help")) {
args = new Object[] {"--help"};
}
invoke(new CommandSession(), command, args);
} catch (HelpException e) {
out = Builtins.compileCommandDescription(e.getMessage());
} catch (Exception e) {
}
return out;
}

private Object cmd1(Builtins.CommandInput input) {
final String[] usage = {
"cmd1 - cmd1 parse input.args, return opt.argObjects[0]",
"cmd1 - parse input.args, return opt.argObjects[0]",
"Usage: cmd1 [OBJECT]",
" -? --help Displays command help"
};
Expand All @@ -139,7 +120,7 @@ private Object cmd1(Builtins.CommandInput input) {

private Object cmd2(Builtins.CommandInput input) {
final String[] usage = {
"cmd2 - cmd2 parse input.xargs, return opt.args[0]",
"cmd2 - parse input.xargs, return opt.args[0]",
"Usage: cmd2",
" -? --help Displays command help"
};
Expand All @@ -154,7 +135,7 @@ private Object cmd2(Builtins.CommandInput input) {

private Object cmd3(Builtins.CommandInput input) {
final String[] usage = {
"cmd3 - cmd3 parse input.xargs, return opt.argObjects[0]",
"cmd3 - parse input.xargs, return opt.argObjects[0]",
"Usage: cmd3 [OBJECT]",
" -? --help Displays command help"
};
Expand All @@ -169,13 +150,13 @@ private Object cmd3(Builtins.CommandInput input) {

private Object help(Builtins.CommandInput input) {
final String[] usage = {
" - demonstrates object parameter usages. ",
"help - show summary of subcommands",
" demonstrates object parameter usages.",
" cmd3 manage correctly object parameters",
" while cmd1 & cmd2 works only with string parameters",
"Summary: " + commandInfo("cmd1").get(0),
" " + commandInfo("cmd2").get(0),
" " + commandInfo("cmd3").get(0),
" help show subcommands help"
"Summary: cmd1 " + commandInfo("cmd1").get(0),
" cmd2 " + commandInfo("cmd2").get(0),
" cmd3 " + commandInfo("cmd3").get(0)
};
Options opt = Options.compile(usage).parse(input.args());
exception = new HelpException(opt.usage());
Expand Down

0 comments on commit 7318cf1

Please sign in to comment.