petermr / ami3

Integration of cephis and normami code into a single base. Tests will be slimmed down
Apache License 2.0
17 stars 5 forks source link

Allow users to define default values with environment variables #64

Closed remkop closed 4 years ago

remkop commented 4 years ago

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 specify ami subcommand.

The Variable Interpolation feature makes this fairly straightforward. We can define the -p option as follows:

@Option(names = {"-p", "--cproject"}, 
        defaultValue = "${AMIPROJECT:-${user.home}/amiprojects/myproject}", 
        paramLabel = "DIR", ...

Then:

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:

@Option(names = {"-p", "--cproject"}, 
    defaultValue = "${AMIPROJECT:-${user.home}/amiprojects/myproject}",
    description = "The CProject (directory) to process."
            + " ..."
            + " The default is: `${DEFAULT-VALUE}`."
remkop commented 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.)

remkop commented 4 years ago

I added AMITest to demonstrate how to create tests for environment variables in the option defaults.