#include /* getopt is defined in "unistd.h". */ #include int main (int argc, char ** argv) { int i; while (1) { char c; c = getopt (argc, argv, "ab:"); if (c == -1) { /* We have finished processing all the arguments. */ break; } switch (c) { case 'a': printf ("User has invoked with -a.\n"); break; case 'b': printf ("User has invoked with -b %s.\n", optarg); break; case '?': default: printf ("Usage: %s [-a] [-b ].\n", argv[0]); } } /* Now set the values of "argc" and "argv" to the values after the options have been processed, above. */ argc -= optind; argv += optind; /* Now do something with the remaining command-line arguments, if necessary. */ if (argc > 0) { printf ("There are %d command-line arguments left to process:\n", argc); for (i = 0; i < argc; i++) { printf (" Argument %d: '%s'\n", i + 1, argv[i]); } } return 0; }