Skip to content

Enum with Flags Attribute #247

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
hallem opened this issue Feb 28, 2018 · 6 comments
Closed

Enum with Flags Attribute #247

hallem opened this issue Feb 28, 2018 · 6 comments

Comments

@hallem
Copy link

hallem commented Feb 28, 2018

I was implementing this in a few of the console applications that I'm building and found that it would be really nice to have Flags support for an Enum. Right now the only solution is to use a collection of Enum values and then parse it out in code. Which is fine, but it would be a really nice to have if the library did that for you.

@nemec
Copy link
Contributor

nemec commented Feb 28, 2018

Hi @hallem can you give a few examples of the CLI UI you would expect to see for this feature?

Something like this?

program --colors "Blue|Yellow"

(the quotes are necessary because | and & are shell operators)

@hallem
Copy link
Author

hallem commented Mar 5, 2018

yeah that would work great.

@YoungCP
Copy link

YoungCP commented Jun 11, 2018

--colors Blue,Yellow, --colors Blue+Yellow or even --colors Blue Yellow would be preferred for me... maybe make a separator configurable?

@nemec
Copy link
Contributor

nemec commented Jun 11, 2018

By the way, this is a workaround to get the behavior in the current system, but it does add an extra property to the options. I imagine it's similar to @hallem's original solution.

void Main()
{
	Parser.Default.ParseArguments<Options>("--color Blue Yellow".Split())
	.WithParsed(o => o.BackgroundColor.Dump());
}

class Options
{
	public Color BackgroundColor
	{
		get
		{
			return _BackgroundColors.Aggregate((Color)0, (o, c) => o | c);
		}
	}
	
	[Option('c', "color")]
	public IEnumerable<Color> _BackgroundColors { get; set; }
}

[Flags]
enum Color
{
	Blue = 1,
	Red = 2,
	Yellow = 4
}

@hallem
Copy link
Author

hallem commented Jun 15, 2018

@nemec That's basically what I'm doing now. Was just looking for a cleaner way to do it.

@ghost
Copy link

ghost commented Jul 25, 2018

@hallem Hi, you found solution?

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

No branches or pull requests

4 participants