Skip to content

Multiple flag options #669

Closed
Closed

Description

@Alxandr

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.

Activity

  1. changed the title [-]Multiple flag optoins[/-] [+]Multiple flag options[/+] on Oct 31, 2019
  2. jonsequitur commented on Oct 31, 2019

    @jonsequitur
    Contributor

    Would you expect this to bind to, for example, an array of bool?

  3. Alxandr commented on Nov 1, 2019

    @Alxandr
    ContributorAuthor

    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.

  4. jonsequitur commented on Nov 1, 2019

    @jonsequitur
    Contributor

    Can you describe a use case?

  5. Alxandr commented on Nov 1, 2019

    @Alxandr
    ContributorAuthor

    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:

    var verbosity = Verbosity.Warning;
    var verboseOption = new Option(new[] { "--verbose", "-v" }) { Argument = CountOccurencesArgument /* pseudo api */ };
    var quietOption = new Option(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);
    var parsed = command.Parse(args);
    
    if (command.HasOption(verboseOption)) {
      // pretend we get count here.
      int verboseOccurences = command.GetOptionValue(verboseOption);
      verbosity += verboseOccurences;
    } else if (command.HasOption(quietOption)) {
      // pretend we get count here.
      int quietOccurences = 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).

  6. added
    enhancement
    on Nov 1, 2019
  7. giggio commented on Nov 5, 2022

    @giggio

    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.

    VerboseOption is a field, it is defined as:

    VerboseOption = new Option<bool>(aliases: new[] { "-v", "-vv", "--verbose" }, description: "Show verbose output.");

    This is in GetBoundValue in model binding:

    var tokens = bindingContext.ParseResult.Tokens;
    var matchedTokens = tokens.Select(token => token.Value).Where(token => VerboseOption.Aliases.Any(alias => alias == token)).ToList();
    var verbosity = matchedTokens.Count switch
    {
        0 => Verbosity.Quiet,
        1 => matchedTokens.Any(token => token == "-vv") ? Verbosity.VeryVerbose : Verbosity.Verbose,
        _ => Verbosity.VeryVerbose,
    };
    return new(verbosity);

    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.

  8. self-assigned this
    on Apr 12, 2023
  9. added this to the 2.0 GA milestone on Apr 12, 2023
  10. added 4 commits that reference this issue on Apr 12, 2023
    4fbed6f
    04fdcce
    fa144c8
    916d421
  11. added a commit that references this issue on Jan 28, 2024
    ad5aed4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area-Parser and Binderenhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions