Closed remkop closed 4 years ago
For testing environment variables, we can use Stephan Birkner's Environment Variables. (Part of his System Rules toolkit that extends JUnit 4 with some very useful tools.)
I added AMITest
to demonstrate how to create tests for environment variables in the option defaults.
Picocli has a feature, Variable Interpolation, that may be useful: for example, currently users need to always specify their project directory with
ami -p some/dir subcommand
.It would be nice if users can set an environment variable, like
AMIPROJECT=some/dir
, and afterwards they can simply specifyami subcommand
.The Variable Interpolation feature makes this fairly straightforward. We can define the
-p
option as follows:Then:
-p some/location
option, this specified value is used-p
is not specified, then picocli will first try to get a value from theAMIPROJECT
environment variables, or system propertiesAMIPROJECT
, then picocli will assign the default value${user.home}/amiprojects/myproject
. This contains another variable,"${user.home}"
, which is guaranteed by the Java language to have a value.We can do a similar thing for other options. Note that options with a default value are not required options (by definition).
Final note: the default value can be referenced in the description with the
${DEFAULT_VALUE}
variable: