Skip to content

Is it possible to use repeated non-option-argument options? #631

Open

Description

@ClaytonHughes

ssh, tar, and other common tools allow for multiple non-option-argument options and act on their total count. (Implementing this with getopt is pretty trivial).

e.g. tar -cvf and tar -cvvf produce different output.

Is there a way to do this with CommandLine? I did not see anything promising in the documentation or examples/source code I perused, but I may have missed something.

(This might just be related to #594, but the issue there seems to be with repeated options with required option-arguments)

Activity

  1. rmunn commented on Jun 10, 2020

    @rmunn
    Contributor

    PR #607 (not yet merged) includes a feature that would enable the use of -vv and similar repeated non-option-argument options. It adds a new Option property called FlagCounter, which should be set on an int property:

    [Option('v', "--verbose", FlagCounter=true)]
    int Verbose { get; set; }

    This will be 0 if the -v option was not passed, 1 if -v was passed, 2 if -vv or -v -v was passed, and so on. If you want dueling --verbose and --quiet options, you could do it like this:

    [Option('v', "--verbose", FlagCounter=true)]
    int Verbose { get; set; }
    
    [Option('q', "--quiet", FlagCounter=true)]
    int Quiet { get; set; }
    
    int Verbosity => Verbose - Quiet;  // C# 6 and up, see https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members#read-only-properties

    Then your code could do something like:

    if (options.Verbosity > 2) {
        LogDebug();
    } else if (options.Verbosity == 2) {
        LogVerbose();
    } else if (options.Verbosity == 1) {
        Log();
    } else {
        DoNotLogBecauseQuietMode();
    }

    BTW, if you want to see #607 merged sooner, stop by #601 and express your opinion of whether CommandLineParser should follow the GNU standards (mixed options and non-option arguments by default) or the POSIX standards (stop processing after you find the first non-option argument) by default. That's the current sticking point that's delaying #607 getting merged.

  2. rmunn commented on Aug 19, 2020

    @rmunn
    Contributor

    #607 has been closed in favor of #684, which still includes the FlagCounter feature that I described.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions