home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / clib / progs / utilslib / Getopt / C / Getopt1 < prev   
Encoding:
Text File  |  1991-09-29  |  3.1 KB  |  139 lines

  1. /* Getopt for GNU.
  2.    Copyright (C) 1987, 1989 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <stddef.h>
  19. #include "getopt.h"
  20.  
  21. int getopt_long (int argc, char *argv[], const char *options,
  22.          const struct option *long_options, int *opt_index)
  23. {
  24.   int val;
  25.  
  26.   _getopt_long_options = long_options;
  27.   val = getopt (argc, argv, options);
  28.   if (val == OPT_LONG && opt_index != NULL)
  29.     *opt_index = option_index;
  30.   return val;
  31. }
  32.  
  33. /* Like getopt_long, but '-' as well as '+' can indicate a long option.
  34.    If an option that starts with '-' doesn't match a long option,
  35.    but does match a short option, it is parsed as a short option
  36.    instead. */
  37.  
  38. int  getopt_long_only (int argc, char *argv[], const char *options,
  39.                const struct option *long_options, int *opt_index)
  40. {
  41.   int val;
  42.  
  43.   _getopt_long_options = long_options;
  44.   _getopt_long_only = 1;
  45.   val = getopt (argc, argv, options);
  46.   if (val == OPT_LONG && opt_index != NULL)
  47.     *opt_index = option_index;
  48.   return val;
  49. }
  50.  
  51.  
  52. #ifdef TEST
  53.  
  54. #include <stdio.h>
  55.  
  56. int main (int argc, char *argv[])
  57. {
  58.   int c;
  59.   int digit_optind = 0;
  60.  
  61.   while (1)
  62.     {
  63.       int this_option_optind = optind ? optind : 1;
  64.       char *name = '\0';
  65.       int option_index = 0;
  66.       static struct option long_options[] =
  67.       {
  68.     {"add", 1, 0, 0},
  69.     {"append", 0, 0, 0},
  70.     {"delete", 1, 0, 0},
  71.     {"verbose", 0, 0, 0},
  72.     {"create", 0, 0, 0},
  73.     {"file", 1, 0, 0},
  74.     {0, 0, 0, 0}
  75.       };
  76.  
  77.       c = getopt_long (argc, argv, "abc:d:0123456789",
  78.                long_options, &option_index);
  79.       if (c == OPT_END)
  80.     break;
  81.  
  82.       switch (c)
  83.     {
  84.     case OPT_LONG:
  85.       printf ("option %s", (long_options[option_index]).name);
  86.       if (optarg)
  87.         printf (" with arg %s", optarg);
  88.       printf ("\n");
  89.       break;
  90.  
  91.     case '0':
  92.     case '1':
  93.     case '2':
  94.     case '3':
  95.     case '4':
  96.     case '5':
  97.     case '6':
  98.     case '7':
  99.     case '8':
  100.     case '9':
  101.       if (digit_optind != 0 && digit_optind != this_option_optind)
  102.         printf ("digits occur in two different argv-elements.\n");
  103.       digit_optind = this_option_optind;
  104.       printf ("option %c\n", c);
  105.       break;
  106.  
  107.     case 'a':
  108.       printf ("option a\n");
  109.       break;
  110.  
  111.     case 'b':
  112.       printf ("option b\n");
  113.       break;
  114.  
  115.     case 'c':
  116.       printf ("option c with value '%s'\n", optarg);
  117.       break;
  118.  
  119.     case OPT_ERR:
  120.       break;
  121.  
  122.     default:
  123.       printf ("?? getopt returned character code 0%o ??\n", c);
  124.     }
  125.     }
  126.  
  127.   if (optind < argc)
  128.     {
  129.       printf ("non-option ARGV-elements: ");
  130.       while (optind < argc)
  131.     printf ("%s ", argv[optind++]);
  132.       printf ("\n");
  133.     }
  134.  
  135.   return 0;
  136. }
  137.  
  138. #endif /* TEST */
  139.