Thread: getopt_long unrecognized option

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    2

    Question getopt_long unrecognized option

    Hi, I'm trying to use the getopt_long function to handle parameters to my C program, in Linux. Here is the relevant code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <getopt.h>
    
    #define ECB_MODE 1000
    
    int main(int argc, char* argv[])
    {
            int mode;
            struct option long_opts[2] = {
            {"ecb", no_argument, &mode, ECB_MODE},
            {0, 0, 0, 0}
        };
    
        while (1)
        {
            ch = getopt_long(argc, argv, "dek:i:o:", long_opts, NULL);
    
            if (ch == -1)
            {
                break;
            }
    
            switch(ch)
            {
                case 'd':
                {
                    encrypt = 0;
                    break;
                }
                case 'e':
                {
                    encrypt = 1;
                    break;
                }
                case 'i':
                {
                    infilename = optarg;
                    break;
                }
                case 'o':
                {
                    outfilename = optarg;
                    break;
                }
                case 'k':
                {
                    keyfilename = optarg;
                    break;
                }
                case 0:
                {
                    printf("ECB option\n");
                    break;
                }
                default:
                {
                    printf("Invalid arguments\n");
                    return 0;
                }
            }
        }
        return 0;
    }
    All of the short options work, but if I call the program with "--ecb" getopt_long returns '?' citing "ambiguous option". In my understanding, it should return 0 and store the value of the macro in the int variable I thoughtfully provided for it in the long_opts struct. My question is, why/how is "--ecb" ambiguous, and how can I make it unambiguous?

  2. #2
    Registered User
    Join Date
    Aug 2012
    Posts
    2
    Never mind. I've been staring at this thing for about two hours, and of course five minutes after I post this for the entire world to see I realize that I had been writing --enc on the command line instead of --ecb. What a shipdit. It works now, though.

    I apologize!
    Last edited by ShereModulus; 08-19-2012 at 09:58 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getopt_long
    By SeriousTyro in forum C Programming
    Replies: 2
    Last Post: 10-31-2010, 03:46 PM
  2. unrecognized option '--hash-style=both'?
    By meili100 in forum C++ Programming
    Replies: 8
    Last Post: 01-22-2008, 06:34 PM
  3. getopt_long
    By ear in forum C Programming
    Replies: 6
    Last Post: 06-12-2007, 02:52 PM
  4. Warning: .drectve `%.*s' unrecognized
    By Gretter in forum C++ Programming
    Replies: 9
    Last Post: 01-28-2005, 04:54 PM
  5. GCC errors: unrecognized option '--as-needed'
    By axr0284 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 12-09-2004, 01:16 PM