Skip to content

Commit

Permalink
#322 update user manual for defaultValue attribute
Browse files Browse the repository at this point in the history
Closes #322
  • Loading branch information
remkop committed Jul 2, 2018
1 parent 7a94b4f commit ee88021
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
33 changes: 32 additions & 1 deletion docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<Annotated Methods,annotated methods>>, 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 <<Show Default Values,show>> the actual default value.

== Multiple Values
Multi-valued options and positional parameters are annotated fields that can capture multiple values from the command line.
Expand Down Expand Up @@ -1396,7 +1427,7 @@ Usage: <main class> <host> <port> [<files>...]

=== 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 <<Default Values,default values>> 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.

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

&#064;Override
Expand Down

0 comments on commit ee88021

Please sign in to comment.