dotnet / command-line-api

Command line parsing, invocation, and rendering of terminal output.
https://github.com/dotnet/command-line-api/wiki
MIT License
3.38k stars 381 forks source link

Required command was not provided on `RootCommand` #2359

Closed cschuchardt88 closed 3 months ago

cschuchardt88 commented 6 months ago

I am get error Required command was not provided. when running MyApp.exe --as-service with the following code below.

internal sealed class DefaultRootCommand : RootCommand
{
    public DefaultRootCommand() : base("Description")
    {
        var exportCommand = new ExportCommand();
        AddCommand(exportCommand);

        AddOption(new Option<bool>("--as-service", "Option1"));
    }
    // ........
}

However if I remove the code below it works fine.

var exportCommand = new ExportCommand();
AddCommand(exportCommand);

Program.cs

static async Task<int> Main(string[] args)
{
    var rootCommand = new DefaultRootCommand();
    var parser = new CommandLineBuilder(rootCommand)
        .UseHost(_ => new HostBuilder(), builder =>
        {
            builder.ConfigureDefaults(args);
            builder.UseSystemd();
            builder.UseWindowsService();
            builder.UseCommandHandler<DefaultRootCommand, DefaultRootCommand.Handler>();
            builder.UseCommandHandler<ExportCommand, ExportCommand.Handler>();
        })
        .UseDefaults()
        .UseExceptionHandler(NullExceptionFilter.Handler)
        .Build();

    return await parser.InvokeAsync(args);
}
KalleOlaviNiemitalo commented 6 months ago

In 2.0.0-beta4.22272.1, the "Required command was not provided" error is set by ParseResultVisitor.ValidateCommandHandler if the user-specified command does not have a handler but has subcommands. This is called during Parser.Parse. Your code above calls builder.UseCommandHandler<DefaultRootCommand, DefaultRootCommand.Handler>() but I think this is executed too late and the parser has already made its decision. Perhaps you could work around this by assigning a dummy handler to the command initially, just to satisfy the parser; the hosting middleware would then substitute the correct handler after parsing finishes, so the dummy handler would never be called.

KalleOlaviNiemitalo commented 6 months ago

@elgonzo, the constructor is public RootCommand(string description = "").

cschuchardt88 commented 6 months ago

In 2.0.0-beta4.22272.1, the "Required command was not provided" error is set by ParseResultVisitor.ValidateCommandHandler if the user-specified command does not have a handler but has subcommands. This is called during Parser.Parse. Your code above calls builder.UseCommandHandler<DefaultRootCommand, DefaultRootCommand.Handler>() but I think this is executed too late and the parser has already made its decision. Perhaps you could work around this by assigning a dummy handler to the command initially, just to satisfy the parser; the hosting middleware would then substitute the correct handler after parsing finishes, so the dummy handler would never be called.

The workaround I found is create sub command and keep RootCommand handler empty like you said.

builder.UseCommandHandler<DefaultRootCommand, ICommandHandler>();
KalleOlaviNiemitalo commented 6 months ago

No, I meant you'd assign a dummy handler to the root command without UseCommandHandler, then let UseCommandHandler replace it with the real handler after parsing has finished.

cschuchardt88 commented 6 months ago

No, I meant you'd assign a dummy handler to the root command without UseCommandHandler, then let UseCommandHandler replace it with the real handler after parsing has finished.

But how would i do that if I was doing this for example:

internal sealed partial class DefaultRootCommand : RootCommand
{
    public DefaultRootCommand() : base("Description")
    {
        var openWalletCommand = new OpenWalletCommand();
        var startOption = new Option<bool>("--as-service", "Option1");
        AddCommand(openWalletCommand);
        AddOption(startOption);
    }

    public new sealed class Handler : ICommandHandler
    {
        public int Invoke(InvocationContext context)
        {
            // TODO: Add Code
        }

        public Task<int> InvokeAsync(InvocationContext context)
        {
            // TODO: Add Code
        }
    }
}
KalleOlaviNiemitalo commented 6 months ago

By assigning Command.Handler or calling some overload of Handler.SetHandler. For example, this.SetHandler((InvocationContext _) => {});.