command-line-api
command-line-api copied to clipboard
Wrong argument applied
Hi,
I have the following command line parameters:
static void Main(string[] args)
{
args = new string[] { "e", "myuser", "mypass", "2020.01.01 01:00", "2021.01.01 01:00" };
var rootCommand = new RootCommand();
var export = new Command("export", "Export");
rootCommand.Add(export);
export.AddAlias("e");
var userNameArg = new Argument<string>("username");
var passwordArg = new Argument<string>("password");
var sdateArg = new Argument<DateTime>("sdate");
var edateArg = new Argument<DateTime>("edate");
edateArg.Arity = ArgumentArity.ZeroOrOne;
var fairwayArg = new Argument<string>("fw");
fairwayArg.Arity = ArgumentArity.ZeroOrOne;
var startRkmArg = new Argument<double>("srk");
startRkmArg.Arity = ArgumentArity.ZeroOrOne;
var endRkmArg = new Argument<double>("erk");
endRkmArg.Arity = ArgumentArity.ZeroOrOne;
export.AddArgument(userNameArg);
export.AddArgument(passwordArg);
export.AddArgument(sdateArg);
export.AddArgument(edateArg);
export.AddArgument(fairwayArg);
export.AddArgument(startRkmArg);
export.AddArgument(endRkmArg);
export.SetHandler(context =>
{
string userName = context.ParseResult.GetValueForArgument(userNameArg);
string password = context.ParseResult.GetValueForArgument(passwordArg);
var sDate = context.ParseResult.GetValueForArgument(sdateArg);
var eDate = context.ParseResult.GetValueForArgument(edateArg);
string fairway = context.ParseResult.GetValueForArgument(fairwayArg);
double startRkm = context.ParseResult.GetValueForArgument(startRkmArg);
double endRkm = context.ParseResult.GetValueForArgument(endRkmArg);
Console.WriteLine($"""
user: {userName}
pass: {password}
sdate: {sDate}
edate: {eDate}
fw: {fairway}
srk: {startRkm}
erk: {endRkm}
""");
Console.ReadLine();
});
var parser = new CommandLineBuilder(rootCommand).UseDefaults().Build();
parser.Invoke(args);
}
When I run this code the result will be:
user: myuser
pass: mypass
sdate: 2020. 01. 01. 1:00:00
edate: 2021. 01. 01. 1:00:00
fw: mypass
srk: 0
erk: 0
So the 2nd "mypass" argument will be applied to the "fw" argiment. Why? How can I solve this?
I'm using the currelty latest version (2.0.0-beta4.22272.1)
Thanks, Gábor