dotnet / dotnet/command-line-api

Wrong argument applied

Open
#2,484 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

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("username");
var passwordArg = new Argument("password");
var sdateArg = new Argument("sdate");
var edateArg = new Argument("edate");
edateArg.Arity = ArgumentArity.ZeroOrOne;

var fairwayArg = new Argument("fw");
fairwayArg.Arity = ArgumentArity.ZeroOrOne;

var startRkmArg = new Argument("srk");
startRkmArg.Arity = ArgumentArity.ZeroOrOne;

var endRkmArg = new Argument("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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.