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

[PATCH] parse-opt: create parse_options_step.

From
Pierre Habouzit <madcoder@debian.org>
Date
Jun 23, 2008, 21:11 UTC
Message-ID
<1214255482-2086-3-git-send-email-madcoder@debian.org>
In-Reply-To
<1214255482-2086-2-git-send-email-madcoder@debian.org>

For now it's unable to stop at unknown options, this commit merely reorganize some code around.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
 parse-options.c |   89 +++++++++++++++++++++++++++++++-----------------------
 parse-options.h |    4 ++
 2 files changed, 55 insertions(+), 38 deletions(-)
diff --git a/parse-options.c b/parse-options.c
index 4e9e675..71b3476 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -250,64 +250,79 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
 	ctx->flags = flags;
 }
 
-int parse_options_end(struct parse_opt_ctx_t *ctx)
-{
-	memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
-	ctx->out[ctx->cpidx + ctx->argc] = NULL;
-	return ctx->cpidx + ctx->argc;
-}
-
 static int usage_with_options_internal(const char * const *,
-                                       const struct option *, int, int);
+                                       const struct option *, int);
 
-int parse_options(int argc, const char **argv, const struct option *options,
-                  const char * const usagestr[], int flags)
+int parse_options_step(struct parse_opt_ctx_t *ctx,
+                       const struct option *options,
+                       const char * const usagestr[])
 {
-	struct parse_opt_ctx_t ctx;
-
-	parse_options_start(&ctx, argc, argv, flags);
-	for (; ctx.argc; ctx.argc--, ctx.argv++) {
-		const char *arg = ctx.argv[0];
+	for (; ctx->argc; ctx->argc--, ctx->argv++) {
+		const char *arg = ctx->argv[0];
 
 		if (*arg != '-' || !arg[1]) {
-			if (ctx.flags & PARSE_OPT_STOP_AT_NON_OPTION)
+			if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
 				break;
-			ctx.out[ctx.cpidx++] = ctx.argv[0];
+			ctx->out[ctx->cpidx++] = ctx->argv[0];
 			continue;
 		}
 
 		if (arg[1] != '-') {
-			ctx.opt = arg + 1;
-			if (*ctx.opt == 'h')
-				usage_with_options(usagestr, options);
-			if (parse_short_opt(&ctx, options) < 0)
+			ctx->opt = arg + 1;
+			if (*ctx->opt == 'h')
+				return parse_options_usage(usagestr, options);
+			if (parse_short_opt(ctx, options) < 0)
 				usage_with_options(usagestr, options);
-			if (ctx.opt)
+			if (ctx->opt)
 				check_typos(arg + 1, options);
-			while (ctx.opt) {
-				if (*ctx.opt == 'h')
-					usage_with_options(usagestr, options);
-				if (parse_short_opt(&ctx, options) < 0)
+			while (ctx->opt) {
+				if (*ctx->opt == 'h')
+					return parse_options_usage(usagestr, options);
+				if (parse_short_opt(ctx, options) < 0)
 					usage_with_options(usagestr, options);
 			}
 			continue;
 		}
 
 		if (!arg[2]) { /* "--" */
-			if (!(ctx.flags & PARSE_OPT_KEEP_DASHDASH)) {
-				ctx.argc--;
-				ctx.argv++;
+			if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
+				ctx->argc--;
+				ctx->argv++;
 			}
 			break;
 		}
 
 		if (!strcmp(arg + 2, "help-all"))
-			usage_with_options_internal(usagestr, options, 1, 1);
+			return usage_with_options_internal(usagestr, options, 1);
 		if (!strcmp(arg + 2, "help"))
-			usage_with_options(usagestr, options);
-		if (parse_long_opt(&ctx, arg + 2, options))
+			return parse_options_usage(usagestr, options);
+		if (parse_long_opt(ctx, arg + 2, options))
 			usage_with_options(usagestr, options);
 	}
+	return PARSE_OPT_DONE;
+}
+
+int parse_options_end(struct parse_opt_ctx_t *ctx)
+{
+	memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
+	ctx->out[ctx->cpidx + ctx->argc] = NULL;
+	return ctx->cpidx + ctx->argc;
+}
+
+int parse_options(int argc, const char **argv, const struct option *options,
+                  const char * const usagestr[], int flags)
+{
+	struct parse_opt_ctx_t ctx;
+
+	parse_options_start(&ctx, argc, argv, flags);
+	switch (parse_options_step(&ctx, options, usagestr)) {
+	case PARSE_OPT_HELP:
+		exit(129);
+	case PARSE_OPT_DONE:
+		break;
+	default: /* PARSE_OPT_UNKNOWN */
+		abort(); /* unreached yet */
+	}
 
 	return parse_options_end(&ctx);
 }
@@ -316,7 +331,7 @@ int parse_options(int argc, const char **argv, const struct option *options,
 #define USAGE_GAP         2
 
 int usage_with_options_internal(const char * const *usagestr,
-                                const struct option *opts, int full, int do_exit)
+                                const struct option *opts, int full)
 {
 	fprintf(stderr, "usage: %s\n", *usagestr++);
 	while (*usagestr && **usagestr)
@@ -401,22 +416,20 @@ int usage_with_options_internal(const char * const *usagestr,
 	}
 	fputc('\n', stderr);
 
-	if (do_exit)
-		exit(129);
 	return PARSE_OPT_HELP;
 }
 
 void usage_with_options(const char * const *usagestr,
                         const struct option *opts)
 {
-	usage_with_options_internal(usagestr, opts, 0, 1);
-	exit(129); /* make gcc happy */
+	usage_with_options_internal(usagestr, opts, 0);
+	exit(129);
 }
 
 int parse_options_usage(const char * const *usagestr,
                         const struct option *opts)
 {
-	return usage_with_options_internal(usagestr, opts, 0, 0);
+	return usage_with_options_internal(usagestr, opts, 0);
 }
 
 
diff --git a/parse-options.h b/parse-options.h
index 424ea46..9da5e8c 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -133,6 +133,10 @@ extern int parse_options_usage(const char * const *usagestr,
 extern void parse_options_start(struct parse_opt_ctx_t *ctx,
                                 int argc, const char **argv, int flags);
 
+extern int parse_options_step(struct parse_opt_ctx_t *ctx,
+                              const struct option *options,
+                              const char * const usagestr[]);
+
 extern int parse_options_end(struct parse_opt_ctx_t *ctx);
 
 
-- 
1.5.6.117.g5fe2
Previous: Pierre HabouzitNext: Pierre Habouzit
Message 22 of 82 in “Convert 'git blame' to parse_options()”
  1. Linus TorvaldsJun 23, 2008
  2. Junio C HamanoJun 23, 2008
  3. Johannes SchindelinJun 23, 2008
  4. [RFC] Re: Convert 'git blame' to parse_options()Pierre Habouzit, Jun 23, 2008
  5. Johannes SchindelinJun 23, 2008
  6. Pierre HabouzitJun 23, 2008
  7. Johannes SchindelinJun 23, 2008
  8. Linus TorvaldsJun 23, 2008
  9. Jeff KingJun 23, 2008
  10. Linus TorvaldsJun 23, 2008
  11. Jeff KingJun 23, 2008
  12. Linus TorvaldsJun 23, 2008
  13. Jeff KingJun 23, 2008
  14. Linus TorvaldsJun 23, 2008
  15. Linus TorvaldsJun 23, 2008
  16. Jeff KingJun 23, 2008
  17. Linus TorvaldsJun 23, 2008
  18. Linus TorvaldsJun 23, 2008
  19. Pierre HabouzitJun 23, 2008
  20. parse-opt: have parse_options_{start,end}.Pierre Habouzit, Jun 23, 2008
  21. parse-opt: Export a non NORETURN usage dumper.Pierre Habouzit, Jun 23, 2008
  22. parse-opt: create parse_options_step.Pierre Habouzit, Jun 23, 2008
  23. parse-opt: do not pring errors on unknown options, return -2 intead.Pierre Habouzit, Jun 23, 2008
  24. parse-opt: fake short strings for callers to believe in.Pierre Habouzit, Jun 23, 2008
  25. Junio C HamanoJun 23, 2008
  26. Pierre HabouzitJun 23, 2008
  27. Pierre HabouzitJun 23, 2008
  28. Junio C HamanoJun 23, 2008
  29. Pierre HabouzitJun 23, 2008
  30. Linus TorvaldsJun 23, 2008
  31. Linus TorvaldsJun 23, 2008
  32. Pierre HabouzitJun 23, 2008
  33. Junio C HamanoJun 23, 2008
  34. Pierre HabouzitJun 23, 2008
  35. Pierre HabouzitJun 23, 2008
  36. Junio C HamanoJun 23, 2008
  37. Pierre HabouzitJun 23, 2008
  38. Linus TorvaldsJun 23, 2008
  39. Junio C HamanoJun 23, 2008
  40. Pierre HabouzitJun 24, 2008
  41. Jeff KingJun 24, 2008
  42. Jeff KingJun 23, 2008
  43. Pierre HabouzitJun 23, 2008
  44. Linus TorvaldsJun 23, 2008
  45. Jeff KingJun 24, 2008
  46. Linus TorvaldsJun 24, 2008
  47. Johannes SchindelinJun 24, 2008
  48. Jeff KingJun 24, 2008
  49. Linus TorvaldsJun 24, 2008
  50. Jeff KingJun 24, 2008
  51. Junio C HamanoJun 24, 2008
  52. Pierre HabouzitJun 24, 2008
  53. Linus TorvaldsJun 24, 2008
  54. Pierre HabouzitJun 24, 2008
  55. Pierre HabouzitJun 24, 2008
  56. Johannes SixtJun 25, 2008
  57. Johannes SchindelinJun 23, 2008
  58. Linus TorvaldsJun 23, 2008
  59. Johannes SchindelinJun 23, 2008
  60. Jeff KingJun 23, 2008
  61. Johannes SchindelinJun 23, 2008
  62. Pierre HabouzitJun 23, 2008
  63. Linus TorvaldsJun 23, 2008
  64. Making parse-opt incremental, reworked seriesPierre Habouzit, Jun 24, 2008
  65. 1/7 parse-opt: have parse_options_{start,end}.Pierre Habouzit, Jun 24, 2008
  66. 2/7 parse-opt: Export a non NORETURN usage dumper.Pierre Habouzit, Jun 24, 2008
  67. 3/7 parse-opt: create parse_options_step.Pierre Habouzit, Jun 24, 2008
  68. 4/7 parse-opt: do not pring errors on unknown options, return -2 intead.Pierre Habouzit, Jun 24, 2008
  69. 5/7 parse-opt: fake short strings for callers to believe in.Pierre Habouzit, Jun 24, 2008
  70. 6/7 parse-opt: add PARSE_OPT_KEEP_ARGV0 parser option.Pierre Habouzit, Jun 24, 2008
  71. 7/7 Migrate git-blame to parse-option partially.Pierre Habouzit, Jun 24, 2008
  72. Pierre HabouzitJun 24, 2008
  73. Linus TorvaldsJun 24, 2008
  74. Pierre HabouzitJun 24, 2008
  75. Pierre HabouzitJun 24, 2008
  76. Linus TorvaldsJun 24, 2008
  77. Pierre HabouzitJun 24, 2008
  78. Andreas EricssonJun 25, 2008
  79. parse-opt: fake short strings for callers to believe in.Pierre Habouzit, Jun 24, 2008
  80. Pierre HabouzitJun 26, 2008
  81. Junio C HamanoJun 26, 2008
  82. Pierre HabouzitJun 26, 2008

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.