getopt1.c 3.3 KB

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