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

[PATCH 1/6] parse-options: add precision handling for PARSE_OPT_CMDMODE

From
René Scharfe <l.s.r@web.de>
Date
Jun 29, 2025, 11:50 UTC
Message-ID
<d92697a6-5367-4412-88db-164ba4dd6230@web.de>
In-Reply-To
<cf5cd57d-733f-4239-80f8-23bdc1523ab2@web.de>

Build on 09705696f7 (parse-options: introduce precision handling for `OPTION_INTEGER`, 2025-04-17) to support value variables of different sizes for PARSE_OPT_CMDMODE options. Do that by requiring their "precision" to be set and casting their "value" pointer accordingly.

get_value() needs to access all PARSE_OPT_CMDMODE values in addition to the actual value it is supposed to get to detect conflicting changes. Give it an example struct option pointer in cmdmode_list instead of just the "value" pointer to allow it to use the proper "precision".

Use optbug() in get_int_value() to report options with unsupported "precision" values without requiring enum opt_parsed flags, as we don't have them in build_cmdmode_list(). Use BUG right afterwards to abort for uses outside of build_cmdmode_list() by aborting immediately.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 builtin/am.c                  |  1 +
 parse-options.c               | 36 ++++++++++++++++++++++++++---------
 parse-options.h               |  1 +
 t/helper/test-parse-options.c | 13 ++++++++++---
 4 files changed, 39 insertions(+), 12 deletions(-)
diff --git a/builtin/am.c b/builtin/am.c
index a800003340..c9d925f7b9 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -2406,6 +2406,7 @@ int cmd_am(int argc,
 			.type = OPTION_CALLBACK,
 			.long_name = "show-current-patch",
 			.value = &resume_mode,
+			.precision = sizeof(resume_mode),
 			.argh = "(diff|raw)",
 			.help = N_("show the patch being applied"),
 			.flags = PARSE_OPT_CMDMODE | PARSE_OPT_OPTARG | PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
diff --git a/parse-options.c b/parse-options.c
index a9a39ecaef..da07a000a3 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -68,6 +68,23 @@ static char *fix_filename(const char *prefix, const char *file)
 		return prefix_filename_except_for_dash(prefix, file);
 }
 
+static intmax_t get_int_value(const struct option *opt)
+{
+	switch (opt->precision) {
+	case sizeof(int8_t):
+		return *(int8_t *)opt->value;
+	case sizeof(int16_t):
+		return *(int16_t *)opt->value;
+	case sizeof(int32_t):
+		return *(int32_t *)opt->value;
+	case sizeof(int64_t):
+		return *(int64_t *)opt->value;
+	default:
+		optbug(opt, "has invalid precision");
+		BUG("invalid 'struct option'");
+	}
+}
+
 static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p,
 					  const struct option *opt,
 					  enum opt_parsed flags,
