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 309be813c9b (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 f41179f16ba (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 <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Oct 8, 2021 at 21:07 UTC 352e761388b5fa41bf40e7c04edf3cb07d888d94
4 files changed +31 -21
builtin/blame.c
+3
@@ -917,6 +917,9 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
917 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
918 for (;;) {
919 switch (parse_options_step(&ctx, options, blame_opt_usage)) {
920 + case PARSE_OPT_NON_OPTION:
921 + case PARSE_OPT_UNKNOWN:
922 + break;
923 case PARSE_OPT_HELP:
924 case PARSE_OPT_ERROR:
925 exit(129);
builtin/shortlog.c
+3
@@ -374,6 +374,9 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
374
375 for (;;) {
376 switch (parse_options_step(&ctx, options, shortlog_usage)) {
377 + case PARSE_OPT_NON_OPTION:
378 + case PARSE_OPT_UNKNOWN:
379 + break;
380 case PARSE_OPT_HELP:
381 case PARSE_OPT_ERROR:
382 exit(129);
parse-options.c
+17 -14
@@ -699,13 +699,14 @@ static void free_preprocessed_options(struct option *options)
699 free(options);
700 }
701
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[])
710 {
711 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
712
@@ -839,10 +840,11 @@ int parse_options_end(struct parse_opt_ctx_t *ctx)
840 return ctx->cpidx + ctx->argc;
841 }
842
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)
848 {
849 struct parse_opt_ctx_t ctx;
850 struct option *real_options;
@@ -900,9 +902,10 @@ static int usage_argh(const struct option *opts, FILE *outfile)
902 #define USAGE_OPTS_WIDTH 24
903 #define USAGE_GAP 2
904
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)
909 {
910 FILE *outfile = err ? stderr : stdout;
911 int need_newline;
parse-options.h
+8 -7
@@ -211,10 +211,11 @@ struct option {
211 * untouched and parse_options() returns the number of options
212 * processed.
213 */
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);
219
220 NORETURN void usage_with_options(const char * const *usagestr,
221 const struct option *options);
@@ -274,9 +275,9 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
275 const struct option *options,
276 enum parse_opt_flags flags);
277
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[]);
281
282 int parse_options_end(struct parse_opt_ctx_t *ctx);
283