Lines Matching refs:ctx
493 static void parse_options_start(struct parse_opt_ctx_t *ctx, in parse_options_start() argument
496 memset(ctx, 0, sizeof(*ctx)); in parse_options_start()
497 ctx->argc = argc - 1; in parse_options_start()
498 ctx->argv = argv + 1; in parse_options_start()
499 ctx->out = argv; in parse_options_start()
500 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0); in parse_options_start()
501 ctx->flags = flags; in parse_options_start()
511 static int parse_options_step(struct parse_opt_ctx_t *ctx, in parse_options_step() argument
515 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP); in parse_options_step()
520 ctx->opt = NULL; in parse_options_step()
522 for (; ctx->argc; ctx->argc--, ctx->argv++) { in parse_options_step()
523 arg = ctx->argv[0]; in parse_options_step()
525 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION) in parse_options_step()
527 ctx->out[ctx->cpidx++] = ctx->argv[0]; in parse_options_step()
532 ctx->opt = ++arg; in parse_options_step()
533 if (internal_help && *ctx->opt == 'h') { in parse_options_step()
534 return usage_with_options_internal(usagestr, options, 0, ctx); in parse_options_step()
536 switch (parse_short_opt(ctx, options)) { in parse_options_step()
546 if (ctx->opt) in parse_options_step()
548 while (ctx->opt) { in parse_options_step()
549 if (internal_help && *ctx->opt == 'h') in parse_options_step()
550 return usage_with_options_internal(usagestr, options, 0, ctx); in parse_options_step()
551 arg = ctx->opt; in parse_options_step()
552 switch (parse_short_opt(ctx, options)) { in parse_options_step()
561 ctx->argv[0] = strdup(ctx->opt - 1); in parse_options_step()
562 *(char *)ctx->argv[0] = '-'; in parse_options_step()
574 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) { in parse_options_step()
575 ctx->argc--; in parse_options_step()
576 ctx->argv++; in parse_options_step()
583 return usage_with_options_internal(usagestr, options, 1, ctx); in parse_options_step()
585 return usage_with_options_internal(usagestr, options, 0, ctx); in parse_options_step()
590 switch (parse_long_opt(ctx, arg, options)) { in parse_options_step()
603 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN)) in parse_options_step()
605 ctx->out[ctx->cpidx++] = ctx->argv[0]; in parse_options_step()
606 ctx->opt = NULL; in parse_options_step()
612 if ((excl_short_opt && ctx->excl_opt->short_name) || in parse_options_step()
613 ctx->excl_opt->long_name == NULL) { in parse_options_step()
614 char opt = ctx->excl_opt->short_name; in parse_options_step()
617 parse_options_usage(NULL, options, ctx->excl_opt->long_name, 0); in parse_options_step()
622 static int parse_options_end(struct parse_opt_ctx_t *ctx) in parse_options_end() argument
624 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out)); in parse_options_end()
625 ctx->out[ctx->cpidx + ctx->argc] = NULL; in parse_options_end()
626 return ctx->cpidx + ctx->argc; in parse_options_end()
632 struct parse_opt_ctx_t ctx; in parse_options_subcommand() local
650 parse_options_start(&ctx, argc, argv, flags); in parse_options_subcommand()
651 switch (parse_options_step(&ctx, options, usagestr)) { in parse_options_subcommand()
672 if (ctx.argv[0][1] == '-') in parse_options_subcommand()
674 ctx.argv[0] + 2); in parse_options_subcommand()
676 astrcatf(&error_buf, "unknown switch `%c'", *ctx.opt); in parse_options_subcommand()
680 return parse_options_end(&ctx); in parse_options_subcommand()
824 static bool option__in_argv(const struct option *opt, const struct parse_opt_ctx_t *ctx) in option__in_argv() argument
828 for (i = 1; i < ctx->argc; ++i) { in option__in_argv()
829 const char *arg = ctx->argv[i]; in option__in_argv()
857 struct parse_opt_ctx_t *ctx) in usage_with_options_internal() argument
889 if (ctx && ctx->argc > 1 && !option__in_argv(opts, ctx)) in usage_with_options_internal()