@@ -266,8 +283,8 @@ static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p,
 }
 
 struct parse_opt_cmdmode_list {
-	int value, *value_ptr;
-	const struct option *opt;
+	intmax_t value;
+	const struct option *opt, *reference_opt;
 	const char *arg;
 	enum opt_parsed flags;
 	struct parse_opt_cmdmode_list *next;
@@ -280,19 +297,18 @@ static void build_cmdmode_list(struct parse_opt_ctx_t *ctx,
 
 	for (; opts->type != OPTION_END; opts++) {
 		struct parse_opt_cmdmode_list *elem = ctx->cmdmode_list;
-		int *value_ptr = opts->value;
 
-		if (!(opts->flags & PARSE_OPT_CMDMODE) || !value_ptr)
+		if (!(opts->flags & PARSE_OPT_CMDMODE) || !opts->value)
 			continue;
 
-		while (elem && elem->value_ptr != value_ptr)
+		while (elem && elem->reference_opt->value != opts->value)
 			elem = elem->next;
 		if (elem)
 			continue;
 
 		CALLOC_ARRAY(elem, 1);
-		elem->value_ptr = value_ptr;
-		elem->value = *value_ptr;
+		elem->reference_opt = opts;
+		elem->value = get_int_value(opts);
 		elem->next = ctx->cmdmode_list;
 		ctx->cmdmode_list = elem;
 	}
@@ -317,7 +333,9 @@ static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
 	char *opt_name, *other_opt_name;
 
 	for (; elem; elem = elem->next) {
-		if (*elem->value_ptr == elem->value)
+		intmax_t new_value = get_int_value(elem->reference_opt);
+
+		if (new_value == elem->value)
 			continue;
 
 		if (elem->opt &&
@@ -327,7 +345,7 @@ static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
 		elem->opt = opt;
 		elem->arg = arg;
 		elem->flags = flags;
-		elem->value = *elem->value_ptr;
+		elem->value = new_value;
 	}
 
 	if (result || !elem)
diff --git a/parse-options.h b/parse-options.h
index 91c3e3c29b..c75a473c9e 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -269,6 +269,7 @@ struct option {
 	.short_name = (s), \
 	.long_name = (l), \
 	.value = (v), \
+	.precision = sizeof(*v), \
 	.help = (h), \
 	.flags = PARSE_OPT_CMDMODE|PARSE_OPT_NOARG|PARSE_OPT_NONEG | (f), \
 	.defval = (i), \
diff --git a/t/helper/test-parse-options.c b/t/helper/test-parse-options.c
index f2663dd0c0..1e03ff88f6 100644
--- a/t/helper/test-parse-options.c
+++ b/t/helper/test-parse-options.c
@@ -148,9 +148,16 @@ int cmd__parse_options(int argc, const char **argv)
 		OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
 		OPT_CMDMODE(0, "mode1", &integer, "set integer to 1 (cmdmode option)", 1),
 		OPT_CMDMODE(0, "mode2", &integer, "set integer to 2 (cmdmode option)", 2),
-		OPT_CALLBACK_F(0, "mode34", &integer, "(3|4)",
-			"set integer to 3 or 4 (cmdmode option)",
-			PARSE_OPT_CMDMODE, mode34_callback),
+		{
+			.type = OPTION_CALLBACK,
+			.long_name = "mode34",
+			.value = &integer,
+			.precision = sizeof(integer),
+			.argh = "(3|4)",
+			.help = "set integer to 3 or 4 (cmdmode option)",
+			.flags = PARSE_OPT_CMDMODE,
+			.callback = mode34_callback,
+		},
 		OPT_CALLBACK('L', "length", &integer, "str",
 			"get length of <str>", length_callback),
 		OPT_FILENAME('F', "file", &file, "set file to <file>"),
-- 
2.50.0
Previous: René ScharfeNext: Patrick Steinhardt
Message 2 of 31 in “parse-options: add more precision handling”
  1. 0/6 parse-options: add more precision handlingRené Scharfe, Jun 29, 2025
  2. 1/6 parse-options: add precision handling for PARSE_OPT_CMDMODERené Scharfe, Jun 29, 2025
  3. Patrick SteinhardtJul 1, 2025
  4. René ScharfeJul 1, 2025
  5. 2/6 parse-options: add precision handling for OPTION_SET_INTRené Scharfe, Jun 29, 2025
  6. Patrick SteinhardtJul 1, 2025
  7. René ScharfeJul 1, 2025
  8. Patrick SteinhardtJul 2, 2025
  9. 3/6 parse-options: add precision handling for OPTION_BITRené Scharfe, Jun 29, 2025
  10. 4/6 parse-options: add precision handling for OPTION_NEGBITRené Scharfe, Jun 29, 2025
  11. 5/6 parse-options: add precision handling for OPTION_BITOPRené Scharfe, Jun 29, 2025
  12. Patrick SteinhardtJul 1, 2025
  13. René ScharfeJul 1, 2025
  14. Patrick SteinhardtJul 2, 2025
  15. 6/6 parse-options: add precision handling for OPTION_COUNTUPRené Scharfe, Jun 29, 2025
  16. Patrick SteinhardtJul 1, 2025
  17. René ScharfeJul 1, 2025
  18. Patrick SteinhardtJul 2, 2025
  19. 0/7 parse-options: add more precision handlingRené Scharfe, Jul 9, 2025
  20. 1/7 parse-options: require PARSE_OPT_NOARG for OPTION_BITOPRené Scharfe, Jul 9, 2025
  21. Patrick SteinhardtJul 9, 2025
  22. 2/7 parse-options: add precision handling for PARSE_OPT_CMDMODERené Scharfe, Jul 9, 2025
  23. Patrick SteinhardtJul 9, 2025
  24. René ScharfeJul 9, 2025
  25. Patrick SteinhardtJul 9, 2025
  26. Junio C HamanoJul 9, 2025
  27. 3/7 parse-options: add precision handling for OPTION_SET_INTRené Scharfe, Jul 9, 2025
  28. 4/7 parse-options: add precision handling for OPTION_BITRené Scharfe, Jul 9, 2025
  29. 5/7 parse-options: add precision handling for OPTION_NEGBITRené Scharfe, Jul 9, 2025
  30. 6/7 parse-options: add precision handling for OPTION_BITOPRené Scharfe, Jul 9, 2025
  31. 7/7 parse-options: add precision handling for OPTION_COUNTUPRené Scharfe, Jul 9, 2025

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.