GETOPT1.C 3.3 KB

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