mirror of
https://github.com/jlengrand/picocli.git
synced 2026-03-10 15:51:44 +00:00
PicocliCommands.commandDescription(): resolve also sub-command descs
This commit is contained in:
@@ -74,6 +74,7 @@ allprojects {
|
||||
compileTestJava.options.encoding = "UTF-8"
|
||||
|
||||
repositories {
|
||||
mavenLocal()
|
||||
jcenter()
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ ivyVersion = 2.4.0
|
||||
jacocoVersion = 0.8.2
|
||||
jansiVersion = 1.15
|
||||
jlineVersion = 2.14.6
|
||||
jline3Version = 3.14.1
|
||||
jline3Version = 3.14.2-SNAPSHOT
|
||||
junitDepVersion = 4.11
|
||||
junitVersion = 4.12
|
||||
springBootVersion = 2.2.2.RELEASE
|
||||
|
||||
@@ -84,14 +84,9 @@ public class PicocliCommands implements CommandRegistry {
|
||||
assert candidates != null;
|
||||
String word = commandLine.word();
|
||||
List<String> words = commandLine.words();
|
||||
CommandLine sub = cmd;
|
||||
for (int i = 0; i < commandLine.wordIndex(); i++) {
|
||||
if (!words.get(i).startsWith("-")) {
|
||||
sub = findSubcommandLine(sub,words.get(i));
|
||||
if (sub == null) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
CommandLine sub = findSubcommandLine(words, commandLine.wordIndex());
|
||||
if (sub == null) {
|
||||
return;
|
||||
}
|
||||
if (word.startsWith("-")) {
|
||||
String buffer = word.substring(0, commandLine.wordCursor());
|
||||
@@ -128,14 +123,28 @@ public class PicocliCommands implements CommandRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
private CommandLine findSubcommandLine(CommandLine cmdline, String command) {
|
||||
for (CommandLine s : cmdline.getSubcommands().values()) {
|
||||
if (s.getCommandName().equals(command) || Arrays.asList(s.getCommandSpec().aliases()).contains(command)) {
|
||||
return s;
|
||||
}
|
||||
|
||||
private CommandLine findSubcommandLine(List<String> args, int lastIdx) {
|
||||
CommandLine out = cmd;
|
||||
for (int i = 0; i < lastIdx; i++) {
|
||||
if (!args.get(i).startsWith("-")) {
|
||||
out = findSubcommandLine(out, args.get(i));
|
||||
if (out == null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private CommandLine findSubcommandLine(CommandLine cmdline, String command) {
|
||||
for (CommandLine s : cmdline.getSubcommands().values()) {
|
||||
if (s.getCommandName().equals(command) || Arrays.asList(s.getCommandSpec().aliases()).contains(command)) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -143,8 +152,13 @@ public class PicocliCommands implements CommandRegistry {
|
||||
* @param command
|
||||
* @return command description for JLine TailTipWidgets to be displayed in terminal status bar.
|
||||
*/
|
||||
public CmdDesc commandDescription(String command) {
|
||||
CommandSpec spec = cmd.getSubcommands().get(command).getCommandSpec();
|
||||
@Override
|
||||
public CmdDesc commandDescription(List<String> args) {
|
||||
CommandLine sub = findSubcommandLine(args, args.size());
|
||||
if (sub == null) {
|
||||
return null;
|
||||
}
|
||||
CommandSpec spec = sub.getCommandSpec();
|
||||
Help cmdhelp= new picocli.CommandLine.Help(spec);
|
||||
List<AttributedString> main = new ArrayList<>();
|
||||
Map<String, List<AttributedString>> options = new HashMap<>();
|
||||
|
||||
@@ -175,7 +175,8 @@ public class Example {
|
||||
.build();
|
||||
builtins.setLineReader(reader);
|
||||
commands.setReader(reader);
|
||||
new TailTipWidgets(reader, systemRegistry::commandDescription, 5, TipType.COMPLETER);
|
||||
TailTipWidgets ttw = new TailTipWidgets(reader, systemRegistry::commandDescription, 5, TipType.COMPLETER);
|
||||
ttw.setDescriptionCache(false);
|
||||
KeyMap<Binding> keyMap = reader.getKeyMaps().get("main");
|
||||
keyMap.bind(new Reference("tailtip-toggle"), KeyMap.alt("s"));
|
||||
|
||||
@@ -194,6 +195,7 @@ public class Example {
|
||||
} catch (EndOfFileException e) {
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
systemRegistry.trace(e);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user