Required Unless Environment Annotation

@RequiredUnlessEnvironment

This feature requires a minimum version of 2.8.5

The @RequiredUnlessEnvironment annotation is applied to a field annotated with @Option or @Arguments to indicate that the option/argument must be specified unless some environment variable is specified. This is typically used when your program automatically reads a default setting from the environment but allows the user to override it via an option e.g.

@Option(name = "--server",
        arity = 1,
        description = "Specifies the server to connect to.")
@RequiredUnlessEnvironment(variables = { "MY_SERVER" })
private String server = System.getenv("MY_SERVER");

When a field is marked with @RequiredUnlessEnvironment it only becomes required if none of the specified environment variables are set. As seen in this example your code remains responsible for populating your option/argument from the environment variable appropriately.

If the user fails to supply the option/argument and no suitable environment variable is set then an error will be thrown during parsing

If you want to require an option regardless then you should use @Required


Improving this Documentation

This documentation is itself open source and lives in GitHub under the docs/ directory.

I am not a professional technical writer and as the developer of this software I can often make assumptions of knowledge that you as a user reading this may not have. Improvements to the documentation are always welcome, if you have suggestions for the documentation please submit pull requests to the main branch.