getopt1.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* getopt_long and getopt_long_only entry points for GNU getopt.
  2. Copyright (C) 1987-2017 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library and is also part of gnulib.
  4. Patches to this file should be submitted to both projects.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public
  7. License as published by the Free Software Foundation; either
  8. version 3 of the License, or (at your option) any later version.
  9. The GNU C Library 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 GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef _LIBC
  17. # include <config.h>
  18. #endif
  19. #include "getopt.h"
  20. #include "getopt_int.h"
  21. int
  22. getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
  23. const struct option *long_options, int *opt_index)
  24. {
  25. return _getopt_internal (argc, (char **) argv, options, long_options,
  26. opt_index, 0, 0);
  27. }
  28. int
  29. _getopt_long_r (int argc, char **argv, const char *options,
  30. const struct option *long_options, int *opt_index,
  31. struct _getopt_data *d)
  32. {
  33. return _getopt_internal_r (argc, argv, options, long_options, opt_index,
  34. 0, d, 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 (int argc, char *__getopt_argv_const *argv,
  42. const char *options,
  43. const struct option *long_options, int *opt_index)
  44. {
  45. return _getopt_internal (argc, (char **) argv, options, long_options,
  46. opt_index, 1, 0);
  47. }
  48. int
  49. _getopt_long_only_r (int argc, char **argv, const char *options,
  50. const struct option *long_options, int *opt_index,
  51. struct _getopt_data *d)
  52. {
  53. return _getopt_internal_r (argc, argv, options, long_options, opt_index,
  54. 1, d, 0);
  55. }
  56. #ifdef TEST
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. int
  60. main (int argc, char **argv)
  61. {
  62. int c;
  63. int digit_optind = 0;
  64. while (1)
  65. {
  66. int this_option_optind = optind ? optind : 1;
  67. int option_index = 0;
  68. static const struct option long_options[] =
  69. {
  70. {"add", 1, 0, 0},
  71. {"append", 0, 0, 0},
  72. {"delete", 1, 0, 0},
  73. {"verbose", 0, 0, 0},
  74. {"create", 0, 0, 0},
  75. {"file", 1, 0, 0},
  76. {0, 0, 0, 0}
  77. };
  78. c = getopt_long (argc, argv, "abc:d:0123456789",
  79. long_options, &option_index);
  80. if (c == -1)
  81. break;
  82. switch (c)
  83. {
  84. case 0:
  85. printf ("option %s", long_options[option_index].name);
  86. if (optarg)
  87. printf (" with arg %s", optarg);
  88. printf ("\n");
  89. break;
  90. case '0':
  91. case '1':
  92. case '2':
  93. case '3':
  94. case '4':
  95. case '5':
  96. case '6':
  97. case '7':
  98. case '8':
  99. case '9':
  100. if (digit_optind != 0 && digit_optind != this_option_optind)
  101. printf ("digits occur in two different argv-elements.\n");
  102. digit_optind = this_option_optind;
  103. printf ("option %c\n", c);
  104. break;
  105. case 'a':
  106. printf ("option a\n");
  107. break;
  108. case 'b':
  109. printf ("option b\n");
  110. break;
  111. case 'c':
  112. printf ("option c with value '%s'\n", optarg);
  113. break;
  114. case 'd':
  115. printf ("option d with value '%s'\n", optarg);
  116. break;
  117. case '?':
  118. break;
  119. default:
  120. printf ("?? getopt returned character code 0%o ??\n", c);
  121. }
  122. }
  123. if (optind < argc)
  124. {
  125. printf ("non-option ARGV-elements: ");
  126. while (optind < argc)
  127. printf ("%s ", argv[optind++]);
  128. printf ("\n");
  129. }
  130. exit (0);
  131. }
  132. #endif /* TEST */