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

Re: [PATCH v3 3/4] parse-options: add early_scan_options()

From
Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
Date
Oct 30, 2026, 21:27 UTC
Message-ID
<6782fa63-1e46-4ae9-b129-a157986c2556@gmail.com>
In-Reply-To
<20261012074413.337183-4-christian.couder@gmail.com>
On 10/12/26 13:14, Christian Couder wrote:
Show 22 quoted lines
> diff --git a/parse-options.c b/parse-options.c
> index a98b958423..c03c396aa6 100644
> --- a/parse-options.c
> +++ b/parse-options.c
> @@ -726,6 +726,8 @@ static void parse_options_check(const struct option *opts)
>   		     opts->long_name))
>   			optbug(opts, "uses feature "
>   			       "not supported for dashless options");
> +		if ((opts->flags & PARSE_OPT_EARLY) && !opts->long_name)
> +			optbug(opts, "uses PARSE_OPT_EARLY, which needs a long name");
>   		if (opts->type == OPTION_SET_INT && !opts->defval &&
>   		    opts->long_name && !(opts->flags & PARSE_OPT_NONEG))
>   			optbug(opts, "OPTION_SET_INT 0 should not be negatable");
> @@ -763,6 +765,8 @@ static void parse_options_check(const struct option *opts)
>   		case OPTION_SUBCOMMAND:
>   			if (!opts->value || !opts->subcommand_fn)
>   				optbug(opts, "OPTION_SUBCOMMAND needs a value and a subcommand function");
> +			if (opts->flags & PARSE_OPT_EARLY)
> +				optbug(opts, "OPTION_SUBCOMMAND does not support PARSE_OPT_EARLY");
>   			if (!subcommand_value)
>   				subcommand_value = opts->value;
>   			else if (subcommand_value != opts->value)
Would it make sense to also have these validations within early_scan_options() just in case it is called before parse_options_start() is invoked?
  > diff --git a/parse-options.h b/parse-options.h
Show 32 quoted lines
> index 589f8d9008..f39b9a779a 100644
> --- a/parse-options.h
> +++ b/parse-options.h
> @@ -51,6 +51,7 @@ enum parse_opt_option_flags {
>   	PARSE_OPT_NODASH = 1 << 5,
>   	PARSE_OPT_LITERAL_ARGHELP = 1 << 6,
>   	PARSE_OPT_FROM_ALIAS = 1 << 7,
> +	PARSE_OPT_EARLY = 1 << 8,	/* only for early_scan_options() */
>   	PARSE_OPT_NOCOMPLETE = 1 << 9,
>   	PARSE_OPT_COMP_ARG = 1 << 10,
>   	PARSE_OPT_CMDMODE = 1 << 11,
> @@ -502,6 +503,96 @@ static inline void die_for_incompatible_opt2(int opt1, const char *opt1_name,
>   		BUG("option callback expects an argument"); \
>   } while(0)
>   > +/*----- Early scan: scanning argv before the actual option parsing -----*/
> +
> +/*
> + * Some commands need to look at a few options before they can parse
> + * their command line for real, for example because the result decides
> + * whether a repository is needed at all.
> + *
> + * Such an early scan has to know which options take their value as a
> + * separate argument, or it could mistake such a value for an
> + * option. The functions below allow performing such early scans
> + * without being fooled by option values.
> + */
> +
> +/*
> + * Called by early_scan_options() for each argument matching a
> + * `struct option` with PARSE_OPT_EARLY set.
> + *
> + * - `option` is the matching option,
Would it be helpful to also convey here that the caller is not expected to retain the pointer as the memory pointing to it would be freed once the scan completes?
Rest of the patch looks fine to me.
-- 
Sivaraam
Previous: Christian CouderNext: Christian Couder
Message 29 of 41 in “Standardize early option scanning to fix argument parsing bugs”
  1. 0/6 Standardize early option scanning to fix argument parsing bugsChristian Couder, Sep 2, 2026
  2. 1/6 parse-options: add early_scan_options()Christian Couder, Sep 2, 2026
  3. Junio C HamanoSep 2, 2026
  4. Christian CouderSep 23, 2026
  5. Junio C HamanoSep 23, 2026
  6. 2/6 bisect: fix "--" detection when a term name is "--"Christian Couder, Sep 2, 2026
  7. Junio C HamanoSep 2, 2026
  8. Christian CouderSep 23, 2026
  9. Junio C HamanoSep 23, 2026
  10. 3/6 rev-parse: fix "--" detection when it is an option valueChristian Couder, Sep 2, 2026
  11. 4/6 parse-options: add parse_options_takes_argument()Christian Couder, Sep 2, 2026
  12. 5/6 parse-options: build early scan options from a struct option arrayChristian Couder, Sep 2, 2026
  13. 6/6 fast-import: use early_scan_options() for --allow-unsafe-featuresChristian Couder, Sep 2, 2026
  14. Junio C HamanoSep 4, 2026
  15. Junio C HamanoSep 2, 2026
  16. Christian CouderSep 23, 2026
  17. 0/3 Standardize early option scanningChristian Couder, Sep 23, 2026
  18. 1/3 parse-options: add parse_options_takes_argument()Christian Couder, Sep 23, 2026
  19. 2/3 parse-options: add early_scan_options()Christian Couder, Sep 23, 2026
  20. Kaartic SivaraamSep 30, 2026
  21. Christian CouderOct 12, 2026
  22. 3/3 fast-import: use early_scan_options() for --allow-unsafe-featuresChristian Couder, Sep 23, 2026
  23. 0/4 Standardize early option scanningChristian Couder, Oct 12, 2026
  24. 1/4 parse-options: add parse_options_takes_argument()Christian Couder, Oct 12, 2026
  25. 2/4 parse-options: extract lookup_long_opt() from parse_long_opt()Christian Couder, Oct 12, 2026
  26. Kaartic SivaraamOct 30, 2026
  27. Christian CouderNov 2, 2026
  28. 3/4 parse-options: add early_scan_options()Christian Couder, Oct 12, 2026
  29. Kaartic SivaraamOct 30, 2026
  30. Christian CouderNov 2, 2026
  31. 4/4 fast-import: use early_scan_options() for --allow-unsafe-featuresChristian Couder, Oct 12, 2026
  32. Cynthia UcheOct 31, 2026
  33. Christian CouderNov 2, 2026
  34. Kaartic SivaraamOct 30, 2026
  35. 0/4 Standardize early option scanningChristian Couder, Nov 2, 2026
  36. 1/4 parse-options: add parse_options_takes_argument()Christian Couder, Nov 2, 2026
  37. 2/4 parse-options: extract lookup_long_opt() from parse_long_opt()Christian Couder, Nov 2, 2026
  38. 3/4 parse-options: add early_scan_options()Christian Couder, Nov 2, 2026
  39. 4/4 fast-import: use early_scan_options() for --allow-unsafe-featuresChristian Couder, Nov 2, 2026
  40. Kaartic SivaraamNov 3, 2026
  41. Junio C HamanoNov 3, 2026

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.