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

Parsed unknown flag is not being added to remaining arguments for a branch (or branch of a branch) with a default command #1607

Open
FrankRay78 opened this issue Aug 12, 2024 · 0 comments · May be fixed by #1660
Assignees
Labels
area-CLI Command-Line Interface bug Something isn't working
Milestone

Comments

@FrankRay78
Copy link
Contributor

Information

  • OS: Windows 10
  • Version: 0.49.1
  • Terminal: N/A (produced in a unit test)

Describe the bug
Parsed unknown flag is not being added to remaining arguments for a branch (or branch of a branch) with a default command.

To Reproduce

        [Fact]
        public void Should_Add_Unknown_Flags_To_Remaining_Arguments_Branch_Default_Command()
        {
            // Given
            var app = new CommandAppTester();
            app.Configure(config =>
            {
                config.AddBranch<EmptyCommandSettings>("branch", branch =>
                {
                    branch.SetDefaultCommand<EmptyCommand>();
                });
            });

            // When
            var result = app.Run("branch", "--felix");

            // Then
            result.Output.ShouldBe(string.Empty);
            result.Context.ShouldHaveRemainingArgument("--felix", new[] { (string)null });
        }

        [Fact]
        public void Should_Add_Unknown_Flags_To_Remaining_Arguments_Branch_Branch_Default_Command()
        {
            // Given
            var app = new CommandAppTester();
            app.Configure(config =>
            {
                config.AddBranch<EmptyCommandSettings>("branch", branch =>
                {
                    branch.AddBranch<EmptyCommandSettings>("hello", hello =>
                    {
                        hello.SetDefaultCommand<EmptyCommand>();
                    });
                });
            });

            // When
            var result = app.Run("branch", "hello", "--felix");

            // Then
            result.Output.ShouldBe(string.Empty);
            result.Context.ShouldHaveRemainingArgument("--felix", new[] { (string)null });
        }

Error message (showing empty remaining arguments):

 Spectre.Console.Tests.Unit.Cli.CommandAppTests+Remaining.Should_Add_Unknown_Flags_To_Remaining_Arguments_Branch_Default_Command
   Source: CommandAppTests.Remaining.cs line 41
   Duration: 46 ms

  Message: 
Shouldly.ShouldAssertException : context.Remaining.Parsed.Contains(name)
    should be
True
    but was
False

  Stack Trace: 
CommandContextExtensions.ShouldHaveRemainingArgument(CommandContext context, String name, String[] values) line 17
Remaining.Should_Add_Unknown_Flags_To_Remaining_Arguments_Branch_Default_Command() line 58
RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

Expected behavior
The above tests both pass.
The remaining arguments contain '--felix'.

Additional context
I wrote several unit tests to validate the issue only occurs when a default command has been set on a branch (or branch of a branch), see:

        [Fact]
        public void Should_Add_Unknown_Flags_To_Remaining_Arguments_Default_Command()
        {
            // Given
            var app = new CommandAppTester();
            app.SetDefaultCommand<EmptyCommand>();

            // When
            var result = app.Run("--felix");

            // Then
            result.Output.ShouldBe(string.Empty);
            result.Context.ShouldHaveRemainingArgument("--felix", new[] { (string)null });
        }

        [Fact]
        public void Should_Add_Unknown_Flags_To_Remaining_Arguments_Command()
        {
            // Given
            var app = new CommandAppTester();
            app.Configure(config =>
            {
                config.AddCommand<EmptyCommand>("empty");
            });

            // When
            var result = app.Run("empty", "--felix");

            // Then
            result.Output.ShouldBe(string.Empty);
            result.Context.ShouldHaveRemainingArgument("--felix", new[] { (string)null });
        }

        [Fact]
        public void Should_Add_Unknown_Flags_To_Remaining_Arguments_Branch_Default_Command()
        {
            // Given
            var app = new CommandAppTester();
            app.Configure(config =>
            {
                config.AddBranch<EmptyCommandSettings>("branch", branch =>
                {
                    branch.SetDefaultCommand<EmptyCommand>();
                });
            });

            // When
            var result = app.Run("branch", "--felix");

            // Then
            result.Output.ShouldBe(string.Empty);
            result.Context.ShouldHaveRemainingArgument("--felix", new[] { (string)null });
        }

        [Fact]
        public void Should_Add_Unknown_Flags_To_Remaining_Arguments_Branch_Command()
        {
            // Given
            var app = new CommandAppTester();
            app.Configure(config =>
            {
                config.AddBranch<EmptyCommandSettings>("branch", branch =>
                {
                    branch.AddCommand<EmptyCommand>("hello");
                });
            });

            // When
            var result = app.Run("branch", "hello", "--felix");

            // Then
            result.Output.ShouldBe(string.Empty);
            result.Context.ShouldHaveRemainingArgument("--felix", new[] { (string)null });
        }

        [Fact]
        public void Should_Add_Unknown_Flags_To_Remaining_Arguments_Branch_Branch_Default_Command()
        {
            // Given
            var app = new CommandAppTester();
            app.Configure(config =>
            {
                config.AddBranch<EmptyCommandSettings>("branch", branch =>
                {
                    branch.AddBranch<EmptyCommandSettings>("hello", hello =>
                    {
                        hello.SetDefaultCommand<EmptyCommand>();
                    });
                });
            });

            // When
            var result = app.Run("branch", "hello", "--felix");

            // Then
            result.Output.ShouldBe(string.Empty);
            result.Context.ShouldHaveRemainingArgument("--felix", new[] { (string)null });
        }

        [Fact]
        public void Should_Add_Unknown_Flags_To_Remaining_Arguments_Branch_Branch_Command()
        {
            // Given
            var app = new CommandAppTester();
            app.Configure(config =>
            {
                config.AddBranch<EmptyCommandSettings>("branch", branch =>
                {
                    branch.AddBranch<EmptyCommandSettings>("hello", hello =>
                    {
                        hello.AddCommand<EmptyCommand>("world");
                    });
                });
            });

            // When
            var result = app.Run("branch", "hello", "world", "--felix");

            // Then
            result.Output.ShouldBe(string.Empty);
            result.Context.ShouldHaveRemainingArgument("--felix", new[] { (string)null });
        }

image


Please upvote 👍 this issue if you are interested in it.

@FrankRay78 FrankRay78 added bug Something isn't working needs triage area-CLI Command-Line Interface and removed needs triage labels Aug 12, 2024
@FrankRay78 FrankRay78 self-assigned this Aug 12, 2024
@FrankRay78 FrankRay78 linked a pull request Aug 13, 2024 that will close this issue
@FrankRay78 FrankRay78 added this to the 0.50 milestone Oct 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-CLI Command-Line Interface bug Something isn't working
Projects
Status: PR 📬
1 participant