commandline
commandline copied to clipboard
Parser constructor is missing overload with ParserSettings argument
I wanted to write this code, because this seems to be the most intuitiv way:
ParserSettings settings = new()
{
AutoHelp = true,
AutoVersion = true,
CaseInsensitiveEnumValues = true,
CaseSensitive = false,
IgnoreUnknownArguments = true,
ParsingCulture = System.Globalization.CultureInfo.InvariantCulture,
};
Parser parser = new(settings);
But it doesn't work, because Parser doesn't have constructor with ParserSettings argument.
This also doesn't work:
Parser parser = new() { Settings = settings };
because Settings
is read-only.
So, there should be this constructor overload to initialize Parser with existing settings.
The constructor accepts a delegate to configure your settings.
using CommandLine;
Parser parser = new Parser(settings =>
{
settings.AutoHelp = true;
settings.AutoVersion = true;
settings.CaseInsensitiveEnumValues = true;
settings.CaseSensitive = false;
settings.IgnoreUnknownArguments = true;
settings.ParsingCulture = System.Globalization.CultureInfo.InvariantCulture;
});
You could just pass a reference to a function so that you don't have to write it multiple times.
using CommandLine;
void ConfigureParserSettings(ParserSettings settings)
{
settings.AutoHelp = true;
settings.AutoVersion = true;
settings.CaseInsensitiveEnumValues = true;
settings.CaseSensitive = false;
settings.IgnoreUnknownArguments = true;
settings.ParsingCulture = System.Globalization.CultureInfo.InvariantCulture;
}
Parser parser = new Parser(ConfigureParserSettings);
If you enable AutoHelp
most likely you want to see the output in console.
For that you need to set the HelpWriter
.
using CommandLine;
void ConfigureParserSettings(ParserSettings settings)
{
settings.AutoHelp = true;
settings.AutoVersion = true;
settings.CaseInsensitiveEnumValues = true;
settings.CaseSensitive = false;
settings.IgnoreUnknownArguments = true;
settings.ParsingCulture = System.Globalization.CultureInfo.InvariantCulture;
settings.HelpWriter = Console.Error;
}
Parser parser = new Parser(ConfigureParserSettings);