| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#include <stdio.h> |
| 31 |
#include <string.h> |
| 32 |
|
| 33 |
#ifdef __cplusplus |
| 34 |
extern "C" { |
| 35 |
#endif |
| 36 |
|
| 37 |
int _PyOS_opterr = 1; |
| 38 |
int _PyOS_optind = 1; |
| 39 |
char *_PyOS_optarg = NULL; |
| 40 |
static char *opt_ptr = ""; |
| 41 |
|
| 42 |
void _PyOS_ResetGetOpt(void) |
| 43 |
{ |
| 44 |
_PyOS_opterr = 1; |
| 45 |
_PyOS_optind = 1; |
| 46 |
_PyOS_optarg = NULL; |
| 47 |
opt_ptr = ""; |
| 48 |
} |
| 49 |
|
| 50 |
int _PyOS_GetOpt(int argc, char **argv, char *optstring) |
| 51 |
{ |
| 52 |
char *ptr; |
| 53 |
int option; |
| 54 |
|
| 55 |
if (*opt_ptr == '\0') { |
| 56 |
|
| 57 |
if (_PyOS_optind >= argc) |
| 58 |
return -1; |
| 59 |
#ifdef MS_WINDOWS |
| 60 |
else if (strcmp(argv[_PyOS_optind], "/?") == 0) { |
| 61 |
++_PyOS_optind; |
| 62 |
return 'h'; |
| 63 |
} |
| 64 |
#endif |
| 65 |
|
| 66 |
else if (argv[_PyOS_optind][0] != '-' || |
| 67 |
argv[_PyOS_optind][1] == '\0' ) |
| 68 |
return -1; |
| 69 |
|
| 70 |
else if (strcmp(argv[_PyOS_optind], "--") == 0) { |
| 71 |
++_PyOS_optind; |
| 72 |
return -1; |
| 73 |
} |
| 74 |
|
| 75 |
else if (strcmp(argv[_PyOS_optind], "--help") == 0) { |
| 76 |
++_PyOS_optind; |
| 77 |
return 'h'; |
| 78 |
} |
| 79 |
|
| 80 |
else if (strcmp(argv[_PyOS_optind], "--version") == 0) { |
| 81 |
++_PyOS_optind; |
| 82 |
return 'V'; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
opt_ptr = &argv[_PyOS_optind++][1]; |
| 87 |
} |
| 88 |
|
| 89 |
if ((option = *opt_ptr++) == '\0') |
| 90 |
return -1; |
| 91 |
|
| 92 |
if (option == 'J') { |
| 93 |
if (_PyOS_opterr) |
| 94 |
fprintf(stderr, "-J is reserved for Jython\n"); |
| 95 |
return '_'; |
| 96 |
} |
| 97 |
|
| 98 |
if (option == 'X') { |
| 99 |
if (_PyOS_opterr) |
| 100 |
fprintf(stderr, |
| 101 |
"-X is reserved for implementation-specific arguments\n"); |
| 102 |
return '_'; |
| 103 |
} |
| 104 |
|
| 105 |
if ((ptr = strchr(optstring, option)) == NULL) { |
| 106 |
if (_PyOS_opterr) |
| 107 |
fprintf(stderr, "Unknown option: -%c\n", option); |
| 108 |
|
| 109 |
return '_'; |
| 110 |
} |
| 111 |
|
| 112 |
if (*(ptr + 1) == ':') { |
| 113 |
if (*opt_ptr != '\0') { |
| 114 |
_PyOS_optarg = opt_ptr; |
| 115 |
opt_ptr = ""; |
| 116 |
} |
| 117 |
|
| 118 |
else { |
| 119 |
if (_PyOS_optind >= argc) { |
| 120 |
if (_PyOS_opterr) |
| 121 |
fprintf(stderr, |
| 122 |
"Argument expected for the -%c option\n", option); |
| 123 |
return '_'; |
| 124 |
} |
| 125 |
|
| 126 |
_PyOS_optarg = argv[_PyOS_optind++]; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
return option; |
| 131 |
} |
| 132 |
|
| 133 |
#ifdef __cplusplus |
| 134 |
} |
| 135 |
#endif |
| 136 |
|