Skip to content
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

Help text not displaying available Enum string values, when property type is Enum[] #31

Closed
Geraint87 opened this issue Nov 18, 2013 · 3 comments

Comments

@Geraint87
Copy link

If one of my properties is of type Enum "Category", in the help text displayed, I get a very useful list of the categories available:
image

If I decorate this enum with the [Flags] attribute, and specify that multiple categories can be selected, I can change the type of my property to Category[] and PowerArgs is clever enough to parse a comma separated list of categories, and store them against my property.

But
In the help text, it no longer displays the list of Categories the user can choose from:
image

I don't think this is a Reviver issue, so is there a way to keep that functionality in?

@adamabdelhamed
Copy link
Owner

PowerArgs is even more clever than you think (just kidding). You don't need to make your argument an Enum[] to use flags support. The comma separated syntax works even with your standard enum. And you can use Enum.HasFlag(theFlag) to test for flags in your code.

I have an example in this test:

    public enum EnumWithFlags
    {
        Zero = 0,
        One = 1,
        Two = 2,
        Four = 4,
        Eight = 8,
        Sixteen = 16,
    }

    public class EnumArgsWithFlags
    {
        public EnumWithFlags Option { get; set; }
    }

    [TestMethod]
    public void TestEnumWithFlags()
    {
        var args = new string[] { "-o", "Zero,One,Two" };
        var parsed = Args.Parse<EnumArgsWithFlags>(args);
        Assert.AreEqual(EnumWithFlags.Zero | EnumWithFlags.One | EnumWithFlags.Two, parsed.Option);
    }

@Geraint87
Copy link
Author

Interesting!

What about Enums where you don't want them to be combinable? What do you think about an amended ArgRevivers.ReviveEnum(Type t, string value, bool ignoreCase):

From:

 if (value.Contains(","))
            {
                int ret = 0;
                var values = value.Split(',').Select(v => v.Trim());
                foreach (var enumValue in values)
                {
...

To:

 if (value.Contains(","))
            {
                if (!t.HasAttr<FlagsAttribute>())
                {
                    System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

                    stringBuilder.AppendLine("You cannot specify multiple values for type " + t.Name);
                    stringBuilder.AppendLine("To allow multiple values, " + t.Name + " must be decorated with the [Flags] attribute.");
                    stringBuilder.AppendLine("See MSDN for further details:");
                    stringBuilder.AppendLine(@"http://msdn.microsoft.com/en-us/library/system.flagsattribute(v=vs.110).aspx");
                    stringBuilder.AppendLine();
                    stringBuilder.AppendLine("Options are " + string.Join(", ", Enum.GetNames(t)));

                    throw new ValidationArgException(stringBuilder.ToString());
                }

                int ret = 0;
                var values = value.Split(',').Select(v => v.Trim());
                foreach (var enumValue in values)
                {
...

Or is this sort of check something that would be better placed in a custom Validator/Reviver instead?

(The message doesn't necessarily have to be that verbose)

@adamabdelhamed
Copy link
Owner

I'd rather not add more restrictions. I personally have never used that FlagsAttribute, and I can imagine that others might not be either.

Like you said, you could build a [NoFlags] validator that explicitly turns off flag support for an enum. That might be a better approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants