command-line-api icon indicating copy to clipboard operation
command-line-api copied to clipboard

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

Open MasterofTofu opened this issue 2 years ago • 1 comments

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!

MasterofTofu avatar May 02 '22 17:05 MasterofTofu

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

jonsequitur avatar May 02 '22 21:05 jonsequitur

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.

qui8t avatar Oct 13 '22 12:10 qui8t