Skip to content

Commit cdee277

Browse files
committed
Small user manual fixes
1 parent 1ba1fd7 commit cdee277

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

docs/index.adoc

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -767,12 +767,14 @@ an option or positional parameter. For example:
767767

768768
[source,java]
769769
----
770-
@Parameters(arity = "3") String[] values;
770+
class OnlyThree {
771+
@Parameters(arity = "3") String[] values;
772+
}
771773
----
772774
The command has only one annotated field, `values`, and it expects exactly three arguments,
773775
so the following input results in an `UnmatchedArgumentException`:
774776
----
775-
<command> 1 2 3 4 5
777+
java OnlyThree 1 2 3 4 5
776778
----
777779

778780
Applications can change this by calling `CommandLine.setUnmatchedArgumentsAllowed(true)` before parsing the input.
@@ -843,7 +845,7 @@ Use these attributes for options that request the usage help message or version
843845
----
844846
App app = CommandLine.populateCommand(new App(), args);
845847
if (app.usageHelpRequested) {
846-
CommandLine.usage(new App(), System.err);
848+
CommandLine.usage(new App(), System.out);
847849
return;
848850
}
849851
----
@@ -859,15 +861,17 @@ usage help or version information was requested (without inspecting the annotate
859861
CommandLine commandLine = new CommandLine(new App());
860862
commandLine.parse(args);
861863
if (commandLine.isUsageHelpRequested()) {
862-
commandLine.usage(System.err);
864+
commandLine.usage(System.out);
863865
return;
864866
} else if (commandLine.isVersionHelpRequested()) {
865-
commandLine.printVersionHelp(System.err);
867+
commandLine.printVersionHelp(System.out);
866868
return;
867869
}
868870
----
869871

870-
From picocli v2.0, the <<Less Boilerplate,convenience methods>> will automatically print usage help and version information when requested with the `versionHelp` and `usageHelp` option attributes (but not for the `help` attribute).
872+
From picocli v2.0, the <<Less Boilerplate,convenience methods>> will automatically print usage help and version information
873+
when requested with the `versionHelp` and `usageHelp` option attributes (but not for the `help` attribute).
874+
From v2.0, the `help` attribute is deprecated.
871875

872876
Methods that automatically print help:
873877

@@ -902,7 +906,7 @@ annotation and prints it to the specified `PrintStream`.
902906
CommandLine commandLine = new CommandLine(new VersionedCommand());
903907
commandLine.parse(args);
904908
if (commandLine.isVersionHelpRequested()) {
905-
commandLine.printVersionHelp(System.err);
909+
commandLine.printVersionHelp(System.out);
906910
return;
907911
}
908912
----
@@ -952,14 +956,20 @@ Format argument values can be passed to the `printVersionHelp` method:
952956
----
953957
String[] args = {"1234", System.getProperty("user.name")};
954958
new CommandLine(new VersionedCommand())
955-
.printVersionHelp(System.err, Help.Ansi.AUTO, args);
959+
.printVersionHelp(System.out, Help.Ansi.AUTO, args);
956960
----
957961

958962
=== Dynamic Version Information
959963
From picocli 2.2, the `@Command` annotation supports a `versionProvider` attribute.
960964
Applications may specify a `IVersionProvider` implementation in this attribute, and picocli will instantiate this class
961965
and invoke it to collect version information.
962966

967+
[source,java]
968+
----
969+
@Command(versionProvider = com.my.custom.ManifestVersionProvider.class)
970+
class App { ... }
971+
----
972+
963973
This is useful when the version of an application should be detected dynamically at runtime.
964974
For example, an implementation may return version information obtained from the JAR manifest, a properties file or some other source.
965975

@@ -1078,7 +1088,7 @@ Usage: <main class> [OPTIONS] [<files>...]
10781088
Note that the positional parameters are not abbreviated.
10791089
[source,java]
10801090
----
1081-
@CommandLine.Command(abbreviateSynopsis = true)
1091+
@Command(abbreviateSynopsis = true)
10821092
class App {
10831093
@Parameters File[] files;
10841094
@Option(names = {"--count", "-c"}) int count;
@@ -1119,7 +1129,7 @@ The `headerHeading` and `footerHeading` may contain format specifiers. See <<Sec
11191129
Section headers can be used to make usage message layout appear more spacious. Section headings may contain embedded line separator (`%n`) format specifiers:
11201130
[source,java]
11211131
----
1122-
@CommandLine.Command(name = "git-commit",
1132+
@Command(name = "git-commit",
11231133
sortOptions = false,
11241134
headerHeading = "Usage:%n%n",
11251135
synopsisHeading = "%n",
@@ -1631,7 +1641,13 @@ class FileUtils {
16311641
description = "this option applies to all subcommands")
16321642
File baseDirectory;
16331643
}
1644+
----
1645+
1646+
The above top-level command has a `--directory` option that applies to its subcommands.
1647+
The `List` subcommand can use the `@ParentCommand` annotation to get a reference to the parent command, so it can easily access the parent command options.
16341648

1649+
[source,java]
1650+
----
16351651
@Command(name = "list")
16361652
class List implements Runnable {
16371653
@@ -1673,6 +1689,7 @@ CommandLine commandLine = new CommandLine(new Git());
16731689
// add subcommands programmatically (not necessary if the parent command
16741690
// declaratively registers the subcommands via annotation)
16751691
commandLine.addSubcommand("status", new GitStatus());
1692+
commandLine.addSubcommand("commit", new GitCommit());
16761693
...
16771694
commandLine.usage(System.out);
16781695
----
@@ -1785,10 +1802,10 @@ CommandLine cmd = new CommandLine(callable);
17851802
try {
17861803
cmd.parse(args);
17871804
if (cmd.isUsageHelpRequested()) {
1788-
cmd.usage(System.err);
1805+
cmd.usage(System.out);
17891806
return null;
17901807
} else if (cmd.isVersionHelpRequested()) {
1791-
cmd.printVersionHelp(System.err);
1808+
cmd.printVersionHelp(System.out);
17921809
return null;
17931810
}
17941811
return callable.call();
@@ -1834,7 +1851,7 @@ CommandLine cmd = new CommandLine(MyTopLevelCommand())
18341851
.addSubcommand("status", new GitStatus())
18351852
.addSubcommand("commit", new GitCommit())
18361853
.addSubcommand("add", new GitAdd());
1837-
List<Object result = cmd.parseWithHandler(new RunAll(), System.err, args);
1854+
List<Object> result = cmd.parseWithHandler(new RunAll(), System.err, args);
18381855
----
18391856

18401857
The `CommandLine::parseWithHandler` method will take care of the following:
@@ -2135,7 +2152,7 @@ import static picocli.CommandLine.*
21352152
@Field boolean helpRequested;
21362153
21372154
//if (helpRequested) { // not necessary: PicocliBaseScript takes care of this
2138-
// CommandLine.usage(this, System.err); return 0;
2155+
// CommandLine.usage(this, System.out); return 0;
21392156
//}
21402157
count.times {
21412158
println "hi"
@@ -2166,15 +2183,15 @@ class MyApp : Runnable {
21662183
21672184
@Option(names = ["-c", "--count"], paramLabel = "COUNT",
21682185
description = ["the count"])
2169-
private val count: Int = 0
2186+
private var count: Int = 0
21702187
21712188
@Option(names = ["-h", "--help"], usageHelp = true,
21722189
description = ["print this help and exit"])
2173-
private val helpRequested: Boolean = false
2190+
private var helpRequested: Boolean = false
21742191
21752192
@Option(names = ["-V", "--version"], versionHelp = true,
21762193
description = ["print version info and exit"])
2177-
private val versionRequested: Boolean = false
2194+
private var versionRequested: Boolean = false
21782195
21792196
override fun run() {
21802197
for (i in 0 until count) {
@@ -2199,15 +2216,15 @@ class MyApp : Runnable {
21992216
22002217
@Option(names = arrayOf("-c", "--count"), paramLabel = "COUNT",
22012218
description = arrayOf("the count"))
2202-
private val count: Int = 0
2219+
private var count: Int = 0
22032220
22042221
@Option(names = arrayOf("-h", "--help"), usageHelp = true,
22052222
description = arrayOf("print this help and exit"))
2206-
private val helpRequested: Boolean = false
2223+
private var helpRequested: Boolean = false
22072224
22082225
@Option(names = arrayOf("-V", "--version"), versionHelp = true,
22092226
description = arrayOf("print version info and exit"))
2210-
private val versionRequested: Boolean = false
2227+
private var versionRequested: Boolean = false
22112228
22122229
override fun run() {
22132230
for (i in 0 until count) {

0 commit comments

Comments
 (0)