Skip to content

Commit ff43ec3

Browse files
MadCodergitster
authored andcommitted
parse-opt: create parse_options_step.
For now it's unable to stop at unknown options, this commit merely reorganize some code around. Signed-off-by: Pierre Habouzit <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ee68b87 commit ff43ec3

File tree

2 files changed

+56
-39
lines changed

2 files changed

+56
-39
lines changed

parse-options.c

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -250,63 +250,78 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
250250
ctx->flags = flags;
251251
}
252252

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-
260253
static int usage_with_options_internal(const char * const *,
261-
const struct option *, int, int);
254+
const struct option *, int);
262255

263-
int parse_options(int argc, const char **argv, const struct option *options,
264-
const char * const usagestr[], int flags)
256+
int parse_options_step(struct parse_opt_ctx_t *ctx,
257+
const struct option *options,
258+
const char * const usagestr[])
265259
{
266-
struct parse_opt_ctx_t ctx;
267-
268-
parse_options_start(&ctx, argc, argv, flags);
269-
for (; ctx.argc; ctx.argc--, ctx.argv++) {
270-
const char *arg = ctx.argv[0];
260+
for (; ctx->argc; ctx->argc--, ctx->argv++) {
261+
const char *arg = ctx->argv[0];
271262

272263
if (*arg != '-' || !arg[1]) {
273-
if (ctx.flags & PARSE_OPT_STOP_AT_NON_OPTION)
264+
if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
274265
break;
275-
ctx.out[ctx.cpidx++] = ctx.argv[0];
266+
ctx->out[ctx->cpidx++] = ctx->argv[0];
276267
continue;
277268
}
278269

279270
if (arg[1] != '-') {
280-
ctx.opt = arg + 1;
281-
if (*ctx.opt == 'h')
282-
usage_with_options(usagestr, options);
283-
if (parse_short_opt(&ctx, options) < 0)
271+
ctx->opt = arg + 1;
272+
if (*ctx->opt == 'h')
273+
return parse_options_usage(usagestr, options);
274+
if (parse_short_opt(ctx, options) < 0)
284275
usage_with_options(usagestr, options);
285-
if (ctx.opt)
276+
if (ctx->opt)
286277
check_typos(arg + 1, options);
287-
while (ctx.opt) {
288-
if (*ctx.opt == 'h')
289-
usage_with_options(usagestr, options);
290-
if (parse_short_opt(&ctx, options) < 0)
278+
while (ctx->opt) {
279+
if (*ctx->opt == 'h')
280+
return parse_options_usage(usagestr, options);
281+
if (parse_short_opt(ctx, options) < 0)
291282
usage_with_options(usagestr, options);
292283
}
293284
continue;
294285
}
295286

296287
if (!arg[2]) { /* "--" */
297-
if (!(ctx.flags & PARSE_OPT_KEEP_DASHDASH)) {
298-
ctx.argc--;
299-
ctx.argv++;
288+
if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
289+
ctx->argc--;
290+
ctx->argv++;
300291
}
301292
break;
302293
}
303294

304295
if (!strcmp(arg + 2, "help-all"))
305-
usage_with_options_internal(usagestr, options, 1, 1);
296+
return usage_with_options_internal(usagestr, options, 1);
306297
if (!strcmp(arg + 2, "help"))
298+
return parse_options_usage(usagestr, options);
299+
if (parse_long_opt(ctx, arg + 2, options))
307300
usage_with_options(usagestr, options);
308-
if (parse_long_opt(&ctx, arg + 2, options))
309-
usage_with_options(usagestr, options);
301+
}
302+
return PARSE_OPT_DONE;
303+
}
304+
305+
int parse_options_end(struct parse_opt_ctx_t *ctx)
306+
{
307+
memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
308+
ctx->out[ctx->cpidx + ctx->argc] = NULL;
309+
return ctx->cpidx + ctx->argc;
310+
}
311+
312+
int parse_options(int argc, const char **argv, const struct option *options,
313+
const char * const usagestr[], int flags)
314+
{
315+
struct parse_opt_ctx_t ctx;
316+
317+
parse_options_start(&ctx, argc, argv, flags);
318+
switch (parse_options_step(&ctx, options, usagestr)) {
319+
case PARSE_OPT_HELP:
320+
exit(129);
321+
case PARSE_OPT_DONE:
322+
break;
323+
default: /* PARSE_OPT_UNKNOWN */
324+
abort(); /* unreached yet */
310325
}
311326

312327
return parse_options_end(&ctx);
@@ -316,7 +331,7 @@ int parse_options(int argc, const char **argv, const struct option *options,
316331
#define USAGE_GAP 2
317332

318333
int usage_with_options_internal(const char * const *usagestr,
319-
const struct option *opts, int full, int do_exit)
334+
const struct option *opts, int full)
320335
{
321336
fprintf(stderr, "usage: %s\n", *usagestr++);
322337
while (*usagestr && **usagestr)
@@ -401,22 +416,20 @@ int usage_with_options_internal(const char * const *usagestr,
401416
}
402417
fputc('\n', stderr);
403418

404-
if (do_exit)
405-
exit(129);
406419
return PARSE_OPT_HELP;
407420
}
408421

409422
void usage_with_options(const char * const *usagestr,
410-
const struct option *opts)
423+
const struct option *opts)
411424
{
412-
usage_with_options_internal(usagestr, opts, 0, 1);
413-
exit(129); /* make gcc happy */
425+
usage_with_options_internal(usagestr, opts, 0);
426+
exit(129);
414427
}
415428

416429
int parse_options_usage(const char * const *usagestr,
417430
const struct option *opts)
418431
{
419-
return usage_with_options_internal(usagestr, opts, 0, 0);
432+
return usage_with_options_internal(usagestr, opts, 0);
420433
}
421434

422435

parse-options.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ extern int parse_options_usage(const char * const *usagestr,
133133
extern void parse_options_start(struct parse_opt_ctx_t *ctx,
134134
int argc, const char **argv, int flags);
135135

136+
extern int parse_options_step(struct parse_opt_ctx_t *ctx,
137+
const struct option *options,
138+
const char * const usagestr[]);
139+
136140
extern int parse_options_end(struct parse_opt_ctx_t *ctx);
137141

138142

0 commit comments

Comments
 (0)