Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# CommandLine.Parser - Use constructor that accepts Action<ParserSettings>

I was using this code, but I am getting a compiler warning that this method of creation is deprecated. As I want to remove the warning, and move to the newer version, I want to correct the code, but I can not get the CommandLineParser 1.9.7 library to work.

CommandLine.Parser OptionParser = new CommandLine.Parser(new CommandLine.ParserSettings
     {
        CaseSensitive = UseCaseSensitive,
        IgnoreUnknownArguments = IgnoreUnknownOptions,
        MutuallyExclusive = EnableMutuallyExclusive
     }
);
bool Result = OptionParser.ParseArguments(Args, this);

This code works and Result would be True/False based on the parameters of the command line and options passed. However, the following warning is posted.

Warning 1   'CommandLine.Parser.Parser(CommandLine.ParserSettings)' is obsolete: 'Use constructor that accepts Action<ParserSettings>.' 

The Online help shows this as an example for using the function.

new CommandLine.Parser(configuration: () => new CommandLine.ParserSettings(Console.Error))

I tried changing the code, but I am not getting the Lambda right, and am not sure how to get this to work. While the code executes, I only get the default functions, I can not seem to change the Case Sensitive, Mutually Exclusive, etc... options.

Line using the Constructor (from the inline IDE help)

bool Result = new CommandLine.Parser(configuration: (Settings) => new CommandLine.ParserSettings(UseCaseSensitive, EnableMutuallyExclusive, IgnoreUnknownOptions, null)).ParseArguments(Args, this);

Trying again with the virtual settings:

bool Result = new CommandLine.Parser(configuration: (Settings) => new CommandLine.ParserSettings
     {
         CaseSensitive = UseCaseSensitive,
         IgnoreUnknownArguments = IgnoreUnknownOptions,
         MutuallyExclusive = EnableMutuallyExclusive
     }
).ParseArguments(Args, this);

The online help has not kept up with the tool, and I could use any pointers someone might have. Thanks in advance...

like image 278
Steven Scott Avatar asked Dec 14 '22 20:12

Steven Scott


1 Answers

Looking at the source code the constructor runs that Action passed on new settings that it creates:

public Parser(Action<ParserSettings> configuration)
{
    if (configuration == null) throw new ArgumentNullException("configuration");
    this.settings = new ParserSettings();
    configuration(this.settings);
    this.settings.Consumed = true;
}

So in the Action<ParserSettings> you should set the values you want on the parameter, not create new settings (remember that an Action<T> is a prototype for a function that takes a T and does not return a value):

var parser = new CommandLine.Parser( s => 
{
    s.CaseSensitive = UseCaseSensitive;
} );

NOTE: The source code I linked to does not appear to be the same version as you are using since Parser( ParserSettings ) is marked internal in the source I found, which means you wouldn't even be able to call it, and some of the ParserSettings properties do not appear in the version I found. However, I believe this answer applies to the version you have as well.

like image 189
clcto Avatar answered May 11 '23 00:05

clcto