Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong argument applied #2484

Open
zgabi opened this issue Sep 2, 2024 · 0 comments
Open

Wrong argument applied #2484

zgabi opened this issue Sep 2, 2024 · 0 comments

Comments

@zgabi
Copy link

zgabi commented Sep 2, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant