/Python/getopt.c

http://unladen-swallow.googlecode.com/ · C · 127 lines · 73 code · 22 blank · 32 comment · 36 complexity · f6b958155aa3dd4bf3838c2869fcb478 MD5 · raw file

  1. /*---------------------------------------------------------------------------*
  2. * <RCS keywords>
  3. *
  4. * C++ Library
  5. *
  6. * Copyright 1992-1994, David Gottner
  7. *
  8. * All Rights Reserved
  9. *
  10. * Permission to use, copy, modify, and distribute this software and its
  11. * documentation for any purpose and without fee is hereby granted,
  12. * provided that the above copyright notice, this permission notice and
  13. * the following disclaimer notice appear unmodified in all copies.
  14. *
  15. * I DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL I
  17. * BE LIABLE FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
  18. * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER
  19. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  20. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21. *
  22. * Nevertheless, I would like to know about bugs in this library or
  23. * suggestions for improvment. Send bug reports and feedback to
  24. * davegottner@delphi.com.
  25. *---------------------------------------------------------------------------*/
  26. /* Modified to support --help and --version, as well as /? on Windows
  27. * by Georg Brandl. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. int _PyOS_opterr = 1; /* generate error messages */
  34. int _PyOS_optind = 1; /* index into argv array */
  35. char *_PyOS_optarg = NULL; /* optional argument */
  36. int _PyOS_GetOpt(int argc, char **argv, char *optstring)
  37. {
  38. static char *opt_ptr = "";
  39. char *ptr;
  40. int option;
  41. if (*opt_ptr == '\0') {
  42. if (_PyOS_optind >= argc)
  43. return -1;
  44. #ifdef MS_WINDOWS
  45. else if (strcmp(argv[_PyOS_optind], "/?") == 0) {
  46. ++_PyOS_optind;
  47. return 'h';
  48. }
  49. #endif
  50. else if (argv[_PyOS_optind][0] != '-' ||
  51. argv[_PyOS_optind][1] == '\0' /* lone dash */ )
  52. return -1;
  53. else if (strcmp(argv[_PyOS_optind], "--") == 0) {
  54. ++_PyOS_optind;
  55. return -1;
  56. }
  57. else if (strcmp(argv[_PyOS_optind], "--help") == 0) {
  58. ++_PyOS_optind;
  59. return 'h';
  60. }
  61. else if (strcmp(argv[_PyOS_optind], "--version") == 0) {
  62. ++_PyOS_optind;
  63. return 'V';
  64. }
  65. opt_ptr = &argv[_PyOS_optind++][1];
  66. }
  67. if ( (option = *opt_ptr++) == '\0')
  68. return -1;
  69. if (option == 'J') {
  70. fprintf(stderr, "-J is reserved for Jython\n");
  71. return '_';
  72. }
  73. if ((ptr = strchr(optstring, option)) == NULL) {
  74. if (_PyOS_opterr)
  75. fprintf(stderr, "Unknown option: -%c\n", option);
  76. return '_';
  77. }
  78. /* The * modifier says that this option _can_ take an argument, but
  79. not necessarily (unlike :, which requires the argument). If no
  80. argument was passed, _PyOS_optarg will be \0. The * modifier
  81. requires the option's argument to be cuddled up to the flag, for
  82. example: -O2 is legal, -O 2 is not. */
  83. if (*(ptr + 1) == '*') {
  84. _PyOS_optarg = opt_ptr;
  85. opt_ptr = "";
  86. } else if (*(ptr + 1) == ':') {
  87. if (*opt_ptr != '\0') {
  88. _PyOS_optarg = opt_ptr;
  89. opt_ptr = "";
  90. }
  91. else {
  92. if (_PyOS_optind >= argc) {
  93. if (_PyOS_opterr)
  94. fprintf(stderr,
  95. "Argument expected for the -%c option\n", option);
  96. return '_';
  97. }
  98. _PyOS_optarg = argv[_PyOS_optind++];
  99. }
  100. }
  101. return option;
  102. }
  103. #ifdef __cplusplus
  104. }
  105. #endif