Skip to content

Commit

Permalink
[#280] fallbackValue: move content in user manual
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop authored Jun 13, 2019
1 parent 57253eb commit 4bda6e1
Showing 1 changed file with 39 additions and 39 deletions.
78 changes: 39 additions & 39 deletions docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -795,12 +795,48 @@ public interface IDefaultValueProvider {
[#fallbackValue-annotation]
=== `fallbackValue` Annotation

For options with an optional parameter, the fallback value (introduced in picocli 4.0) is assigned
if the option is specified on the command line without an option parameter.
If an option is defined with `arity = "0..1"`, it may or not have a parameter value.
If such an option is specified without a value on the command line, it is assigned the fallback value.

The `fallbackValue` annotation was introduced in picocli 4.0; prior to this, (from picocli 2.3) an empty String was assigned.

This is different from the `defaultValue`, which is assigned if the option is not specified at all on the command line.

See <<Optional Values>> for details.
For example:

[source, java]
----
class FallbackValueDemo implements Runnable {
@Option(names = "-x", arity = "0..1",
defaultValue = "-1", fallbackValue = "-2",
description = "Optional parameter. Default: ${DEFAULT-VALUE}, " +
"if specified without parameter: ${FALLBACK-VALUE}")
int x;
public void run() { System.out.printf("x = %s%n", x); }
public static void main(String... args) {
CommandLine.run(new FallbackValueDemo(), args);
}
}
----
Gives the following results:
[source, bash]
----
java FallbackValueDemo -x 100
x = 100
java FallbackValueDemo -x
x = -2
java FallbackValueDemo
x = -1
----

Any String value is converted to the type of the option before it is assigned to the option. Options and positional parameters may define a <<Custom Type Converters,custom type converter>> if necessary.

Note that the option description may contain the `${FALLBACK-VALUE}` <<Predefined Variables,variable>> which will be replaced with the actual fallback value when the usage help is shown.


== Multiple Values
Multi-valued options and positional parameters are annotated fields that can capture multiple values from the command line.
Expand Down Expand Up @@ -944,42 +980,6 @@ When a `@Parameters` field is applied (because its index matches the index of th
=== Optional Values
If an option is defined with `arity = "0..1"`, it may or not have a parameter value.
If such an option is specified without a value on the command line, it is assigned the <<fallbackValue-annotation,fallback value>>.
The `fallbackValue` annotation was introduced in picocli 4.0; prior to this, (from picocli 2.3) an empty String was assigned.

If the option is not specified on the command line at all, it keeps its default value. For example:

[source, java]
----
class OptionalValueDemo implements Runnable {
@Option(names = "-x", arity = "0..1",
defaultValue = "-1", fallbackValue = "-2",
description = "Optional parameter. Default: ${DEFAULT-VALUE}, " +
"if specified without parameter: ${FALLBACK-VALUE}")
int x;
public void run() { System.out.printf("x = %s%n", x); }
public static void main(String... args) {
CommandLine.run(new OptionalValueDemo(), args);
}
}
----
Gives the following results:
[source, bash]
----
java OptionalValueDemo -x 100
x = 100
java OptionalValueDemo -x
x = -2
java OptionalValueDemo
x = -1
----

Any String value is converted to the type of the option before it is assigned to the option. Options and positional parameters may define a <<Custom Type Converters,custom type converter>> if necessary.

Note that the option description may contain the `${FALLBACK-VALUE}` <<Predefined Variables,variable>> which will be replaced with the actual fallback value when the usage help is shown.

== Required Arguments
=== Required Options
Expand Down

0 comments on commit 4bda6e1

Please sign in to comment.