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

Re: [PATCH 1/2] parse-options: add int value pointer to struct option

From
Taylor Blau <me@ttaylorr.com>
Date
Sep 10, 2023, 18:40 UTC
Message-ID
<ZP4NrVeqMtFTLEuf@nand.local>
In-Reply-To
<2d6f3d74-687a-2d40-5c0c-abc396aef80f@web.de>
On Sat, Sep 09, 2023 at 11:10:36PM +0200, René Scharfe wrote:
> Add an int pointer, value_int, to struct option to provide a typed value
> pointer for the various integer options.  It allows type checks at
> compile time, which is not possible with the void pointer, value.  Its
> use is optional for now.

This is an interesting direction. I wonder about whether or not you'd consider changing the option structure to contain a tagged union type that represents some common cases we'd want from a parse-options callback, something like:

    struct option {
        /* ... */
        union {
            void *value;
            int *value_int;
            /* etc ... */
        } u;
        enum option_type t;
    };

where option_type has some value corresponding to "void *", another for "int *", and so on.

Alternatively, perhaps you are thinking that we'd use both the value pointer and the value_int pointer to point at potentially different values in the same callback. I don't have strong feelings about it, but I'd just as soon encourage us to shy away from that approach, since assigning a single callback parameter to each function seems more organized.

Show 8 quoted lines
> @@ -109,6 +110,7 @@ static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
>  	const char *s, *arg;
>  	const int unset = flags & OPT_UNSET;
>  	int err;
> +	int *value_int = opt->value_int ? opt->value_int : opt->value;
>
>  	if (unset && p->opt)
>  		return error(_("%s takes no value"), optname(opt, flags));

Reading this hunk, I wonder whether we even need a type tag (the option_type enum above) if each callback knows a priori what type it expects. But I think storing them together in a union makes sense to do.

Thanks, Taylor

Previous: Oswald BuddenhagenNext: René Scharfe
Message 28 of 36 in “parse-options: add int value pointer to struct option”
  1. 1/2 parse-options: add int value pointer to struct optionRené Scharfe, Sep 9, 2023
  2. 2/2 parse-options: use and require int pointer for OPT_CMDMODERené Scharfe, Sep 9, 2023
  3. Oswald BuddenhagenSep 10, 2023
  4. René ScharfeSep 11, 2023
  5. Jeff KingSep 12, 2023
  6. Junio C HamanoSep 16, 2023
  7. René ScharfeSep 18, 2023
  8. Oswald BuddenhagenSep 18, 2023
  9. René ScharfeSep 19, 2023
  10. am: fix error message in parse_opt_show_current_patch()Oswald Buddenhagen, Sep 21, 2023
  11. Junio C HamanoSep 21, 2023
  12. Oswald BuddenhagenSep 21, 2023
  13. Phillip WoodSep 18, 2023
  14. Junio C HamanoSep 18, 2023
  15. Phillip WoodSep 18, 2023
  16. René ScharfeOct 3, 2023
  17. Junio C HamanoOct 3, 2023
  18. René ScharfeSep 19, 2023
  19. Junio C HamanoSep 11, 2023
  20. René ScharfeSep 11, 2023
  21. Oswald BuddenhagenSep 19, 2023
  22. René ScharfeSep 20, 2023
  23. Oswald BuddenhagenSep 21, 2023
  24. René ScharfeOct 3, 2023
  25. Oswald BuddenhagenOct 3, 2023
  26. René ScharfeOct 3, 2023
  27. Oswald BuddenhagenOct 3, 2023
  28. Taylor BlauSep 10, 2023
  29. René ScharfeSep 11, 2023
  30. Junio C HamanoSep 11, 2023
  31. Oswald BuddenhagenSep 11, 2023
  32. Kristoffer HaugsbakkSep 18, 2023
  33. René ScharfeSep 18, 2023
  34. Oswald BuddenhagenSep 18, 2023
  35. Junio C HamanoSep 18, 2023
  36. René ScharfeSep 20, 2023

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.