j-maly / CommandLineParser

Command line parser. Declarative arguments support. Rich set of argument types (switches, enums, files, etc...). Mutually exclusive arguments validations.
MIT License
137 stars 30 forks source link

EnumeratedValueArgument w/ Enum #56

Closed Kritner closed 6 years ago

Kritner commented 6 years ago

At a glance, I'm not seeing a method of using an enum along with EnumeratedValueArgument, specifically when using a attribute style parsing target.

I was hoping for something like:

        public enum MyEnum
        {
            One,
            Two
        }

        [EnumeratedValueArgument(typeof(MyEnum), 'm', "myEnum")]
        public MyEnum ParsedEnum;

or perhaps:

public enum MyEnum
        {
            One,
            Two
        }

        public static Dictionary<MyEnum, string> EnumMapping = new Dictionary<MyEnum, string>()
        {
            { MyEnum.One, "do something with one" },
            { MyEnum.Two, "do something with two" },
        };

        [EnumeratedValueArgument(typeof(string), 'm', "myEnum", AllowedValues = string.Join(";", EnumMapping.Keys)]
        public string ParsedEnum;

but that gives:

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

any ideas on this sort of functionality?

j-maly commented 6 years ago

EnumeratedValueArgument is not the best here. It is meant for cases where you have a known set of primitive values you want to allow. E.g. 1,2,3,4,5,6

You can use plain ValueArgument:

ValueArgument<MyEnum> //(in code) 

or

[ValueArgument(typeof(MyEnum),...)] //(in attributes)

it should work fine for you

Kritner commented 6 years ago

Darn, guess no method for grabbing the list of valid values from the enum using that attribute either eh?