Skip to content

Commit 7e7bbcb

Browse files
MadCodergitster
authored andcommitted
parse-opt: have parse_options_{start,end}.
Make the struct optparse_t public under the better name parse_opt_ctx_t. Signed-off-by: Pierre Habouzit <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 112db55 commit 7e7bbcb

File tree

2 files changed

+55
-30
lines changed

2 files changed

+55
-30
lines changed

parse-options.c

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@
44
#define OPT_SHORT 1
55
#define OPT_UNSET 2
66

7-
struct optparse_t {
8-
const char **argv;
9-
const char **out;
10-
int argc, cpidx;
11-
const char *opt;
12-
};
13-
14-
static inline const char *get_arg(struct optparse_t *p)
7+
static inline const char *get_arg(struct parse_opt_ctx_t *p)
158
{
169
if (p->opt) {
1710
const char *res = p->opt;
@@ -37,7 +30,7 @@ static int opterror(const struct option *opt, const char *reason, int flags)
3730
return error("option `%s' %s", opt->long_name, reason);
3831
}
3932

40-
static int get_value(struct optparse_t *p,
33+
static int get_value(struct parse_opt_ctx_t *p,
4134
const struct option *opt, int flags)
4235
{
4336
const char *s, *arg;
@@ -131,7 +124,7 @@ static int get_value(struct optparse_t *p,
131124
}
132125
}
133126

134-
static int parse_short_opt(struct optparse_t *p, const struct option *options)
127+
static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
135128
{
136129
for (; options->type != OPTION_END; options++) {
137130
if (options->short_name == *p->opt) {
@@ -142,7 +135,7 @@ static int parse_short_opt(struct optparse_t *p, const struct option *options)
142135
return error("unknown switch `%c'", *p->opt);
143136
}
144137

145-
static int parse_long_opt(struct optparse_t *p, const char *arg,
138+
static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
146139
const struct option *options)
147140
{
148141
const char *arg_end = strchr(arg, '=');
@@ -247,45 +240,63 @@ void check_typos(const char *arg, const struct option *options)
247240
}
248241
}
249242

243+
void parse_options_start(struct parse_opt_ctx_t *ctx,
244+
int argc, const char **argv, int flags)
245+
{
246+
memset(ctx, 0, sizeof(*ctx));
247+
ctx->argc = argc - 1;
248+
ctx->argv = argv + 1;
249+
ctx->out = argv;
250+
ctx->flags = flags;
251+
}
252+
253+
int parse_options_end(struct parse_opt_ctx_t *ctx)
254+
{
255+
memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
256+
ctx->out[ctx->cpidx + ctx->argc] = NULL;
257+
return ctx->cpidx + ctx->argc;
258+
}
259+
250260
static NORETURN void usage_with_options_internal(const char * const *,
251261
const struct option *, int);
252262

253263
int parse_options(int argc, const char **argv, const struct option *options,
254264
const char * const usagestr[], int flags)
255265
{
256-
struct optparse_t args = { argv + 1, argv, argc - 1, 0, NULL };
266+
struct parse_opt_ctx_t ctx;
257267

258-
for (; args.argc; args.argc--, args.argv++) {
259-
const char *arg = args.argv[0];
268+
parse_options_start(&ctx, argc, argv, flags);
269+
for (; ctx.argc; ctx.argc--, ctx.argv++) {
270+
const char *arg = ctx.argv[0];
260271

261272
if (*arg != '-' || !arg[1]) {
262-
if (flags & PARSE_OPT_STOP_AT_NON_OPTION)
273+
if (ctx.flags & PARSE_OPT_STOP_AT_NON_OPTION)
263274
break;
264-
args.out[args.cpidx++] = args.argv[0];
275+
ctx.out[ctx.cpidx++] = ctx.argv[0];
265276
continue;
266277
}
267278

268279
if (arg[1] != '-') {
269-
args.opt = arg + 1;
270-
if (*args.opt == 'h')
280+
ctx.opt = arg + 1;
281+
if (*ctx.opt == 'h')
271282
usage_with_options(usagestr, options);
272-
if (parse_short_opt(&args, options) < 0)
283+
if (parse_short_opt(&ctx, options) < 0)
273284
usage_with_options(usagestr, options);
274-
if (args.opt)
285+
if (ctx.opt)
275286
check_typos(arg + 1, options);
276-
while (args.opt) {
277-
if (*args.opt == 'h')
287+
while (ctx.opt) {
288+
if (*ctx.opt == 'h')
278289
usage_with_options(usagestr, options);
279-
if (parse_short_opt(&args, options) < 0)
290+
if (parse_short_opt(&ctx, options) < 0)
280291
usage_with_options(usagestr, options);
281292
}
282293
continue;
283294
}
284295

285296
if (!arg[2]) { /* "--" */
286-
if (!(flags & PARSE_OPT_KEEP_DASHDASH)) {
287-
args.argc--;
288-
args.argv++;
297+
if (!(ctx.flags & PARSE_OPT_KEEP_DASHDASH)) {
298+
ctx.argc--;
299+
ctx.argv++;
289300
}
290301
break;
291302
}
@@ -294,13 +305,11 @@ int parse_options(int argc, const char **argv, const struct option *options,
294305
usage_with_options_internal(usagestr, options, 1);
295306
if (!strcmp(arg + 2, "help"))
296307
usage_with_options(usagestr, options);
297-
if (parse_long_opt(&args, arg + 2, options))
308+
if (parse_long_opt(&ctx, arg + 2, options))
298309
usage_with_options(usagestr, options);
299310
}
300311

301-
memmove(args.out + args.cpidx, args.argv, args.argc * sizeof(*args.out));
302-
args.out[args.cpidx + args.argc] = NULL;
303-
return args.cpidx + args.argc;
312+
return parse_options_end(&ctx);
304313
}
305314

306315
#define USAGE_OPTS_WIDTH 24

parse-options.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ extern int parse_options(int argc, const char **argv,
111111
extern NORETURN void usage_with_options(const char * const *usagestr,
112112
const struct option *options);
113113

114+
/*----- incremantal advanced APIs -----*/
115+
116+
struct parse_opt_ctx_t {
117+
const char **argv;
118+
const char **out;
119+
int argc, cpidx;
120+
const char *opt;
121+
int flags;
122+
};
123+
124+
extern void parse_options_start(struct parse_opt_ctx_t *ctx,
125+
int argc, const char **argv, int flags);
126+
127+
extern int parse_options_end(struct parse_opt_ctx_t *ctx);
128+
129+
114130
/*----- some often used options -----*/
115131
extern int parse_opt_abbrev_cb(const struct option *, const char *, int);
116132
extern int parse_opt_approxidate_cb(const struct option *, const char *, int);

0 commit comments

Comments
 (0)