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

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

Open Simonl9l opened this issue 3 years ago • 3 comments

Information

  • OS: MacOS
  • Version: 0.43.0"
  • Terminal: MacOS/zsh

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.

Simonl9l avatar Mar 07 '22 03:03 Simonl9l

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; }

PawelSpoon avatar Sep 27 '22 08:09 PawelSpoon

Facing the same issue with this command option:

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

Please provide a workaround.

aa-dit-yuh avatar Aug 30 '23 07:08 aa-dit-yuh

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.

patriksvensson avatar Aug 30 '23 08:08 patriksvensson