Skip to content

Add Choices Option #172

Description

@zackmark29

I can't seem find any in the documentation about choices option.
Like in python there is choices option in the argument. Is it possible to do with this great extension?

Activity

  1. Matthiee commented on Apr 27, 2022

    @Matthiee
    Member

    At the moment it is not possible.

    However I feel like you could get very close using enum's.

    Example:

    public enum TestEnum
    {
        Value1,
        Value2,
        Value3,
    }
    
    public class Options
    {
        public TestEnum MyEnum { get; set; }
    }
    
    parser.Configure(opt => opt.MyEnum)
                    .Name("e");
    > Example.exe -e "Value2"
    > MyEnum=Value2
    
    > Example.exe -e "Value10"
    > Unable to parse option '-e|--e' value 'value10' is invalid!
    

    Another way to achieve this without enums would be to use FluentValidations Extension.

  2. zackmark29 commented on Apr 27, 2022

    @zackmark29
    Author

    At the moment it is not possible.

    However I feel like you could get very close using enum's.

    Example:

    public enum TestEnum
    {
        Value1,
        Value2,
        Value3,
    }
    
    public class Options
    {
        public TestEnum MyEnum { get; set; }
    }
    
    parser.Configure(opt => opt.MyEnum)
                    .Name("e");
    > Example.exe -e "Value2"
    > MyEnum=Value2
    
    > Example.exe -e "Value10"
    > Unable to parse option '-e|--e' value 'value10' is invalid!
    

    Another way to achieve this without enums would be to use FluentValidations Extension.

    Thank you for the response. Yeah Enum is good idea I'll do it instead.
    I still preferred your extension it's easy to use :)

  3. zackmark29 commented on Apr 27, 2022

    @zackmark29
    Author

    Thank you that solved my problem. However there's just little problem when I set the default and input invalid value, It's not throwing the exception but still getting the default value only. But it's ok I just skip the default value

  4. Matthiee commented on Apr 27, 2022

    @Matthiee
    Member

    I do like the choices idea.

    API could look something like this:

    IOptionsBuilder<T> Choices(IEnumerable<T> choices);
    
    parser.Configure(opt => opt.SomeOption)
              .Choices(new []{  1, 2, 3 });

    Not sure how this would work when the option type is a Collection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions