Back to index

glibc  2.9
longopt.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <getopt.h>
00004 
00005 /* Flag set by @samp{--verbose}.  */
00006 static int verbose_flag;
00007 
00008 int
00009 main (argc, argv)
00010      int argc;
00011      char **argv;
00012 {
00013   int c;
00014 
00015   while (1)
00016     {
00017       static struct option long_options[] =
00018        {
00019          /* These options set a flag.  */
00020          {"verbose", no_argument,       &verbose_flag, 1},
00021          {"brief",   no_argument,       &verbose_flag, 0},
00022          /* These options don't set a flag.
00023             We distinguish them by their indices.  */
00024          {"add",     no_argument,       0, 'a'},
00025          {"append",  no_argument,       0, 'b'},
00026          {"delete",  required_argument, 0, 'd'},
00027          {"create",  required_argument, 0, 'c'},
00028          {"file",    required_argument, 0, 'f'},
00029          {0, 0, 0, 0}
00030        };
00031       /* @code{getopt_long} stores the option index here.  */
00032       int option_index = 0;
00033 
00034       c = getopt_long (argc, argv, "abc:d:f:",
00035                      long_options, &option_index);
00036 
00037       /* Detect the end of the options.  */
00038       if (c == -1)
00039        break;
00040 
00041       switch (c)
00042        {
00043        case 0:
00044          /* If this option set a flag, do nothing else now.  */
00045          if (long_options[option_index].flag != 0)
00046            break;
00047          printf ("option %s", long_options[option_index].name);
00048          if (optarg)
00049            printf (" with arg %s", optarg);
00050          printf ("\n");
00051          break;
00052 
00053        case 'a':
00054          puts ("option -a\n");
00055          break;
00056 
00057        case 'b':
00058          puts ("option -b\n");
00059          break;
00060 
00061        case 'c':
00062          printf ("option -c with value `%s'\n", optarg);
00063          break;
00064 
00065        case 'd':
00066          printf ("option -d with value `%s'\n", optarg);
00067          break;
00068 
00069        case 'f':
00070          printf ("option -f with value `%s'\n", optarg);
00071          break;
00072 
00073        case '?':
00074          /* @code{getopt_long} already printed an error message.  */
00075          break;
00076 
00077        default:
00078          abort ();
00079        }
00080     }
00081 
00082   /* Instead of reporting @samp{--verbose}
00083      and @samp{--brief} as they are encountered,
00084      we report the final status resulting from them.  */
00085   if (verbose_flag)
00086     puts ("verbose flag is set");
00087 
00088   /* Print any remaining command line arguments (not options).  */
00089   if (optind < argc)
00090     {
00091       printf ("non-option ARGV-elements: ");
00092       while (optind < argc)
00093        printf ("%s ", argv[optind++]);
00094       putchar ('\n');
00095     }
00096 
00097   exit (0);
00098 }