Skip to content

Commit 5e773a9

Browse files
committed
[#563] Doc: Improve documentation for explicitly showing usage help from subcommands
1 parent 60f0ed7 commit 5e773a9

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

RELEASE-NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ The following information has been added to the tracing output in this release:
4646
- [#560] Enhancement: Better tracing.
4747
- [#554] Bugfix: Convenience method error handling was broken for command methods that explicitly throw an ParameterException: InvocationTargetException hides the ParameterException. Thanks to [SysLord](https://github.com/SysLord) for the bug report.
4848
- [#553] Doc: Fix broken link to CommandLine.java source code. Thanks to [Simon Legner](https://github.com/simon04) for the pull request.
49+
- [#563] Doc: Improve documentation for explicitly showing usage help from subcommands. Thanks to [Steve Johnson](https://github.com/Blatwurst) for raising this issue.
4950

5051
## <a name="3.8.1-deprecated"></a> Deprecations
5152
No features were deprecated in this release.

docs/index.adoc

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,13 +2184,32 @@ class Git {
21842184
The above example uses the <<Mixin Standard Help Options,mixinStandardHelpOptions>> attribute to mix in
21852185
<<Help Options,`usageHelp`>> and <<Help Options,`versionHelp`>> options and registers the `help` subcommand.
21862186

2187-
The usage help message for each subcommand is produced by calling `CommandLine.usage(new SubCommand(), out)`.
2188-
For example, see <<Section Headings>> for an example subcommand (`git commit`), which produces the help message shown
2189-
in <<Expanded Example>>.
2187+
TIP: Use the `@Spec` annotation for subcommands that need to show the usage help message explicitly.
21902188

21912189
From picocli 3.1, the usage help synopsis of the subcommand shows not only the subcommand name but also the parent command name.
21922190
For example, if the `git` command has a `commit` subcommand, the usage help for the `commit` subcommand shows `Usage: git commit <options>`.
21932191

2192+
If a subcommand explicitly wants to show the usage help message, the `@Spec` annotation may be useful.
2193+
The injected `CommandSpec` has its parent command initialized correctly, so the usage help can show the fully qualified name of the subcommand.
2194+
2195+
[source,java]
2196+
----
2197+
@Command(name = "commit", description = "...")
2198+
class Commit implements Runnable {
2199+
@Spec CommandSpec spec;
2200+
2201+
public void run() {
2202+
if (shouldShowHelp()) {
2203+
new CommandLine(spec).usage(System.out);
2204+
}
2205+
}
2206+
}
2207+
----
2208+
2209+
For example, see <<Section Headings>> for an example subcommand (`git commit`), which produces the help message shown
2210+
in <<Expanded Example>>.
2211+
2212+
21942213
=== Hidden Subcommands
21952214

21962215
Commands with the `hidden` attribute set to `true` will not be shown in the usage help message of their parent command.

0 commit comments

Comments
 (0)