[LC++]getopt-question

robert at ncdesigns.net robert at ncdesigns.net
Fri Dec 13 01:22:01 UTC 2002


On Thu, 12 Dec 2002, Julien Patrick Claassen wrote:

> Hello all!
>   I want to call a program with some options. But there are some which exclude
> one another. I.e.: You use either -r or -s, but not both. How can I do it
> nicely with using getopt_long?
>   Thanks for any help!
>   Kindest regards
>          Julien
> 

It's rather quite simple, here's pseudocode

char *val_r;
char *val_s;

val_r = val_s = NULL;

while (1)
{
   char c = getopt();
   if (c == -1)
       break;

   switch (c)
   {
       case 'r':
           val_r = optarg;
           break;
       case 's':
           val_s = optarg;
           break;
   }
}

if ((val_r != NULL) && (val_s != NULL))
    exit_func();


Of course you could write this a little simpler, but it's more readable.

Robert





More information about the tuxCPProgramming mailing list