-
Notifications
You must be signed in to change notification settings - Fork 481
Multi Culture Support
The parser is culture-aware. The args[] array passed to ParseArguments(...) is a string array, so when the parser loads data into target instance every string undergoes a conversion.
Suppose you define an Option class like:
class Options {
[Option('v', "value")]
public double SomeValue { get; set; }
}
If your system use a dot as decimal separator and you type the following command line:
$ app -v 10,4
the parsing process will fail.
If you want that parsing occurs with a particular culture, just set desired CultureInfo in settings instance.
var parser = new CommandLine.Parser(with => with.ParsingCulture = new CultureInfo("it-IT"));
Remarks: CommandLine.Parser.Default is a singleton uses CultureInfo.InvariantCulture
.
Double infinity ∞
which is represented by the constant Double.PositiveInfinity
is not supported in Parser.Default because it uses CultureInfo.InvariantCulture
.
To parse the Infinity ∞ , you should use custom parser and set ParsingCulture, like 'en-Us':
var parser = new Parser(with =>
{
//setup culture for parser 'en-US', for example
with.ParsingCulture = new CultureInfo("en-US"); //modify to use your cultureinfo
});
var result = parser.ParseArguments<Options>(args);
-e ∞ -m 50 -p -w 0.25
If you want to use Parser.Default
, you can use Infinity
as a symbol value, e.g.
#-e ∞ -m 50 -p -w 0.25 //∞ not supported in Parser.Default
-e Infinity -m 50 -p -w 0.25 # use Infinity instead of ∞
CultureInfo ci = new CultureInfo("it-IT");
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;