parse-options: deduplicate parse_options_usage() calls
Avoid long lines and repeating parse_options_usage() calls with their duplicate parameters by providing labels with speaking names to jump to. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Jeff King <peff@peff.net>
René Scharfe committed
Nov 17, 2015 at 11:25 UTC
ac20ff6daa818b3dad99368dfbc123dcf81e6824
1 file changed
+12
-6
parse-options.c
+12
-6
@@ -435,6 +435,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
435
const char * const usagestr[])
436
{
437
int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
438
+ int err = 0;
439
440
/* we must reset ->opt, unknown short option leave it dangling */
441
ctx->opt = NULL;
@@ -454,10 +455,10 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
455
if (arg[1] != '-') {
456
ctx->opt = arg + 1;
457
if (internal_help && *ctx->opt == 'h')
457
- return parse_options_usage(ctx, usagestr, options, 0);
458
+ goto show_usage;
459
switch (parse_short_opt(ctx, options)) {
460
case -1:
460
- return parse_options_usage(ctx, usagestr, options, 1);
461
+ goto show_usage_error;
462
case -2:
463
if (ctx->opt)
464
check_typos(arg + 1, options);
@@ -467,10 +468,10 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
468
check_typos(arg + 1, options);
469
while (ctx->opt) {
470
if (internal_help && *ctx->opt == 'h')
470
- return parse_options_usage(ctx, usagestr, options, 0);
471
+ goto show_usage;
472
switch (parse_short_opt(ctx, options)) {
473
case -1:
473
- return parse_options_usage(ctx, usagestr, options, 1);
474
+ goto show_usage_error;
475
case -2:
476
/* fake a short option thing to hide the fact that we may have
477
* started to parse aggregated stuff
@@ -496,10 +497,10 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
497
if (internal_help && !strcmp(arg + 2, "help-all"))
498
return usage_with_options_internal(ctx, usagestr, options, 1, 0);
499
if (internal_help && !strcmp(arg + 2, "help"))
499
- return parse_options_usage(ctx, usagestr, options, 0);
500
+ goto show_usage;
501
switch (parse_long_opt(ctx, arg + 2, options)) {
502
case -1:
502
- return parse_options_usage(ctx, usagestr, options, 1);
503
+ goto show_usage_error;
504
case -2:
505
goto unknown;
506
}
@@ -511,6 +512,11 @@ unknown:
512
ctx->opt = NULL;
513
}
514
return PARSE_OPT_DONE;
515
+
516
+ show_usage_error:
517
+ err = 1;
518
+ show_usage:
519
+ return parse_options_usage(ctx, usagestr, options, err);
520
}
521
522
int parse_options_end(struct parse_opt_ctx_t *ctx)