[C] parsing command line switches

From: Marlene Stebbins (stebbins_at_email.com)
Date: 01/26/04


Date: Mon, 26 Jan 2004 20:08:24 GMT

I haven't found any examples of code that parses command line
switches. I tried this:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
   if(argc != 3) exit(EXIT_FAILURE);

   if(strcmp(argv[2], "-s") == 0)
     printf("do s\n");
   else if(strcmp(argv[2], "-t") == 0)
     printf("do t\n");
   else
   {
     fprintf(stderr, "invalid switch\n");
     exit(EXIT_FAILURE);
   }

   return 0;
}

and it works, but I was wondering if this is anything like the
conventional method.

Marlene



Relevant Pages