Skip to content

When using UseCommandHandler with subcomand, Help overrides RootCommand  #1344

Open
@leonardochaia

Description

@leonardochaia

Hi all,

I love this project. We are using it to build dotnet-affected!

I'm not sure if this is a known issue, but I'm migrating to use the Hosting setup and I'm seeing different behavior between declaring the RootCommand with a Handler, and binding a handler through the HostBuilder.

It only happens when using a subcommand. If I remove the subcommad, it works.

For example:

public class PublicApiTests
{
    private class SubCommand : Command
    {
        public SubCommand() 
            : base("thing")
        {
        }
    }
    private class HelloRootCommand : RootCommand
    {
        public HelloRootCommand()
        {
            this.AddCommand(new SubCommand());
        }
    }
    
    private class HelloHandler : ICommandHandler
    {
        public Task<int> InvokeAsync(InvocationContext context)
        {
            context.Console.Out.WriteLine("Hello World");
            return Task.FromResult(0);
        }
    }

    [Fact]
    public void Should_Print_HelloWorld()
    {
        var console = new TestConsole();
        new CommandLineBuilder(new HelloRootCommand() {Handler = new HelloHandler()})
            .UseDefaults()
            .UseHost(host =>
            {
                // .. configure DI
            })
            .Build()
            .Invoke("", console);
        
        Assert.Contains("Hello World", console.Out.ToString());
    }
    
   // this test fails! It prints the help
    [Fact]
    public void With_DI_Handler_Should_Print_HelloWorld()
    {
        var console = new TestConsole();
        new CommandLineBuilder(new HelloRootCommand())
            .UseHost(host =>
            {
                // .. configure DI
                host.UseCommandHandler<HelloRootCommand, HelloHandler>();
            })
            .UseDefaults()
            .Build()
            .Invoke("", console);
        
        Assert.Contains("Hello World", console.Out.ToString());
    }
}

EDIT: Changing the order of UseDefaults does not seem to make any difference.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions