-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargparse.c
323 lines (291 loc) · 9.98 KB
/
argparse.c
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright 2015 Malcolm Inglis <http://minglis.id.au>
//
// This file is part of Libargs.
//
// Libargs is free software: you can redistribute it and/or modify it under
// the terms of the GNU Affero General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// Libargs is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
// more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Libargs. If not, see <https://gnu.org/licenses/>.
#include "argparse.h"
#include <errno.h>
#include <libmacro/assert.h>
#include <libmacro/debug.h>
#include <libstr/str.h>
#include <libarray/array_str.h>
void
arg_parse_str(
char const * const _,
char const * const arg,
void * const vdest )
{
errno = 0;
if ( vdest == NULL ) { return; }
// Empty strings are, by default, invalid arguments:
if ( str__is_empty( arg ) ) {
errno = EINVAL;
} else {
char const * * const dest = vdest;
*dest = arg;
}
}
void
arg_set_false(
char const * const _1,
char const * const _2,
void * const vdest )
{
ASSERT( vdest != NULL );
bool * const dest = vdest;
*dest = false;
}
void
arg_set_true(
char const * const _1,
char const * const _2,
void * const vdest )
{
ASSERT( vdest != NULL );
bool * const dest = vdest;
*dest = true;
}
static
ArgFlag const *
find_flag(
ArrayC_ArgFlag const flags,
char const * const arg )
{
ASSERT( arg != NULL );
for ( size_t i = 0; i < flags.length; i++ ) {
ArgFlag const * const af = flags.e + i;
if ( ( af->pattern != NULL && af->pattern( arg ) )
|| arrayc_str__elem( af->names, arg )
|| ( af->name != NULL && str__equal( af->name, arg ) ) ) {
return af;
}
}
return NULL;
}
static
ArgOption const *
find_option(
ArrayC_ArgOption const options,
char const * const arg )
{
ASSERT( arg != NULL );
for ( size_t i = 0; i < options.length; i++ ) {
ArgOption const * const ao = options.e + i;
if ( ( ao->pattern != NULL && ao->pattern( arg ) )
|| arrayc_str__elem( ao->names, arg )
|| ( ao->name != NULL && str__equal( ao->name, arg ) ) ) {
return ao;
}
}
return NULL;
}
static
bool
over_or_eq_max(
ArgsNum const argsnum,
size_t const i )
{
if ( argsnum.max == ArgsNum_NONE ) {
return true;
} else if ( argsnum.max == ArgsNum_INFINITE ) {
return false;
} else {
ASSERT( argsnum.max >= 0 );
uint const max = ( argsnum.max == 0 ) ? 1 : argsnum.max;
return i >= max;
}
}
static
bool
under_min(
ArgsNum const argsnum,
size_t const i )
{
if ( argsnum.min == ArgsNum_NONE ) {
return false;
} else {
ASSERT( argsnum.min >= 0 );
uint const min = ( argsnum.min == 0 ) ? 1 : argsnum.min;
return i < min;
}
}
void
argparse(
int const argc,
char const * const * const argv,
ArgsError * const err,
ArgsSpec const spec )
{
ASSERT( argc >= 1, argv != NULL, err != NULL );
ArrayC_str const args = arrayc_str__new( argv + 1, argc - 1 );
argparse_array( args, err, spec );
}
void
argparse_array(
ArrayC_str const args,
ArgsError * const err,
ArgsSpec const spec )
{
ASSERT( arrayc_str__is_valid( args ), err != NULL );
*err = ( ArgsError ){ .type = ArgsError_NONE };
size_t num_positionals = 0;
// Positional parsing state:
ArgPositional const * positional = NULL;
size_t positional_arg_count = 0;
bool preserve_positional = false;
// Option parsing state:
ArgOption const * option = NULL;
size_t option_arg_count = 0;
char const * option_name = NULL;
bool preserve_option = false;
// Argument parsing loop:
for ( size_t i = 0; i < args.length; i++ ) {
char const * const arg = args.e[ i ];
// Reset positional state if we aren't parsing positional args:
if ( !preserve_positional ) {
if ( positional != NULL ) {
if ( under_min( positional->num_args,
positional_arg_count ) ) {
*err = ( ArgsError ){ .type = ArgsError_MISSING_ARG,
.str = positional->name };
return;
}
num_positionals++;
}
positional = NULL;
positional_arg_count = 0;
}
preserve_positional = false;
// Reset option state if we aren't parsing option parameters:
if ( !preserve_option ) {
if ( option != NULL
&& under_min( option->num_args, option_arg_count ) ) {
*err = ( ArgsError ){ .type = ArgsError_MISSING_OPTION_ARG,
.str = option_name };
return;
}
option = NULL;
option_arg_count = 0;
option_name = NULL;
}
preserve_option = false;
// If our argument matches a flag name:
ArgFlag const * const flag = find_flag( spec.flags, arg );
if ( flag != NULL ) {
( flag->parser ? flag->parser : arg_set_true )
( arg, NULL, flag->destination );
if ( errno ) {
*err = ( ArgsError ){ .type = ArgsError_PARSE_ARG,
.error = errno,
.str = arg };
return;
}
if ( flag->stop ) { return; }
continue;
}
// Or, if our argument matches an option name:
ArgOption const * const new_option = find_option( spec.options, arg );
if ( new_option != NULL ) {
if ( option != NULL
&& under_min( option->num_args, option_arg_count ) ) {
*err = ( ArgsError ){ .type = ArgsError_MISSING_OPTION_ARG,
.str = option_name };
return;
}
option = new_option;
option_name = arg;
option_arg_count = 0;
preserve_option = true;
continue;
}
// Or, if we're parsing option arguments:
if ( option != NULL ) {
( option->parser ? option->parser : arg_parse_str )
( option_name, arg, option->destination );
if ( errno ) {
*err = ( ArgsError ){ .type = ArgsError_PARSE_ARG,
.error = errno,
.str = arg };
return;
}
option_arg_count++;
preserve_option = !over_or_eq_max( option->num_args,
option_arg_count );
continue;
}
// Or, if we have outstanding positional arguments:
if ( num_positionals < spec.positionals.length ) {
positional = spec.positionals.e + num_positionals;
( positional->parser ? positional->parser : arg_parse_str )
( positional->name, arg, positional->destination );
if ( errno ) {
*err = ( ArgsError ){ .type = ArgsError_PARSE_ARG,
.error = errno,
.str = arg };
return;
}
positional_arg_count++;
preserve_positional = !over_or_eq_max( positional->num_args,
positional_arg_count );
continue;
}
// Otherwise, the argument did not match a specified long/short
// flag/option, and we're not parsing option parameters, and there
// are no outstanding positional arguments, so it's invalid:
*err = ( ArgsError ){ .type = ArgsError_UNKNOWN_ARG,
.str = arg };
return;
}
// If we exited the loop on parsing an option, then we need to check that
// we parsed enough parameters for that option:
if ( option != NULL
&& under_min( option->num_args, option_arg_count ) ) {
*err = ( ArgsError ){ .type = ArgsError_MISSING_OPTION_ARG,
.str = option_name };
return;
}
// Or, if we exited the loop on parsing a positional, then we won't have
// been able to increment `num_positionals` as appropriate:
if ( positional != NULL ) {
if ( under_min( positional->num_args, positional_arg_count ) ) {
*err = ( ArgsError ){ .type = ArgsError_MISSING_ARG,
.str = positional->name };
return;
}
num_positionals++;
}
// If we parsed fewer than the specified number of positionals, error:
if ( num_positionals < spec.positionals.length ) {
*err = ( ArgsError ){ .type = ArgsError_MISSING_ARG,
.str = spec.positionals
.e[ num_positionals ].name };
return;
}
}
char const *
argserrortype__to_str(
enum ArgsErrorType const t )
{
switch ( t ) {
case ArgsError_NONE: return "none";
case ArgsError_ERROR: return "error";
case ArgsError_MISSING_ARG: return "missing argument";
case ArgsError_PARSE_ARG: return "parse argument";
case ArgsError_UNKNOWN_ARG: return "unknown argument";
case ArgsError_MISSING_OPTION_ARG: return "missing option argument";
case ArgsError_INCONSISTENT_ARG: return "inconsistent argument";
case ArgsError_SYSTEM: return "system error";
default: return "unknown error type";
}
}