416
const struct option *options, int flags)
417
{
418
memset(ctx, 0, sizeof(*ctx));
419
- ctx->argc = ctx->total = argc - 1;
420
- ctx->argv = argv + 1;
421
- ctx->out = argv;
419
+ ctx->argc = argc;
420
+ ctx->argv = argv;
421
+ if (!(flags & PARSE_OPT_ONE_SHOT)) {
422
+ ctx->argc--;
423
+ ctx->argv++;
424
+ }
425
+ ctx->total = ctx->argc;
426
+ ctx->out = argv;
427
ctx->prefix = prefix;
428
ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
429
ctx->flags = flags;
430
if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
426
- (flags & PARSE_OPT_STOP_AT_NON_OPTION))
431
+ (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
432
+ !(flags & PARSE_OPT_ONE_SHOT))
433
BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
434
+ if ((flags & PARSE_OPT_ONE_SHOT) &&
435
+ (flags & PARSE_OPT_KEEP_ARGV0))
436
+ BUG("Can't keep argv0 if you don't have it");
437
parse_options_check(options);
438
}
439
545
for (; ctx->argc; ctx->argc--, ctx->argv++) {
546
const char *arg = ctx->argv[0];
547
548
+ if (ctx->flags & PARSE_OPT_ONE_SHOT &&
549
+ ctx->argc != ctx->total)
550
+ break;
551
+
552
if (*arg != '-' || !arg[1]) {
553
if (parse_nodash_opt(ctx, arg, options) == 0)
554
continue;
623
}
624
continue;
625
unknown:
626
+ if (ctx->flags & PARSE_OPT_ONE_SHOT)
627
+ break;
628
if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
629
return PARSE_OPT_UNKNOWN;
630
ctx->out[ctx->cpidx++] = ctx->argv[0];
638
639
int parse_options_end(struct parse_opt_ctx_t *ctx)
640
{
641
+ if (ctx->flags & PARSE_OPT_ONE_SHOT)
642
+ return ctx->total - ctx->argc;
643
+
644
MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
645
ctx->out[ctx->cpidx + ctx->argc] = NULL;
646
return ctx->cpidx + ctx->argc;