[Python] How do I ignore case of double-dash options (long-options) in argparse?

Discussão em 'Python' iniciado por Stack, Janeiro 17, 2025.

  1. Stack

    Stack Membro Participativo

    I've written some command-line tools that have certain options having both single-dash-one-letter-abbreviations (short-options), as well as more verbosely named double-dash options (long-options) - as I expect many of us do. (Check jrowellfx's github repo if curious for tools I've written.)

    For readability I (mostly) like to use 'camel-case' for my long-options.

    While paying attention to the case of short-options makes total sense, for example -f and -F might be treated as distinct options, I'd LOVE to ignore the case of long-options. Meaning if someone typed one of my more verbosely named long-options but wrote it all lower-case (instead of camel-case), I don't want argparse to throw an error, but treat it as if they typed it with the correct camel-case.

    I don't know how to do this. My searches if anyone else has ever asked about this has turned up nothing. I could easily have missed the discussion elsewhere. But this does seems like a feature of argparse that would be quite nice.

    I only want to specify the camel-case options to argparse, and I want the help and error messages to respect the camel-case names. So I ONLY want to let the user be more sloppy when typing the more verbosely named options.

    Seems like a pretty nice feature to have in argparse no?

    Anyone have any idea to get down into the guts of "parse_args()" to enable this kind of behavior?

    Continue reading...

Compartilhe esta Página