-
Notifications
You must be signed in to change notification settings - Fork 406
Description
In the example bellow I was expecting that running cli
without specifying a subcommand would fail with the error "Required command was not provided." as it happens without using the host
var root = new Command("cli")
{
Subcommands = { new Command("cmd") }
};
var configuration = new CommandLineConfiguration(root).UseHost(
_ => Host.CreateDefaultBuilder(),
cfg => cfg.UseInvocationLifetime());
await configuration.InvokeAsync(args);
However, it works fine and no errors pop up. It seems that UseHost
calls HostingAction.SetHandlers
extensions on the root command and it sets actions to all commands in the chain. As a result, this validation never sets the error.
command-line-api/src/System.CommandLine/Parsing/CommandResult.cs
Lines 46 to 55 in b7f0d1c
/// <param name="completeValidation">Only the inner most command goes through complete validation.</param> | |
internal void Validate(bool completeValidation) | |
{ | |
if (completeValidation) | |
{ | |
if (Command.Action is null && Command.HasSubcommands) | |
{ | |
SymbolResultTree.InsertFirstError( | |
new ParseError(LocalizationResources.RequiredCommandWasNotProvided(), this)); | |
} |
Was it made intentionally? As a workaround I can use Validators and check that there are tokens provided in the arguments list, but it would be nice to keep the same behaviour as it works without using a host.