git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/5] parse-options: deduplicate parse_options_usage() calls

From
René Scharfe <l.s.r@web.de>
Date
Nov 17, 2015, 10:25 UTC
Message-ID
<564B008A.9000102@web.de>
In-Reply-To
<564AFF32.1030406@web.de>

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>
---
 parse-options.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
diff --git a/parse-options.c b/parse-options.c
index 3eceba4..f0b6d9b 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -435,6 +435,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
 		       const char * const usagestr[])
 {
 	int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
+	int err = 0;
 
 	/* we must reset ->opt, unknown short option leave it dangling */
 	ctx->opt = NULL;
@@ -454,10 +455,10 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
 		if (arg[1] != '-') {
 			ctx->opt = arg + 1;
 			if (internal_help && *ctx->opt == 'h')
-				return parse_options_usage(ctx, usagestr, options, 0);
+				goto show_usage;
 			switch (parse_short_opt(ctx, options)) {
 			case -1:
-				return parse_options_usage(ctx, usagestr, options, 1);
+				goto show_usage_error;
 			case -2:
 				if (ctx->opt)
 					check_typos(arg + 1, options);
@@ -467,10 +468,10 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
 				check_typos(arg + 1, options);
 			while (ctx->opt) {
 				if (internal_help && *ctx->opt == 'h')
-					return parse_options_usage(ctx, usagestr, options, 0);
+					goto show_usage;
 				switch (parse_short_opt(ctx, options)) {
 				case -1:
-					return parse_options_usage(ctx, usagestr, options, 1);
+					goto show_usage_error;
 				case -2:
 					/* fake a short option thing to hide the fact that we may have
 					 * started to parse aggregated stuff
@@ -496,10 +497,10 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
 		if (internal_help && !strcmp(arg + 2, "help-all"))
 			return usage_with_options_internal(ctx, usagestr, options, 1, 0);
 		if (internal_help && !strcmp(arg + 2, "help"))
-			return parse_options_usage(ctx, usagestr, options, 0);
+			goto show_usage;
 		switch (parse_long_opt(ctx, arg + 2, options)) {
 		case -1:
-			return parse_options_usage(ctx, usagestr, options, 1);
+			goto show_usage_error;
 		case -2:
 			goto unknown;
 		}
@@ -511,6 +512,11 @@ unknown:
 		ctx->opt = NULL;
 	}
 	return PARSE_OPT_DONE;
+
+ show_usage_error:
+	err = 1;
+ show_usage:
+	return parse_options_usage(ctx, usagestr, options, err);
 }
 
 int parse_options_end(struct parse_opt_ctx_t *ctx)
-- 
2.6.3
Previous: René ScharfeNext: René Scharfe
Message 2 of 7 in “parse-options: allow -h as a short option”
  1. 0/5 parse-options: allow -h as a short optionRené Scharfe, Nov 17, 2015
  2. 1/5 parse-options: deduplicate parse_options_usage() callsRené Scharfe, Nov 17, 2015
  3. 2/5 parse-options: inline parse_options_usage() at its only remaining callerRené Scharfe, Nov 17, 2015
  4. 3/5 parse-options: allow -h as a short optionRené Scharfe, Nov 17, 2015
  5. 4/5 grep: stop using PARSE_OPT_NO_INTERNAL_HELPRené Scharfe, Nov 17, 2015
  6. 5/5 show-ref: stop using PARSE_OPT_NO_INTERNAL_HELPRené Scharfe, Nov 17, 2015
  7. Junio C HamanoDec 4, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.