Skip to content

Commit 352e761

Browse files
avargitster
authored andcommitted
parse-options.[ch]: consistently use "enum parse_opt_result"
Use the "enum parse_opt_result" instead of an "int flags" as the return value of the applicable functions in parse-options.c. This will help catch future bugs, such as the missing "case" arms in the two existing users of the API in "blame.c" and "shortlog.c". A third caller in 309be81 (update-index: migrate to parse-options API, 2010-12-01) was already checking for these. As can be seen when trying to sort through the deluge of warnings produced when compiling this with CC=g++ (mostly unrelated to this change) we're not consistently using "enum parse_opt_result" even now, i.e. we'll return error() and "return 0;". See f41179f (parse-options: avoid magic return codes, 2019-01-27) for a commit which started changing some of that. I'm not doing any more of that exhaustive migration here, and it's probably not worthwhile past the point of being able to check "enum parse_opt_result" in switch(). Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3f9ab7c commit 352e761

File tree

4 files changed

+31
-21
lines changed

4 files changed

+31
-21
lines changed

builtin/blame.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,9 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
917917
PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
918918
for (;;) {
919919
switch (parse_options_step(&ctx, options, blame_opt_usage)) {
920+
case PARSE_OPT_NON_OPTION:
921+
case PARSE_OPT_UNKNOWN:
922+
break;
920923
case PARSE_OPT_HELP:
921924
case PARSE_OPT_ERROR:
922925
exit(129);

builtin/shortlog.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
374374

375375
for (;;) {
376376
switch (parse_options_step(&ctx, options, shortlog_usage)) {
377+
case PARSE_OPT_NON_OPTION:
378+
case PARSE_OPT_UNKNOWN:
379+
break;
377380
case PARSE_OPT_HELP:
378381
case PARSE_OPT_ERROR:
379382
exit(129);

parse-options.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -699,13 +699,14 @@ static void free_preprocessed_options(struct option *options)
699699
free(options);
700700
}
701701

702-
static int usage_with_options_internal(struct parse_opt_ctx_t *,
703-
const char * const *,
704-
const struct option *, int, int);
705-
706-
int parse_options_step(struct parse_opt_ctx_t *ctx,
707-
const struct option *options,
708-
const char * const usagestr[])
702+
static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t *,
703+
const char * const *,
704+
const struct option *,
705+
int, int);
706+
707+
enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
708+
const struct option *options,
709+
const char * const usagestr[])
709710
{
710711
int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
711712

@@ -839,10 +840,11 @@ int parse_options_end(struct parse_opt_ctx_t *ctx)
839840
return ctx->cpidx + ctx->argc;
840841
}
841842

842-
int parse_options(int argc, const char **argv, const char *prefix,
843-
const struct option *options,
844-
const char * const usagestr[],
845-
enum parse_opt_flags flags)
843+
enum parse_opt_result parse_options(int argc, const char **argv,
844+
const char *prefix,
845+
const struct option *options,
846+
const char * const usagestr[],
847+
enum parse_opt_flags flags)
846848
{
847849
struct parse_opt_ctx_t ctx;
848850
struct option *real_options;
@@ -900,9 +902,10 @@ static int usage_argh(const struct option *opts, FILE *outfile)
900902
#define USAGE_OPTS_WIDTH 24
901903
#define USAGE_GAP 2
902904

903-
static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
904-
const char * const *usagestr,
905-
const struct option *opts, int full, int err)
905+
static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t *ctx,
906+
const char * const *usagestr,
907+
const struct option *opts,
908+
int full, int err)
906909
{
907910
FILE *outfile = err ? stderr : stdout;
908911
int need_newline;

parse-options.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,11 @@ struct option {
211211
* untouched and parse_options() returns the number of options
212212
* processed.
213213
*/
214-
int parse_options(int argc, const char **argv, const char *prefix,
215-
const struct option *options,
216-
const char * const usagestr[],
217-
enum parse_opt_flags flags);
214+
enum parse_opt_result parse_options(int argc, const char **argv,
215+
const char *prefix,
216+
const struct option *options,
217+
const char * const usagestr[],
218+
enum parse_opt_flags flags);
218219

219220
NORETURN void usage_with_options(const char * const *usagestr,
220221
const struct option *options);
@@ -274,9 +275,9 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
274275
const struct option *options,
275276
enum parse_opt_flags flags);
276277

277-
int parse_options_step(struct parse_opt_ctx_t *ctx,
278-
const struct option *options,
279-
const char * const usagestr[]);
278+
enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
279+
const struct option *options,
280+
const char * const usagestr[]);
280281

281282
int parse_options_end(struct parse_opt_ctx_t *ctx);
282283

0 commit comments

Comments
 (0)