Skip to content

Commit

Permalink
parse-options.c: add a parse_options_check_flags()
Browse files Browse the repository at this point in the history
Add a parse_options_check_flags() for those checks that need to check
the "enum parse_opt_flags", or the "flags" and "opts" in combination.

This changes no behavior, and just moves existing code around, but in
a subsequent commit we'll start checking the per-option flags against
the parse_options() flags, this change makes that subsequent behavior
change smaller.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
  • Loading branch information
avar committed Jul 25, 2022
1 parent 8eeee58 commit 55dda82
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions parse-options.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,18 @@ static void check_typos(const char *arg, const struct option *options)
}
}

static void parse_options_check_flags(const struct option *opts,
const enum parse_opt_flags flags)
{
if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
(flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
!(flags & PARSE_OPT_ONE_SHOT))
BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
if ((flags & PARSE_OPT_ONE_SHOT) &&
(flags & PARSE_OPT_KEEP_ARGV0))
BUG("Can't keep argv0 if you don't have it");
}

static void parse_options_check(const struct option *opts)
{
char short_opts[128];
Expand Down Expand Up @@ -530,13 +542,7 @@ static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
ctx->prefix = prefix;
ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
ctx->flags = flags;
if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
(flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
!(flags & PARSE_OPT_ONE_SHOT))
BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
if ((flags & PARSE_OPT_ONE_SHOT) &&
(flags & PARSE_OPT_KEEP_ARGV0))
BUG("Can't keep argv0 if you don't have it");
parse_options_check_flags(options, flags);
parse_options_check(options);
}

Expand Down

0 comments on commit 55dda82

Please sign in to comment.