Skip to content

Is it possible for the FromAmong option extension to be case-insensitive with strings? #1726

Closed

Description

Hey everyone! Just wanted to start by saying I love this API, it has made building a command line for my app so much faster and easier!

I'm using the OptionExtensions.FromAmong() method to allow only certain strings for a "--name" option in my app. The app doesn't care if the strings are uppercase or lowercase, just that the word matches. However, the array of names that are allowed is potentially long (10-20 names). Here is what I"m talking about when I run the command:

PS C:\Users\masteroftofu\Documents\cliproject\bin\Debug\net6.0> .\cli run --name name1
Argument 'name1' not recognized. Must be one of:
        'NAME1'

Is there any way to accomplish this in System.Commandline without repeating the names in lowercase? It seems that parseArgument isn't invoked if the validation fails, and it also looks like tokens can't be modified in the validators (which makes sense). I've also tried a few other approaches with little success.

I apologize if this is an obvious question, but I am new to C# development and I've dug around a lot for a solution and haven't been able to find one yet.

Thanks, and cheers!

Activity

  1. jonsequitur commented on May 2, 2022

    @jonsequitur
    Contributor

    Thanks for the kinds words!

    FromAmong is doing two things with the values you give it. It's adding a validator as well as calling AddCompletions. In your case I'd say it's probably easiest to do these two things separately since you probably don't need both name1 and NAME1 in your completion list. You can do this using the AddValidator and AddCompletions methods. Here's an example:

    image

  2. added
    question
    on May 2, 2022
  3. qui8t commented on Oct 13, 2022

    @qui8t

    I guess this would common use case, so I am wondering if adding a flag to FromAmong to treat the values case-[in]sensitive be a better solution.

  4. cbersch commented on Mar 6, 2026

    @cbersch

    @jonsequitur It's basically the same reasoning as for enum parsing, which is already case-insensitive.

  5. added 3 commits that reference this issue on Mar 10, 2026
    c5c834b
    2956e1f
    a3576fe
  6. added a commit that references this issue on Apr 2, 2026
    e5a19ac
  7. added a commit that references this issue on Apr 2, 2026
    d5a39b1
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

    question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions