spectre.console icon indicating copy to clipboard operation
spectre.console copied to clipboard

Missing error message for required option in base command

Open lucasshiva opened this issue 8 months ago • 0 comments

Running the base command with a required option is not showing an error message.

var app = new CommandApp<AppCommand>();
app.Configure(config =>
{
    config.AddCommand<CountCommand>("count");
});
return app.Run(args);

internal sealed class AppCommand : Command<AppCommand.Settings>
{
    public class Settings : CommandSettings
    {
        [Description("Whether to show verbose output")]
        [CommandOption("-v|--verbose", isRequired: true)]
        public bool IsVerbose { get; init; }
    }

    public override int Execute(CommandContext context, Settings settings)
    {
        Console.WriteLine($"App: {settings.IsVerbose}");
        return 0;
    }
}

internal sealed class CountCommand : Command<CountCommand.Settings>
{
    public sealed class Settings : AppCommand.Settings
    {
    }

    public override int Execute(CommandContext context, Settings settings)
    {
        Console.WriteLine($"Count: {settings.IsVerbose}");
        return 0;
    }
}

Output for base command:

❯ dotnet run --            
USAGE:
    myapp.dll [OPTIONS] [COMMAND]

OPTIONS:
    -h, --help       Prints help information                 
    -v, --verbose    Whether to show verbose output. Required

COMMANDS:
    count

There's no error message. It works correctly when passing a value:

❯ dotnet run -- -v      
App: True

And the output for the subcommand count does show an error message:

❯ dotnet run -- count   
Error: Command 'count' is missing required argument 'verbose'.

As a side note, perhaps it would be a good idea to change the message to say "option" rather than "argument"?

lucasshiva avatar Jun 06 '25 23:06 lucasshiva