fclp / fluent-command-line-parser

A simple, strongly typed .NET C# command line parser library using a fluent easy to use interface
Other
533 stars 86 forks source link

Any way of showing help on unknown/misspelled options? #79

Closed opinionmachine closed 7 years ago

opinionmachine commented 7 years ago

If I have declared an optional parameter such as "filepath" and some user types "filepsth" - is there any way to show help if an unknown option is specified even though no required options are missing?

I see it as follows:

  1. OK - User supplies no arguments (settings will come from elsewhere, beyond the scope of this question)
  2. OK - User supplies correct arguments -
  3. NOT OK - User supplies incorrect arguments - I assume the user mistyped and had intended to supply valid arguments instead, and thus the default case, 1 above, is not desirable.

I have tried to find a way to handle situation 3 above. The UseForEmptyArgs() on SetupHelp does NOT address this. I need a way to capture what looks like a correctly parsable option is not a valid one (again --filepsth instead of --filepath)

opinionmachine commented 7 years ago

Actually - I had, as suspected missed the AdditionalOptionsFound list. Using that and the CaptureAdditionalArguments() callback with a flag I could achieve almost what I want. My only missing feature is "> exe.exe garblage" is not flagged as being garbage, but "> exe.exe --filepsth C:\Temp" and "> exe.exe --filepath C:\Temp garblage" are both correctly flagged as mistakes.

bool otherInvalidOccurrence = false;
 commandLineParser.Setup(arguments => arguments.FilePath)
        .As("FilePath")
        .CaptureAdditionalArguments(a => otherInvalidOccurrence = true)
        .WithDescription(" - FilePath. Should be wrapped in quotes");

 if (result.AdditionalOptionsFound.Any() || otherInvalidOccurrence)
 {
        commandLineParser.HelpOption.ShowHelp(commandLineParser.Options);
        return false; // not relevant to this, basically just not running the rest of the program. 
 }