From ee880217bcec34edc42e256ab02f04bcf531d10f Mon Sep 17 00:00:00 2001 From: Remko Popma Date: Mon, 2 Jul 2018 20:42:34 +0900 Subject: [PATCH] #322 update user manual for `defaultValue` attribute Closes #322 --- docs/index.adoc | 33 ++++++++++++++++++++++++++++++++- src/main/java/overview.html | 2 +- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/docs/index.adoc b/docs/index.adoc index aa02640a3..cb9cab0b1 100644 --- a/docs/index.adoc +++ b/docs/index.adoc @@ -552,6 +552,37 @@ class TypeDemo { } ---- +== Default Values +It is possible to define a default value for an option or positional parameter, that is assigned when the user did not specify this option or positional parameter on the command line. + +For annotated fields, it is simplest to declare the field with a value: +[source,java] +---- +@Option(names = "-c", description = "The count (default: ${DEFAULT_VALUE})") +int count = 123; // default value is 123 +---- +For <>, use the `defaultValue` annotation attribute. For example, for an annotated interface: +[source,java] +---- +interface Spec { + @Option(names = "-c", defaultValue = "123", description = "... ${DEFAULT_VALUE} ...") + int count(); +} +---- +Or similarly for an annotated concrete class: +[source,java] +---- +class Impl { + int count; + + @Option(names = "-c", defaultValue = "123", description = "... ${DEFAULT_VALUE} ...") + void setCount(int count) { + this.count = count; + } +} +---- + +Note that you can use the `${DEFAULT_VALUE}` variable in the `description` of the option or positional parameter and picocli will <> the actual default value. == Multiple Values Multi-valued options and positional parameters are annotated fields that can capture multiple values from the command line. @@ -1396,7 +1427,7 @@ Usage:
[...] === Show Default Values ==== `${DEFAULT-VALUE}` Variable -From picocli 3.2, it is possible to embed the default values in the description for an option or positional parameter by +From picocli 3.2, it is possible to embed the <> in the description for an option or positional parameter by specifying the variable `${DEFAULT-VALUE}` in the description text. Picocli uses reflection to get the default values from the annotated fields. diff --git a/src/main/java/overview.html b/src/main/java/overview.html index b2265a936..77a69572a 100644 --- a/src/main/java/overview.html +++ b/src/main/java/overview.html @@ -35,7 +35,7 @@ public static void main(String[] args) throws Exception { // CheckSum implements Callable, so parsing, error handling and handling user // requests for usage help or version help can be done with one line of code. - CommandLine.call(new CheckSum(), System.err, args); + CommandLine.call(new CheckSum(), args); } @Override