Zypher
Zypher

Reputation: 1

using getopt to parse command line arguments

I'm trying to use getopt to parse my command line arguments but i'm having an issue where it is setting the wrong values its skipping case 1 and setting case 2 as case 1.Here is the while loop of just the cases. The flags work fine its just reading in by position that is the problem.

while ((c = getopt (argc, argv, "-l:u:eo")) != -1)
         switch (c)
           {
           case 1:
            printf("here\n");
             lower = atoi(optarg);
             break;
           case 2:
             upper = atoi(optarg);
             break;
    }

here is the output from the terminal after running the program.

(%) fibon 12 22
here
here
lower = 22, upper = 0, even = 0, odd = 0
Usage: fibon[[lower upper] | [-l lower -u upper]] [-e|-o]

Upvotes: 0

Views: 281

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409166

From the getopt(3) manual page:

If the first character of optstring is '-', then each nonoption argv-element is handled as if it were the argument of an option with character code 1.

That means that both numbers will be returned from getopt as 1. You need to keep track of which option you have yourself.

Upvotes: 1

Related Questions