Skip to content

EnumSet<> options #628

@leeatkinson

Description

@leeatkinson

I have a enum and I want an argument to be an EnumSet so I can set flags on the CLI, for example:

foo --result-types PARTIAL,COMPLETE

with code similar to:

public enum ResultTypes {
    NONE,
    PARTIAL,
    COMPLETE
}

@Option(names = "--result-types")
private EnumSet<ResultTypes> resultTypes = EnumSet.of(ResultTypes.COMPLETE);

But I get the error when executed:

Invalid value for option '--result-types' (<ResultTypes>): expected one of [NONE, PARTIAL, COMPLETE] (case-sensitive) but was 'PARTIAL,COMPLETE'

I have written a converter to parse the string:

public class ResultTypesEnumSetConverter implements CommandLine.ITypeConverter<EnumSet<ResultTypes>> {
    @Override
    public EnumSet<ResultTypes> convert(String value) throws Exception {
        String[] split = value.split(",");
        EnumSet<ResultTypes> types = EnumSet.noneOf(ResultTypes.class);
        for (int i = 0; i < split.length; i++) {
            types.add(ResultTypes.valueOf(split[i]));
        }
        return types;
    }
}

but this doesn't seem to work:

InstantiationException: java.util.EnumSet while processing argument at or before arg[1] 'PARTIAL,COMPLETE'

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions