Skip to content

Commit 695a4dc

Browse files
committed
--command-path option
1 parent cca5172 commit 695a4dc

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

cli/src/main/java/org/nasdanika/models/app/cli/HelpSiteCommand.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.util.ArrayList;
6+
import java.util.Map;
67

78
import org.eclipse.emf.common.util.DiagnosticException;
89
import org.eclipse.emf.common.util.URI;
@@ -24,6 +25,7 @@
2425
import picocli.CommandLine;
2526
import picocli.CommandLine.Command;
2627
import picocli.CommandLine.Option;
28+
import picocli.CommandLine.ParameterException;
2729
import picocli.CommandLine.Parameters;
2830

2931
@Command(
@@ -78,6 +80,21 @@ protected URI getModelURI(URI contextURI) {
7880
@Option(names = "--root-action-location", description = "Root action location")
7981
private String rootActionLocation;
8082

83+
@Option(
84+
names = "--command-path",
85+
split = ",",
86+
description = {
87+
"Comma-separated list of command names",
88+
"help is generated for the last command",
89+
"in the path"
90+
})
91+
@Description(
92+
"""
93+
Use this option if you want to generate help not for the root
94+
command, but for a sub-command.
95+
""")
96+
private String[] commandPath;
97+
8198
@Override
8299
protected int generate(Context context, ProgressMonitor progressMonitor) throws IOException, DiagnosticException {
83100
Action rootAction = AppFactory.eINSTANCE.createAction();
@@ -94,8 +111,23 @@ protected int generate(Context context, ProgressMonitor progressMonitor) throws
94111
}
95112
}
96113

114+
CommandLine command = rootCommand;
115+
if (commandPath != null) {
116+
for (String cmdName: commandPath) {
117+
Map<String, CommandLine> subCommands = command.getSubcommands();
118+
if (subCommands == null) {
119+
throw new ParameterException(command, command.getCommandName() + " has no subcommands");
120+
} else {
121+
CommandLine subCommand = subCommands.get(cmdName);
122+
if (subCommand == null) {
123+
throw new ParameterException(command, command.getCommandName() + " does not have " + cmdName + " subcommand");
124+
}
125+
command = subCommand;
126+
}
127+
}
128+
}
97129
Action rootCommandAction = ActionHelpMixIn.createCommandLineAction(
98-
rootCommand,
130+
command,
99131
documentationFactories,
100132
progressMonitor);
101133

0 commit comments

Comments
 (0)