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

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

Open
ClaytonHughes opened this issue May 17, 2020 · 2 comments
Open

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

ClaytonHughes opened this issue May 17, 2020 · 2 comments

Comments

@ClaytonHughes
Copy link

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)

@rmunn
Copy link
Contributor

rmunn commented Jun 10, 2020

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.

@rmunn
Copy link
Contributor

rmunn commented Aug 19, 2020

#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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants