AllowedRawValues Annotation

@AllowedRawValues

The @AllowedRawValues annotation may be applied to fields annotated with @Option and @Arguments to limit the set of values that an option may be used with e.g.

@Option(name = { "-v", "--verbosity" }, arity = 1, title = "Level", description = "Sets the desired verbosity")
@AllowedRawValues(allowedValues = { "1", "2", "3" })
public int verbosity = 1;

This specifies that the --verbosity option only allows the values 1, 2 and 3 to be specified by the user. Any other value will be rejected.

Note that the restriction applies to the raw string provided to the parser prior to converting it to the target type. So in the above example the values would be checked prior to parsing them as integers.

If your list of allowed values is from an enum you can simplify your code by using @AllowedEnumValues to reference the relevant enumeration.

If there may be more than one way that the same value may be specified then it may be useful to use the @AllowedValues annotation instead.

For more complex value restrictions a regular expression based restriction using @Pattern might be appropriate.


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.