-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Expand file tree
/
Copy pathgetopt.c
More file actions
187 lines (164 loc) · 5.11 KB
/
Copy pathgetopt.c
File metadata and controls
187 lines (164 loc) · 5.11 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*---------------------------------------------------------------------------*
* <RCS keywords>
*
* C++ Library
*
* Copyright 1992-1994, David Gottner
*
* All Rights Reserved
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice, this permission notice and
* the following disclaimer notice appear unmodified in all copies.
*
* I DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL I
* BE LIABLE FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
* DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*---------------------------------------------------------------------------*/
/* Modified to support --help and --version, as well as /? on Windows
* by Georg Brandl. */
#include <Python.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include "pycore_getopt.h" // struct _PyOS_GetOpt
/* Python command line short and long options */
#define SHORT_OPTS L"bBc:dEhiIm:OPqRsStuvVW:xX:?"
typedef struct {
const wchar_t *name;
int has_arg;
int val;
} _PyOS_LongOption;
static const _PyOS_LongOption longopts[] = {
/* name, has_arg, val (used in switch in initconfig.c) */
{L"check-hash-based-pycs", 1, 1},
{L"help-all", 0, 2},
{L"help-env", 0, 3},
{L"help-xoptions", 0, 4},
{NULL, 0, -1}, /* sentinel */
};
void
_PyOS_GetOpt_Init(struct _PyOS_GetOpt *getopt,
Py_ssize_t argc, wchar_t * const *argv)
{
getopt->error = 1;
getopt->index = 1;
getopt->arg = NULL;
getopt->ptr = L"";
getopt->argc = argc;
getopt->argv = argv;
}
// Parse a command line option.
//
// Return a character for short option (ex: return 'h' for -h).
// Return a number for long options (see 'longopts' array).
// Return '_' on unknown option or missing argument.
// Return -1 when done.
//
// Return 'h' for --help and return 'V' for --version.
//
// If an option has an argument, set getopt->arg to the argument.
// If getopt->error is non-error, write error messages to stderr.
int
_PyOS_GetOpt(struct _PyOS_GetOpt *getopt)
{
// Local copy of read-only members to omit "getopt->"
const Py_ssize_t argc = getopt->argc;
wchar_t * const *argv = getopt->argv;
const int error = getopt->error;
if (*getopt->ptr == '\0') {
if (getopt->index >= argc) {
return -1;
}
const wchar_t *arg = argv[getopt->index];
#ifdef MS_WINDOWS
if (wcscmp(arg, L"/?") == 0) {
++getopt->index;
return 'h';
}
#endif
if (arg[0] != L'-' || arg[1] == L'\0' /* lone dash */ ) {
return -1;
}
if (wcscmp(arg, L"--") == 0) {
++getopt->index;
return -1;
}
if (wcscmp(arg, L"--help") == 0) {
++getopt->index;
return 'h';
}
if (wcscmp(arg, L"--version") == 0) {
++getopt->index;
return 'V';
}
getopt->ptr = &argv[getopt->index++][1];
}
wchar_t option = *getopt->ptr++;
if (option == L'\0') {
return -1;
}
if (option == L'-') {
// Parse long option.
if (*getopt->ptr == L'\0') {
if (error) {
fprintf(stderr, "Expected long option\n");
}
return -1;
}
int longindex = 0;
const _PyOS_LongOption *opt;
for (opt = &longopts[longindex]; opt->name; opt = &longopts[++longindex]) {
if (wcscmp(opt->name, getopt->ptr) == 0) {
break;
}
}
if (!opt->name) {
if (error) {
fprintf(stderr, "Unknown option: %ls\n", argv[getopt->index - 1]);
}
return '_';
}
getopt->ptr = L"";
if (!opt->has_arg) {
return opt->val;
}
if (getopt->index >= argc) {
if (error) {
fprintf(stderr, "Argument expected for the %ls options\n",
argv[getopt->index - 1]);
}
return '_';
}
getopt->arg = argv[getopt->index++];
return opt->val;
}
wchar_t *ptr = wcschr(SHORT_OPTS, option);
if (ptr == NULL) {
if (error) {
fprintf(stderr, "Unknown option: -%lc\n", option);
}
return '_';
}
if (*(ptr + 1) == L':') {
if (*getopt->ptr != L'\0') {
getopt->arg = getopt->ptr;
getopt->ptr = L"";
}
else {
if (getopt->index >= argc) {
if (error) {
fprintf(stderr,
"Argument expected for the -%lc option\n", option);
}
return '_';
}
getopt->arg = argv[getopt->index++];
}
}
return option;
}

