-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargparse.c
More file actions
256 lines (229 loc) · 7.95 KB
/
argparse.c
File metadata and controls
256 lines (229 loc) · 7.95 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "fatal.h"
#include "strutil.h"
#include "readline.h"
#include "argparse.h"
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix))
#define DIRSEP "/" /* directory separator */
#else
#define DIRSEP "\\"
#endif
static char *warnmsg =
"Note: in case of this being a filename, "
"please specify '--' first, the end of option marker.\n"
"Note: type -h or --help to get help.";
// `parse_shrtop` (short option) processes options starting with `-`.
static void parse_shrtop(const char *arg, optflg_t *of);
// `parse_longop` (long option) processes options starting with `--`.
static void parse_longop(const char *arg, optflg_t *of, const char **keeppath);
// `parse_wrtmod` (writing mode) processes `-` or `+` option.
static void parse_wrtmod(const char *arg, optflg_t *of);
// `parse_endopt` (end-of-option marker) processes `--`.
static void parse_endopt(optflg_t *of);
// `parse_filenm` (filename) processes filenames.
static void parse_filenm(const char *arg, optflg_t *of, dynarr_t *files);
extern dynarr_t *parse_args(optflg_t *of, const char *argv[], const char **keeppath) {
/*
Synopsis:
(1) proofread [-l] [-f] [--dry-run[=(full|line)]] [-]
(2) proofread [-l] [-f] [--dry-run[=(full|line)]] [--mute] + [--] <file>...
(3) proofread [-l] [-f] [--keep[=<path>]] [--mute] + [--] <file>...
(4) proofread (-h|--help)
(5) proofread (-v|-vv|-vvv)
*/
dynarr_t *files = dynarr_create(sizeof (char *));
/*
The loop starts at i = 1, since argv[0] is the program's name;
it terminates when argv[i] is NULL.
*/
int i;
const char *arg;
for (i = 1; (arg = argv[i]); i++) {
/* After the end of option marker, treats EVERYTHING as filename. */
if (of->eoo) {
dynarr_append(files, &arg);
continue;
}
// detects -x options
if (strlen(arg) > 1 && strspn(arg, "-") == 1)
parse_shrtop(arg, of);
else
// detects --x options
if (strlen(arg) > 2 && strspn(arg, "-") == 2)
parse_longop(arg, of, keeppath);
else
// detects - or +
if (strlen(arg) == 1 && match(arg[0], "-+"))
parse_wrtmod(arg, of);
else
// detects --, or the end of option marker
if (strlen(arg) == 2 && !strcmp(arg, "--"))
parse_endopt(of);
else
// handles unrecognizable arguments
if (arg[0] == '-')
vfatal("proofread: unable to recognize this option: %s.\n%s", arg, warnmsg);
else
// takes the arg as a filename
parse_filenm(arg, of, files);
}
return files;
}
// `validate_hlpopt` (help option) checks whether `-h` is valid in the context it appears.
static void validate_hlpopt(optflg_t *of);
/*
FIXME: the following codes have this shape:
if (arg equals to opt_1) {
// tests to see if it is fine to take this arg
// sets the option
}
else if (arg equals to opt_2) { ... }
It seems it is possible to refactor like so:
opts = [opt_1, opt_2, ...]
handlers = [opt_1_handler, opt_2_handler, ...]
for (until the end of args) {
// checks whether arg is in opts; if exists, gets the index
// invokes the corresponding handler: handlers[index]()
}
void opt_n_handler(optflg_t *of) {
// tests to see if it is fine to take this arg
// sets the option
}
*/
static void parse_shrtop(const char *arg, optflg_t *of) {
int i;
char ch;
for (i = 1; (ch = arg[i]); i++) {
if (ch == 'l') {
if (of->eol) fatal("proofread: -l seen already.");
if (of->hlp) fatal("proofread: -h with -l.");
if (of->vsn) fatal("proofread: -v with -l.");
of->eol = true;
}
else
if (ch == 'f') {
if (of->eof) fatal("proofread: -f seen already.");
if (of->hlp) fatal("proofread: -h with -f.");
if (of->vsn) fatal("proofread: -v with -f.");
of->eof = true;
}
else
if (ch == 'h') {
validate_hlpopt(of);
of->hlp = true;
}
else
if (ch == 'v') {
if (of->eol) fatal("proofread: -l with -v.");
if (of->eof) fatal("proofread: -f with -v.");
if (of->hlp) fatal("proofread: -h with -v.");
if (of->drn) fatal("proofread: --dry-run with -v.");
if (of->sto) fatal("proofread: - with -v.");
if (of->owf) fatal("proofread: + with -v.");
if (of->vsn == 3)
fatal("proofread: -v specified more than three times.");
if (of->kep) fatal("proofread: --keep with -v.");
if (of->mut) fatal("proofread: --mute with -v.");
of->vsn++;
}
else
// handles unrecognizable arguments
vfatal("proofread: unable to recognize this option: -%c.\n%s", ch, warnmsg);
} /* end of loop */
}
static void validate_hlpopt(optflg_t *of) {
if (of->eol) fatal("proofread: -l with -h.");
if (of->eof) fatal("proofread: -f with -h.");
if (of->hlp) fatal("proofread: -h seen already.");
if (of->vsn) fatal("proofread: -v with -h.");
if (of->drn) fatal("proofread: --dry-run with -h.");
if (of->sto) fatal("proofread: - with -h.");
if (of->owf) fatal("proofread: + with -h.");
if (of->kep) fatal("proofread: --keep with -h.");
if (of->mut) fatal("proofread: --mute with -h.");
}
static void parse_longop(const char *arg, optflg_t *of, const char **keeppath) {
// skips --
arg += 2;
// detects --dry-run
if (!strncmp(arg, "dry-run", 7)) {
if (of->hlp) fatal("proofread: -h with --dry-run.");
if (of->vsn) fatal("proofread: -v with --dry-run.");
if (of->drn) fatal("proofread: --dry-run seen already.");
if (of->kep) fatal("proofread: --keep with --dry-run.");
arg += 7;
if (strlen(arg) > 0) {
if (!strcmp(arg, "=full"))
of->ful = true;
else
if (!strcmp(arg, "=line"))
of->lne = true;
else
vfatal("proofread: unable to recognize the suboption: %s.\n%s", arg, warnmsg);
}
of->drn = true;
}
else
// detects --help
if (!strncmp(arg, "help", 4)) {
validate_hlpopt(of);
of->hlp = true;
}
else
// detects --keep
if (!strncmp(arg, "keep", 4)) {
if (of->hlp) fatal("proofread: -h with --keep.");
if (of->vsn) fatal("proofread: -v with --keep.");
if (of->drn) fatal("proofread: --dry-run with --keep.");
if (of->sto) fatal("proofread: - with --keep.");
if (of->kep) fatal("proofread: --keep seen already.");
arg += 4;
if (strlen(arg) > 0) {
if (arg[0] != '=')
fatal("proofread: --keep has an incorrect syntax.");
*keeppath = arg + 1;
}
else
*keeppath = "bak";
of->kep = true;
}
else
// detects --mute
if (!strncmp(arg, "mute", 4)) {
if (of->hlp) fatal("proofread: -h with --mute.");
if (of->vsn) fatal("proofread: -v with --mute.");
if (of->sto) fatal("proofread: - with --mute.");
of->mut = true;
}
else
// handles unrecognizable arguments
vfatal("proofread: unable to recognize this option: --%s.\n%s", arg, warnmsg);
}
static void parse_wrtmod(const char *arg, optflg_t *of) {
if (arg[0] == '-') {
if (of->hlp) fatal("proofread: -h with -.");
if (of->vsn) fatal("proofread: -v with -.");
if (of->kep) fatal("proofread: --keep with -.");
if (of->mut) fatal("proofread: --mute with -.");
of->sto = true;
}
else { /* '+' */
if (of->hlp) fatal("proofread: -h with +.");
if (of->vsn) fatal("proofread: -v with +.");
of->owf = true;
}
}
static void parse_endopt(optflg_t *of) {
if (of->hlp) fatal("proofread: -h with --.");
if (of->vsn) fatal("proofread: -v with --.");
if (of->sto) fatal("proofread: - with --.");
if (!of->owf) fatal("proofread: + must be specified before --.");
of->eoo = true;
}
static void parse_filenm(const char *arg, optflg_t *of, dynarr_t *files) {
if (!of->owf) fatal("proofread: + option must be present before a filename.");
dynarr_append(files, &arg);
}