rvesse / airline

Java annotation-based framework for parsing Git like command line structures with deep extensibility
https://rvesse.github.io/airline/
Apache License 2.0
128 stars 20 forks source link

List possible enum values in the help for an Option #102

Closed dodie closed 5 years ago

dodie commented 5 years ago

Hi!

I have a command, with an enum as an option:

public class MyCommand {
...
    @Option(name = {"--my-enum"}, description = "This is MyEnum. Possible values: EXAMPLE_A, EXAMPLE_B")
    MyEnum myEnum;
...
}

Is it possible to automatically include all possible values of the enum to the description instead of hard-coding them in the description?

I've tried to include them based on MyEnum.values(), but annotations can only consume compile time constants. Is there an airline annotation (or plans to have such feature) that makes this possible?

Cheers and thank you, David

rvesse commented 5 years ago

The @AllowedEnumValues should do what you want, simply add this to the relevant option declaring the class of your enum e.g.

public class MyCommand {
...
    @Option(name = {"--my-enum"}, description = "This is MyEnum. Possible values: EXAMPLE_A, EXAMPLE_B")
        @AllowedEnumValues(MyEnum.class)
    MyEnum myEnum;
...
}

See http://rvesse.github.io/airline/guide/annotations/allowed-enum-values.html for the docs

This is a relatively new feature that was added as of the 2.7.0 release

dodie commented 5 years ago

Just what I needed, thanks, @rvesse !