Closed cschuchardt88 closed 3 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.
@elgonzo, the constructor is public RootCommand(string description = "")
.
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>();
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.
No, I meant you'd assign a dummy handler to the root command without
UseCommandHandler
, then letUseCommandHandler
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
}
}
}
By assigning Command.Handler or calling some overload of Handler.SetHandler. For example, this.SetHandler((InvocationContext _) => {});
.
I am get error
Required command was not provided.
when runningMyApp.exe --as-service
with the following code below.However if I remove the code below it works fine.
Program.cs