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

[PATCH 2/4] parseopt: add PARSE_OPT_NO_INTERNAL_HELP

From
René Scharfe <rene.scharfe@lsrfire.ath.cx>
Date
Mar 8, 2009, 18:15 UTC
Message-ID
<49B40B2C.1070008@lsrfire.ath.cx>
In-Reply-To
<49B2C784.90800@lsrfire.ath.cx>

Add a parseopt flag, PARSE_OPT_NO_INTERNAL_HELP, that turns off internal handling of -h, --help and --help-all. This allows the implementation of custom help option handlers or incremental parsers.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
---
 parse-options.c |   10 ++++++----
 parse-options.h |    1 +
 2 files changed, 7 insertions(+), 4 deletions(-)
diff --git a/parse-options.c b/parse-options.c
index 39808ae..8b21dea 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -253,6 +253,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
 		       const struct option *options,
 		       const char * const usagestr[])
 {
+	int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
+
 	/* we must reset ->opt, unknown short option leave it dangling */
 	ctx->opt = NULL;
 
@@ -268,7 +270,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
 
 		if (arg[1] != '-') {
 			ctx->opt = arg + 1;
-			if (*ctx->opt == 'h')
+			if (internal_help && *ctx->opt == 'h')
 				return parse_options_usage(usagestr, options);
 			switch (parse_short_opt(ctx, options)) {
 			case -1:
@@ -279,7 +281,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
 			if (ctx->opt)
 				check_typos(arg + 1, options);
 			while (ctx->opt) {
-				if (*ctx->opt == 'h')
+				if (internal_help && *ctx->opt == 'h')
 					return parse_options_usage(usagestr, options);
 				switch (parse_short_opt(ctx, options)) {
 				case -1:
@@ -306,9 +308,9 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
 			break;
 		}
 
-		if (!strcmp(arg + 2, "help-all"))
+		if (internal_help && !strcmp(arg + 2, "help-all"))
 			return usage_with_options_internal(usagestr, options, 1);
-		if (!strcmp(arg + 2, "help"))
+		if (internal_help && !strcmp(arg + 2, "help"))
 			return parse_options_usage(usagestr, options);
 		switch (parse_long_opt(ctx, arg + 2, options)) {
 		case -1:
diff --git a/parse-options.h b/parse-options.h
index b7d08b1..f8ef1db 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -22,6 +22,7 @@ enum parse_opt_flags {
 	PARSE_OPT_STOP_AT_NON_OPTION = 2,
 	PARSE_OPT_KEEP_ARGV0 = 4,
 	PARSE_OPT_KEEP_UNKNOWN = 8,
+	PARSE_OPT_NO_INTERNAL_HELP = 16,
 };
 
 enum parse_opt_option_flags {
-- 
1.6.2
Previous: René ScharfeNext: René Scharfe
Message 8 of 13 in “What's in git.git (Mar 2009, #02; Thu, 05)”
  1. Junio C HamanoMar 5, 2009
  2. René ScharfeMar 7, 2009
  3. 1/4 parseopt: add PARSE_OPT_KEEP_UNKNOWNRené Scharfe, Mar 8, 2009
  4. Junio C HamanoMar 8, 2009
  5. Junio C HamanoMar 8, 2009
  6. 5/4 parseopt: document KEEP_ARGV0, KEEP_UNKNOWN, NO_INTERNAL_HELPRené Scharfe, Mar 9, 2009
  7. 6/4 parseopt: prevent KEEP_UNKNOWN and STOP_AT_NON_OPTION from being used togetherRené Scharfe, Mar 9, 2009
  8. 2/4 parseopt: add PARSE_OPT_NO_INTERNAL_HELPRené Scharfe, Mar 8, 2009
  9. 3/4 parseopt: make usage optionalRené Scharfe, Mar 8, 2009
  10. Junio C HamanoMar 8, 2009
  11. René ScharfeMar 9, 2009
  12. 4/4 archive: use parseopt for local-only optionsRené Scharfe, Mar 8, 2009
  13. Junio C HamanoMar 8, 2009

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.