-
Notifications
You must be signed in to change notification settings - Fork 481
Test for Enum.IsDefined in TypeConverter.ToEnum is broken in the face of [Flags] #599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks @ryanmolden for feedback. |
Create a C# console app
If you invoke this app with this command line -n 2 -f 1 it prints 'Success' If you invoke it with this command line -n 2 -f 3 It prints this ERROR(S): 3 is a valid enum value for FlagsEnum, it represents IsGood|IsVeryGood, however there is no single enum member defined with a value of 3 so Enum.IsDefined says false. You should either eliminate the check entirely OR check if the given enum has the Flags attribute and if so only do the check if that attribute is not present. As it stands if I want an enum like FlagsEnum as an arg I have to type it as int (to get past this bug) and then cast it to FlagsEnum. This isn't the worst but it would be much nicer if I could just type it as what I want it to be, an enum, and the parser would do the right thing. |
The normal use case of Enum is representing value as string and limiting the permissible values within the enum string values. Help also show these values as string.
Sure enum values can be integer.
I agree with you that Can you look for the solution of Flags here. |
Representing textually is an undesirable step, the textual piece of an enum exists only for programmer convenience, underlying value are just numeric as computers don't do well with strings. Forcing me to break down a flags value into an IEnumerable is just a bad API. I get these flags values from native code in Windows, it comes to me as an ulong, having to process the info to break it down into something the command line parser can understand is just wasted time. |
The test in CommandLine.Core.TypeConverter.ToEnum which calls Enum.IsDefined is broken if the given enum has the Flags attribute (i.e it ONLY checks if the EXACT given value maps to an enum member, that will never be true for flag combinations of enum members).
This prevents having strongly types enum arguments that are passed on the command line, which is not an uncommon thing (especially in Windows where TONS of interesting data is described via flag style enums).
A workaround is to pass as a raw int/uint/ulong/whatever and just cast inside the program, but thats less ideal than just typing the command line param as what it actually is.
The text was updated successfully, but these errors were encountered: