Skip to content

Consider optional case insensitive parsing #133

Open

Description

@jonsequitur

Developers should have the option to create command lines where commands and options are matched in a case insensitive manner. This matches existing expectations for PowerShell users, and even for POSIX users, this may make it easier for them to do the right thing.

Activity

  1. added
    needs discussion
    on Jun 6, 2018
  2. KathleenDollard commented on Jun 20, 2020

    @KathleenDollard
    Contributor

    I think this is a good idea and we should continue to get feedback on it.

    My windows Command Prompt initial mindset (where I internalized CLI years ago) is definitely case insensitive.

  3. jonsequitur commented on Jul 9, 2020

    @jonsequitur
    ContributorAuthor

    As I posted on #969, it's not clear to me that we should do this. Command lines are typically case sensitive on Linux/macOS, and often on Windows as well. Could case insensitivity lead to an inconsistent ecosystem that will confuse people? When I'm using a new command line tool, how will I know whether it's case sensitive or not?

  4. baochenw commented on Jul 10, 2020

    @baochenw

    How about give more flexibility to let the consumer decide it? Maybe, theirs command lines are cross platform. And also maybe theirs are windows only. Even theirs existing apps are case insensitive and would like to move to this great package.
    We are encouraging making the command lines case sensitive. I'm not sure if MSFT will change cmd and powershell to case sensitive.
    For some reason, if they want to make theirs command lines case insensitive but we don't support it. Ultimately, they will look for other solutions which support that but don't change theirs idea.

  5. Keboo commented on Aug 27, 2020

    @Keboo
    Member

    If we allow case insensitive matching, that that opens up the possibility of an invocation failing do to an ambiguous match. That is, when there are more than one possible symbols could potentially match a given token.
    A few things worth discussing further:

    • How would case insensitivity be toggled? If it is controlled by the developer this could simply be an API addition. If this were control by the CLI consumer, it would likely need to be controlled by something like a directive.
    • How to handle error caused by case insensitive matching? I suspect this would need to be handled in a similar manner to an existing parse error, with some message indicating that it failed due to matching multiple symbols.
    • How to handle posix bundling? Right now we support bundling of arguments, allowing case insensitive matching adds additional complexity here.
    • How to handle symbol matching? Unforuently this is not always simple with nested commands since items may be specified in any order. Should it be consider ambiguous if there is a case-insensitive match on the current command, but an exact match on a parent command?
  6. elgonzo commented on Feb 7, 2021

    @elgonzo
    Contributor

    I would very much like to see providing the developer with a choice of whether they want their CLI to behave in a case-sensitive or in a case-insensitive manner. I prefer using camelCase or PascalCase for (longer) option names, even when also employing kebab-case, as i find this more readable in the help dump than using snake-case or kebab-case alone. But i do not want to burden the user with painstakingly observing and obeying the letter case.

    While i have no deeper insight into where and what a good API for such a choice should look like, i feel like CommandLineConfiguration and CommandLineBuilder (through a Use* extension method) could be good candidates for offering such a choice to a developer employing System.CommandLine.


    If we allow case insensitive matching, that that opens up the possibility of an invocation failing do to an ambiguous match. That is, when there are more than one possible symbols could potentially match a given token.

    How and where do you see case-insensitivity introducing additional ambiguity? In a case-insensitive mode, the options -c and -C would be equivalent. Having a case-insensitive CLI that has to parse -c -C or posix-bundled -cC would be no different than having a case-sensitive CLI that has to parse -c -c or -cc. System.Commandline already has to deal with the latter, so dealing with the former should be exactly the same thing but based on StringComparer.OrdinalIgnoreCase-based comparisons and lookups. (Or perhaps StringComparer.InvariantCultureIgnoreCase; i don't know what would be the wiser choice if some developer chooses to define option names with non-latin letters.)

    • How would case insensitivity be toggled? If it is controlled by the developer this could simply be an API addition. If this were control by the CLI consumer, it would likely need to be controlled by something like a directive.

    In my opinion, the choice and ability to enable case-sensitivity should rest in the hands of the developer only. The developer either designs their CLI to function in a case-insensitive manner, or they don't.

    If the CLI is not designed to be case-insensitive, what would be the consequences of the CLI consumer being able to enable case-insensitive parsing through a directive or other mechanism? Doing so would put a burden on the consumer of the CLI to verify that the CLI would still operate in the same way and does not exhibit unforeseen and unexpected changes in the behavior. That would be a pretty though ask for the conumer of the CLI.

    On the other hand, if the CLI is expressly designed and behaves in case-insensitive manner to begin with, what would be the actual point of a consumer of the CLI being able to enable case-sensitive mode?

    • How to handle error caused by case insensitive matching? I suspect this would need to be handled in a similar manner to an existing parse error, with some message indicating that it failed due to matching multiple symbols.

    Since in case-insensitive mode, -c and -C would be equivalent, defining an option for -c and an option for -C for a case-insensitive CLI should be treated exactly like a developer defining the option -c twice (a situation which System.Commandline already handles).

    • How to handle posix bundling? Right now we support bundling of arguments, allowing case insensitive matching adds additional complexity here.

    Where and what is the nature of the additional complexity you see? In case-insensitive mode, a bundle like "-abcCD" would be equivalent to "-abccd" (and which System.Commandline is already able to handle).

    • How to handle symbol matching? Unforuently this is not always simple with nested commands since items may be specified in any order. Should it be consider ambiguous if there is a case-insensitive match on the current command, but an exact match on a parent command?

    Don't (re)introduce case-sensitive behavior if the developer has expressly chosen case-insensitive behavior. If a parent command and a sub command feature options with names that only differ in their letter case, then treat those options as having the same name when in case-insensitive mode.

  7. jonsequitur commented on Feb 9, 2021

    @jonsequitur
    ContributorAuthor

    I think the previous comments assumed this would be an end user decision and not a developer decision. I think each approach has its own problems.

    For example, possible ambiguities arise if the end user can toggle this setting. We would need a mechanism (preferably at design time, to avoid a perf hit on every run) to ensure that a given configuration would be valid in case-insensitive mode.

    If this is a developer decision, on the other hand, how will users know whether a given command line is case sensitive? Will it be confusing for macOS / *nix users to encounter case insensitive command lines?

  8. Pxtl commented on Feb 22, 2021

    @Pxtl

    I just hit this. I'm trying to modernize an internal application where its previous command-line parser was a NIH homebrew, and my users expect case-insensitivity. This is a show-stopper.

    To my mind, each alias should be defineable as case-sensitive or not, so that single-character aliases can be case-sensitive in their crowded space, but long verbose commands don't need to be.

    Honeslty, I'm surprised even this lib is so standardized on the double-hyphen prefix. I'd assume it would have me pick the identifiers and then use whatever prefixing characters are native to the platform.

  9. jonsequitur commented on Feb 22, 2021

    @jonsequitur
    ContributorAuthor

    each alias should be defineable as case-sensitive or not, so that single-character aliases can be case-sensitive in their crowded space, but long verbose commands don't need to be.

    You can do this currently by specifying the additional aliases:

    var option = new Option<string>(new [] { "-a", "-A" });

    I'd assume it would have me pick the identifiers and then use whatever prefixing characters are native to the platform.

    Most of our examples use POSIX-style prefixes but you can absolutely choose the prefixes you like. The System.CommandLine HelpOption includes both styles:

    public HelpOption() : base(new[]
    {
    "-h",
    "/h",
    "--help",
    "-?",
    "/?"

    The .NET command line ecosystem has been trending toward broader use of POSIX for several years, not least because .NET Core tools are cross-platform, and on non-Windows systems, the / character conflicts with file path separators. Consistency across operating systems helps reuse of documentation, script code, and so on.

  10. Pxtl commented on Feb 22, 2021

    @Pxtl

    Ah, thanks for the clarification. Although by "conventions of the platform", I was thinking of how Powershell is pretty strongly on single-hyphen, not double-hyphen, for options. That's mostly what I had in mind, not the old slashes.

    As for "using multiple cases as aliases" - I actually meant the reverse - that single-char aliases you'd want case-sensitivity there, but case-insensitivity would be more useful for long options where the user can't remember if it's StartDateTime or startDateTime or StartDatetime or startDatetime or startdatetime or what, and generating out every possible combination is non-trivial. And while I've been experimenting with autogenerating a suite of aliases for longer option-names, I found they made the "Help" screen unusable cluttered, unless there's a way to restrict "help" to only list certain aliases?

  11. masample-ms commented on Feb 23, 2021

    @masample-ms

    From an accessibility standpoint, case sensitivity can be a nightmare for dyslexic/dysgraphic users. Having the option when developing to disable case sensitivity (especially on subcommands) would definitely improve my experience with my own tools. Its less important on single letter aliases than it is on longer "--this-is-a-thing" params or subcommands. I don't think this would harm too many *nix users either, given that the case sensitive environment there usually conditions one to not change the casing of flags from the way they learn them/discover them in help.

  12. ScottArbeit commented on Aug 21, 2021

    @ScottArbeit

    I love this library, been trying it from F#, it's really nice. 🙂

    And, obviously, I'm here because I'd also like case-insensitivity.

    If this is a developer decision, on the other hand, how will users know whether a given command line is case sensitive? Will it be confusing for macOS / *nix users to encounter case insensitive command lines?

    It should definitely be a developer decision. And what I really, really want is some sort of .ApplyPlatformCaseSensitivity = true. On Windows, where I live, I'll get case-insensitivity. On Linux etc. they'll get case-sensitive.

    If not, I'm happy to write the if Windows {...case-insensitive...} else {...case-sensitive...} part myself. Just please give me the chance to.


    Beyond that, I want to challenge an assumption underneath all of this: that case-sensitive is somehow a better choice because operating systems that go back to the 1970's did it that way. I just don't agree. And I'm not alone.

    It does a disservice to Windows users, who deserve to have their CLI's work the way Windows does. If CLI's are case-sensitive on Windows, that's a UX fail. It feels lazy. And I'd rather ship something case-insensitive and have most Linux etc. users not notice it, than have every Windows user run into errors within the first minute of using my CLI because of case-sensitivity. It's a rude way to meet a new thing you're trying.

    You may disagree, and that's great. But, please, as a craftsman, give me the choice of doing UX the way I think it should be done.

    It's not enough to make me stop using System.CommandLine, I really do l like it, but it's a gap that I think is important to address.

  13. Pxtl commented on Sep 3, 2021

    @Pxtl

    Here's my crude way of implementing case-insensitivity as a workaround:

    1. I've made all my option-names all lower-case.
    2. My program opens with
            private static int Main(string[] args)
            {
                ChangeAllOptionNamesToLowerCase(args);
    
    
    ...
    
    
            /// <summary>
            /// System.CommandLine is case-sensitive and does not support
            /// case-insensitivity, so //HACK in case-insensitivity by lowercasing
            /// all option-names.
            /// </summary>
            private static void ChangeAllOptionNamesToLowerCase(string[] args)
            {
                for (var i=0; i<args.Length; i++)
                {
                    if(args[i].StartsWith("-") || i==0) //i==0 is the command-name.
                    {
                        args[i] = args[i].ToLower();
                    }
                }
            }

    However, this hack only works because my program has a very simple command-name structure - first arg is command-name and all following args are option-names or option-values.

    Obviously this is a very crude hack to just mutate all the args, but it works for me.

  14. John0King commented on Oct 28, 2021

    @John0King

    should it be an option on the parser/command ?

  15. 15 remaining items

  16. jonsequitur commented on May 20, 2022

    @jonsequitur
    ContributorAuthor

    I guess the question I really have is: maybe I'm missing something, but what is your resistance to adding a way to do case-insensitive commands and options here? By all means, keep System.CommandLine case-sensitive by default, I'm not arguing with that. Roughly 50% of my expected users will be on Windows and I just want to deliver a platform-consistent experience to them.

    I understand that you're not pushing for a change to the default behavior (though I think it might be unobtrusive to do so for commands.)

    Options are more complicated because case insensitivity might make certain things harder for end users to understand. In short, I think it comes down to a choice that the developer makes, and the end user then needs to be able to understand from one tool to another, between either

    • case-insensitive options
      or
    • support for POSIX bundling.

    When we give developers this choice (which is objectively a good thing), it's also our responsibility to help end users understand it.

    For context, a design principle we've tried to follow is to avoid fragmenting the end user experience. This presents a couple of tensions:

    • Consistency of your app across operating systems vs. consistency with the conventions of the operating system. (We've favored the former, POSIX conventions, and case sensitivity.)

    • Consistency across apps for things like parsing behaviors, help conventions, and CLI grammar vs. customization of all of those things. (We've chosen what we think are sensible defaults and provided many places to customize, often driven by the goal of enabling people to move older apps onto System.CommandLine without forcing breaking changes.)

  17. ScottArbeit commented on May 20, 2022

    @ScottArbeit

    @jonsequitur Thank you for the context. I really do appreciate how seriously the team has thought about it, and the desire to maintain consistency.

    I can imagine a nuisance if, say, one team member / open-source contributor on Windows writes a script using my tool, expecting it to be case-insensitive, and then another team member / contributor runs that script on Linux and it fails because an option has the wrong casing. Not ideal, but also not a major blocker.

    The one assumption you listed that I'll challenge is

    it's also our responsibility to help end users understand it.

    I hadn't considered that, and I love that you've all thought about it, and that you want to help with it. Just so you and the rest of the team knows: I feel like it's my responsibility for my end users, not yours. I'm willing to take the risk of having my users blame me if they don't like my UX. I don't expect they'll blame System.CommandLine, if they're even aware that it's part of the solution.

  18. jonsequitur commented on May 20, 2022

    @jonsequitur
    ContributorAuthor

    @ScottArbeit You're quite welcome.

    The way I look at it, if System.CommandLine doesn't help end users understand how to use apps built with it, then it falls to app developers, and not everyone has the time for that. Even when they do, everyone will convey it a little differently. This is why we've tried to create a good, consistent default help output, for example.

    And of course we're also building this library to support Microsoft's own tools, including the dotnet CLI, so discoverability of the rules and consistency across operating systems is a priority, and if we can help our customers and yours at the same time, it's ideal.

    But again we don't want to force these decisions on you. We just want to make sure the defaults favor the most consistent and usable experience for end users.

  19. cgravill commented on May 20, 2022

    @cgravill

    @jonsequitur

    Case-insensitive commands are enough for us for now. We currently add aliases for different casing (camelCase. PascalCase, UPPERCASE, and lowercase so far) which works well enough but does make the help noisy.

    Commands:
      {...more commands elided...}
      commandLikeThis, CommandLikeThis, COMMANDLIKETHIS, commandlikethis Do the command
    
  20. PolarbearDK commented on Apr 28, 2023

    @PolarbearDK

    I would suggest optional case insensitivity only for the long versions of options.

    As in --help --Help and --HELP resulting in same option.
    But -h and -H being different options.

  21. tig commented on Oct 30, 2023

    @tig

    I don't care if Option names are case sensitive or not.

    What I and my users want is Argument<string>s to be case-insensitive.

  22. elgonzo commented on Oct 30, 2023

    @elgonzo
    Contributor

    @tig

    What I and my users want is Argument<string>s to be case-insensitive.

    What exactly is that supposed to mean? Argument<string> (or CliArgument<string> with respect to the current code base) is basically just representing a (user-provided) string value. Values are neither case-sensitive nor case-insensitive. Certain string operations are, but not a string value itself. Where do you see case-(in)sensitivity come into play regarding Argument<string>/CliArgument<string> that would naturally fall within the scope and control of the System.CommandLine library?

  23. tig commented on Oct 30, 2023

    @tig

    @tig

    What I and my users want is Argument<string>s to be case-insensitive.

    What exactly is that supposed to mean? Argument<string> (or CliArgument<string> with respect to the current code base) is basically just representing a (user-provided) string value. Values are neither case-sensitive nor case-insensitive. Certain string operations are, but not a string value itself. Where do you see case-(in)sensitivity come into play regarding Argument<string>/CliArgument<string> that would naturally fall within the scope and control of the System.CommandLine library?

    var scenarioArgument = new Argument<string> (
    				name: "scenario",
    				description: "The name of the scenario to run.",
    				getDefaultValue: () => "none"
    				).FromAmong (_scenarios.Select (s => s.GetName ()).Append ("none").ToArray ());

    _scenarios contains about 30 items (e.g. "Character Map").

    UICatalog "Character Map" works.

    UICatalog "character map" results in an error ("scenario not found") and help being shown.

  24. elgonzo commented on Oct 30, 2023

    @elgonzo
    Contributor

    @tig thanks for the clarification. (Just as an FYI, there is also another open issue report asking about such a feature: #1726)

  25. schittli commented on Nov 29, 2024

    @schittli

    Good evening

    Thank you very much for sharing your great work!
    This library has great concepts and it's great to have them.

    But...:

    I am very sorry, but this discussion is extremely disappointing.
    For example, arguments like this are (sorry!) very embarrassing: “Mac OS and Linux have always been case-sensitive!”

    So let's do it the same way they did 2 decades ago, even if it brings ZERO benefit and makes no sense??

    What is the goal of a command line library?
    Is the most important thing to follow an ideology and mathematical laws?

    At the end, the goal must be:

    • The programmed app must serve the user
    • An app is great when the usability is just perfect
    • The app is ingenious and fun when it is smart

    There are also non-nerds who find command line apps brilliant.
    But if you force them to study the help every time just to get the capitalization right, it's just embarrassing for the developer.

    Modern libraries should have no problem mapping --add to --Add so that the user can work easily and the Computer does its job.

    I bet that three things are trivial to solve the problem:

    1. that you can add a case-insensitive-flag to the option-attribute
    2. that the lib first checks at app startup whether the parameters are no longer unique because case-sensitive is disabled and an ambiguous match will happen.
    3. Then, the app simply aborts and displays the parameters that have an ambiguous match.

    This allows the developer to easily start the app and check if the parameters are unique.
    And the developer can then decide whether he wants to program a smart or stubborn app.

    Thanks a lot, kind regards,
    Thomas

  26. Pxtl commented on Nov 29, 2024

    @Pxtl

    This just popped into my notifications again, and it actually makes me curious...

    My background is Windows-first, so I don't have deep knowledge of POSIX parameters. How does POSIX-bundling work with multi-character parameters? Like, if I have a parameter -f for force, -a for archive -x for extract, and -fax for send fax, and I call -fax <value>, how does that get resolved? I know typically POSIX applications resolve this by using the single-hyphen-prefix for single-char parameters and the double--hyphen--prefix for multi-char parameters. I assume double-hyphen-prefix means no POSIX bundling. Could something like that be done here? Allow us to choose to enable bundling, case-insensitivity, or neither by prefix scope? So if I wanted to follow a semi-case-insensitive variant of what I understand is the POSIX convention, I could say

    Bundleable case-sensitive single-hyphen prefix
    -f apply filter
    -F force
    -a archive
    -A all
    -x extract
    -X execute
    
    non-bundleable case-insensitive double-hyphen prefix
    --fax send-fax
    

    where --fax can also be --Fax or --FAX or whatnot to send a fax, but meanwhile -fax means "filter, archive, extract" and -FAX means "force, all, execute".

    And no, I have no idea what program would involve options for all those things.

  27. schittli commented on Nov 30, 2024

    @schittli

    It's great to have concepts like POSIX: they give good, useful hints.
    But it's like in real life: when they become a stubborn ideology, the solutions get sick and lose sight of the goal.

    And the goal here is to provide the best possible tools and command line interfaces for the respective user group that make THEM, the users, happy - and not the API or POSIX.

    If our customers can justify (and they have to!) why a parameter must have 3 aliases or why an abbreviation should have two letters, then we program it.

    This is probably one of the main reasons why our customers love our tools and keep working with us on new projects. For over a quarter of a century 🥰

    And that makes us happy when our customers enjoy working with our tools.

    When the developers of an API complain: “That's not how it was meant to be, you're breaking the rules!” then we ask: so what?
    (Your API was developed to solve read-world problems - and that is exactly what we have achieved. It couldn't be better.) 😃

    Thanks a lot!, kind regards,
    Thomas

  28. ScottArbeit commented on Nov 30, 2024

    @ScottArbeit

    In case anyone is interested, I ended up writing my own caseInsensitiveMiddleware for System.CommandLine.

    It's very much shaped to my particular use - I have an alias construct that has to be considered as part of looking at the tokens - but hopefully it'll get you started towards implementing it for your projects.

    Really, I just got so sick of my project being case-sensitive as I was testing it, I started anger-coding it one night, for my own sanity.

    Anyway, the original F# source is in ScottArbeit/Grace in Program.CLI.fs, where I define most of the global stuff I use with System.CommandLine.

    You can see it added as middleware to the builder here.

    I've created an F# -> C# conversion of it using ChatGPT. (It's prettier in F#. 🤷🏻‍♂️😉) Feel free to continue that conversation to get help from GPT-4o.

  29. schittli commented on Dec 3, 2024

    @schittli

    Thanks a lot, @ScottArbeit!, for sharing your great work!

    💡 For the dotnet developers:
    If you watch here:

    You can see it added as middleware to the builder here.
    .AddMiddleware(caseInsensitiveMiddleware, MiddlewareOrder.ExceptionHandler)

    You can see that it should be pretty easy to allow customers of this API to enable/disable case-insensitive handling.

    There is ZERO meaningful reason to patronize customers with ideologies and actively prevent them from writing software for users that has great usability.

    It's a shame when we have to explain to our users that they have to type --Help because Microsoft doesn't want --help to work. Simply embarrassing.

    Thanks a lot, kind regards,
    Thomas

  30. schittli commented on Dec 8, 2024

    @schittli

    Yes, what a shame that Microsoft would rather spend 5 years discussing a customer request instead of doing the job and programming the 60 or so lines of code and offering a middleware handler, for example:

    https://stackoverflow.com/questions/79263454/c-sharp-net-system-commandline-making-the-command-line-case-insensitive/79263455#79263455

    This is a small working example that used the great middleware of ScottArbeit.

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

    Area-Parser and Binderneeds discussion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions