spectreconsole / spectre.console

A .NET library that makes it easier to create beautiful console applications.
https://spectreconsole.net
MIT License
9.12k stars 468 forks source link

The option has an optional value but does not implement IFlagValue. #765

Open Simonl9l opened 2 years ago

Simonl9l commented 2 years ago

Information

Describe the bug

public class MySettings : CommandSettings
{

    [CommandOption("-o|--my-option-list [item]")]
    [Description("Description")]
    public IReadOnlyList<string>? OptionList { get; init; } = null;
}

Spectre.Console.Cli.CommandConfigurationException: The option 'my-option-list' has an optional value but does not implement IFlagValue.
   at Spectre.Console.Cli.CommandModelValidator.Validate(CommandInfo command) in /_/src/Spectre.Console/Cli/Internal/Modelling/CommandModelValidator.cs:line 117
   at Spectre.Console.Cli.CommandModelValidator.Validate(CommandInfo command) in /_/src/Spectre.Console/Cli/Internal/Modelling/CommandModelValidator.cs:line 124
   at Spectre.Console.Cli.CommandModelValidator.Validate(CommandModel model, CommandAppSettings settings) in /_/src/Spectre.Console/Cli/Internal/Modelling/CommandModelValidator.cs:line 41
   at Spectre.Console.Cli.CommandModelBuilder.Build(IConfiguration configuration) in /_/src/Spectre.Console/Cli/Internal/Modelling/CommandModelBuilder.cs:line 46
   at Spectre.Console.Cli.CommandExecutor.Execute(IConfiguration configuration, IEnumerable`1 args) in /_/src/Spectre.Console/Cli/Internal/CommandExecutor.cs:line 30
   at Spectre.Console.Cli.CommandApp.RunAsync(IEnumerable`1 args) in /_/src/Spectre.Console/Cli/CommandApp.cs:line 87

Im looking to have an Option that will accept a list of strings, but also in the case that no string are provided it acts a flag similar to a CommandOption with bool?

The Error is obscure at best. I've also tried with string[] OptionList with same results.

The error message here is completely un-helpful!


Please upvote :+1: this issue if you are interested in it.

PawelSpoon commented 1 year ago

stumbled today over the same issue. wanted to set my options properly and change <> to [] funny as options are not required anyway and i had to implement support for required options myself using std. Required annotation

        [CommandOption("-n|--name <NAME>")]
        [Description("name of configuration object")]
        [Required]
        public string Name { get; set; }
aa-dit-yuh commented 1 year ago

Facing the same issue with this command option:

    [CommandOption("--app-id [APPID]")]
    public Guid AppId { get; init; } = Guid.Empty;

Please provide a workaround.

patriksvensson commented 1 year ago

If an option has an optional value, the data type must implement IFlagValue.

    [CommandOption("--app-id [APPID]")]
    public FlagValue<Guid> AppId { get; init; } = Guid.Empty;

Are you sure that --app-id should be settable without a value? Otherwise I would suggest --app-id <APPID> which tells Spectre.Console that the option requires the value APPID.