You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How do you create something that can parse -vvv or --verbose --verbose --verbose? The way to make an option take multiple values is by having it accept an array, but in this case, there is no values provided.
Reacted by Hong Minhee (洪 民憙), Speaqer, Anthony Truskinger, Giovanni Bassi and CEbbinghaus
More like an int to be honest. I think this may warrant a new type with just a count property. Bool array can be passed false after all. In other frameworks I've worked with, this is treated as a option of type count, as opposed to flag or value.
It's quite common to have unix programs that allow you to increase verbosity with -v, and reduce it with -q. Take this pseudo-code for instance:
varverbosity=Verbosity.Warning;varverboseOption=newOption(new[]{"--verbose","-v"}){Argument=CountOccurencesArgument/* pseudo api */};varquietOption=newOption(new[]{"--quiet","-q"}){Argument=CountOccurencesArgument/* pseudo api */}// add validation that verbose and quiet can not be used both at the same time.
command.Add(verboseOption);command.Add(quietOption);varparsed=command.Parse(args);if(command.HasOption(verboseOption)){// pretend we get count here.intverboseOccurences=command.GetOptionValue(verboseOption);verbosity+=verboseOccurences;}elseif(command.HasOption(quietOption)){// pretend we get count here.intquietOccurences=command.GetOptionValue(quietOption);verbosity-=verboseOccurences;}ConfigureLogging(verbosity);
[Edit]
In this case; doing -vvv would take verbosity from warning -> info -> debug -> trace, wherease -q would take it the other way to error. It's also been a supported use-case by all other command-line argument parsers I've used (that I can remember at least). For instance in commander and yargs (this one is IMHO closer to the API of what I would expect of System.CommandLine).
This is error prone because I'm taking the tokens that are showing in bindingContext.ParseResult.Tokens and hoping they are the same symbols as the ones defined in my option. They could mean something else, but as their name is very explicit the chance is small.
I wish this library had a better solution for this problem.
Activity
[-]Multiple flag optoins[/-][+]Multiple flag options[/+]jonsequitur commentedon Oct 31, 2019
Would you expect this to bind to, for example, an array of
bool?Alxandr commentedon Nov 1, 2019
More like an int to be honest. I think this may warrant a new type with just a count property. Bool array can be passed false after all. In other frameworks I've worked with, this is treated as a option of type count, as opposed to flag or value.
jonsequitur commentedon Nov 1, 2019
Can you describe a use case?
Alxandr commentedon Nov 1, 2019
It's quite common to have unix programs that allow you to increase verbosity with
-v, and reduce it with-q. Take this pseudo-code for instance:[Edit]
In this case; doing
-vvvwould take verbosity fromwarning->info->debug->trace, wherease-qwould take it the other way toerror. It's also been a supported use-case by all other command-line argument parsers I've used (that I can remember at least). For instance in commander and yargs (this one is IMHO closer to the API of what I would expect ofSystem.CommandLine).giggio commentedon Nov 5, 2022
I solved this in a very ugly way, but it works. It has some rough edges, but it is enough for me right now.
This is the workaround I found. This is all in a model binder class.
VerboseOptionis a field, it is defined as:This is in
GetBoundValuein model binding:This is error prone because I'm taking the tokens that are showing in
bindingContext.ParseResult.Tokensand hoping they are the same symbols as the ones defined in my option. They could mean something else, but as their name is very explicit the chance is small.I wish this library had a better solution for this problem.
Add OptionResult.IdentifierTokenCount property
Add OptionResult.IdentifierTokenCount property
Add OptionResult.IdentifierTokenCount property
Add OptionResult.IdentifierTokenCount property
Add OptionResult.IdentifierTokenCount property