Skip to content

All commands are always optional when using hosting #2525

Closed as not planned
Closed as not planned
@altafard

Description

@altafard

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.

/// <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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions