commandlineparser / commandline

The best C# command line parser that brings standardized *nix getopt style, for .NET. Includes F# support
MIT License
4.6k stars 480 forks source link

General Questions #364

Closed Korporal closed 4 years ago

Korporal commented 6 years ago

I have some basic questions, I skimmed the docs and got a feel for the approach but have more specific questions.

Is there a list of the kinds of arguments supported? like optional args, switch args (on/off), args that can take one of a fixed set of values (e.g. -output_type fixed, -output_type float, -output_type int)? args that must be numeric and perhaps fit some designated range? Is there a way to do a final consistency check to make sure there are no incompatible args which might be OK on their own but not together?

I should explain too that I once used a very powerful command line parser when I worked on Stratus fault tolerant minicomputers in the 1980s, these ran VOS a derivative the Multics.

Their approach was a function that took a set of descriptors, it worked well and I wonder how your stuff compares with this?

Here's their documentation for this.

Thanks

gauranitai commented 6 years ago

I would love to see an argument that takes a value from set of predefined options like enums. For example logLevel would take Info, Error, Warning Debug but not other values. Parsed example: app --logLevel=Debug Unparssed Example: app --logLevel=Exceptions

nemec commented 6 years ago

Hey @Korporal, for the most part arguments simply consume strings and use the IConvertible interface to transform them into objects (via Convert.ChangeType). Two exceptions are:

  1. booleans are flags, they do not consume any values. -f sets the bool to true, otherwise it isn't set.
  2. Enums are supported even though they don't implement IConvertible. By default, parsing is case sensitive, although that can be configured if you don't use the Default parser. Enums are restricted to only valid values and throw an error otherwise. No support for "bitflags" e.g. Debug|Error
gauranitai commented 6 years ago

@nemec can you please share an example code on how to use Enums as values for commandline arguments ?

nemec commented 6 years ago

Have you tried

public enum LogLevel
{
    Debug,
    Warning,
    Error
}

public class Options
{
    [Option("logLevel")]
    public LogLevel Level { get; set; }
}
gauranitai commented 6 years ago

@nemec thank you, it accepts enum and if an invalid string is given it says: Option 'LogLevel' is defined with a bad format. Required option 'LogLevel' is missing.

I guess that's good enough and if we include the options in help then we should be good.

[Option("logLevel", Required = true, HelpText="Loglevel with values Debug, Warning or Error")]
            public LogLevel Level
            {
                get; set;
            }