Case-insensitive getopt()

This is a discussion on Case-insensitive getopt() within the C Programming forums, part of the General Programming Boards category; Hi all. I'm using getopt_long_only() in a project and I'd like to able to use it in a case insensitive ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    12

    Case-insensitive getopt()

    Hi all. I'm using getopt_long_only() in a project and I'd like to able to use it in a case insensitive manner. I've done a fair bit of googling on the subject but I can't find a thing. As a hack I could just lower the case of the command line string prior to processing, but if I could manage it in a more graceful way that would be preferable. A portable stand-alone command line parser (preferably similar to getopt) that permits this would also be good. Any help is appreciated. Thanks, Ehtyar.
    Last edited by Ehtyar; 11-18-2008 at 08:47 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Well if searching the manual revealed nothing about case insenstive behaviour, I'd go with your backup plan.

    At least anyone reading the code would instantly see what was going on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    12
    A problem I didn't foresee is file names on the command line in Linux. I may just be better off finding a 3rd party getopt library and modifying it. Ehtyar.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    How about posting what you got.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    First of all, I'm pretty sure there is a GNU getopt implementation somewhere.

    Second, do you really need a fully case-insensitive option getter, or just support the common cases:
    Option
    option
    OPTION
    rather than supporting for example
    OpTiOn
    opTIon
    and other very unlikely combinations.

    If the former, perhaps writing a piece of code for the setup of getopt to produce the three or so variants that are likely from a base-configuration (what I'm trying to say is to just produce the variants that make sense with code that does toupper in suitable places).

    That is not as "nice" as something that allows just about any combination, but in honesty, how many people would grumble if -OptIoN doesn't work?

    --
    Mats

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    You will have to simply code for the case. Using a 'switch case' for your options for both the lower case and upper case and the logic will just fall threw just do not call a break in between the options.

    Research the switch statement if you are not familiar with this.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by slingerland3g View Post
    You will have to simply code for the case. Using a 'switch case' for your options for both the lower case and upper case and the logic will just fall threw just do not call a break in between the options.

    Research the switch statement if you are not familiar with this.
    But that is not how getopt works.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    12
    I do agree that it is unlikely that switches will be specified in mixed case, though the duplication as was mentioned would lead to easily 150 different switches to process. I'd really just prefer case insensitivity.

    I'm going to pull the getopt implementation from MinGW and see how portable it is. If it's all good then I'll modify it and use it in my project.

    Otherwise, I think I may just end up including a warning in the documentation and leave it at that.

    Thanks for the replies thus far, Ehtyar.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Ehtyar View Post
    I do agree that it is unlikely that switches will be specified in mixed case, though the duplication as was mentioned would lead to easily 150 different switches to process. I'd really just prefer case insensitivity.
    But as long as the usage isn't listing 3 of each option, what is the problem with having 150 switches generated from the original 50 switches - all it takes is a little bit of coding (probably about 5 lines or so) to generate the longer version of the input table to getopt.

    Generating variants of the same thing is something computers are pretty darn good at. 150 options does not take up much space in a modern system, and if you have 50 or so options, I expect you aren't dealing with some program that runs on a microcontroller where a few kilobytes of memory makes a huge difference.

    Of course, the choice is yours, I'm just suggesting "an easy solution" that doesn't involve changing some rather complicated, existing and working code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    12
    Well, after examining the getopt.c from MinGW (was too windows-specific) and trying various others found on the net (more bugs than I'd care to have in a bug zapper) I've decided to go with the warning in the readme and leave it at that. I expect there will be the odd issue, but as long as I print errors for unknown switches things should go OK.

    Thanks for all the help guys, much appreciated.

    Ehtyar.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Ehtyar View Post
    Well, after examining the getopt.c from MinGW (was too windows-specific) and trying various others found on the net (more bugs than I'd care to have in a bug zapper) I've decided to go with the warning in the readme and leave it at that. I expect there will be the odd issue, but as long as I print errors for unknown switches things should go OK.

    Thanks for all the help guys, much appreciated.

    Ehtyar.
    That is obviously another "easy" solution. And most people will be able to learn that options are given as lower-case or upper-case only, and live with it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help it won't compile!!!!!
    By esbo in forum C Programming
    Replies: 58
    Last Post: 01-04-2009, 02:22 PM
  2. Segmentation fault!?!?!?
    By Viper187 in forum Windows Programming
    Replies: 57
    Last Post: 10-02-2008, 09:40 PM
  3. Format Precision & Scale
    By mattnewtoc in forum C Programming
    Replies: 1
    Last Post: 09-16-2008, 10:34 AM
  4. Keypress reading
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 06-16-2004, 12:16 PM
  5. returning to a spot in the code
    By Shadow in forum C Programming
    Replies: 4
    Last Post: 09-22-2001, 05:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21