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

Re: [PATCH 1/6] parse-options: add early_scan_options()

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 2, 2026, 22:11 UTC
Message-ID
<xmqqy0djfgmt.fsf@gitster.g>
In-Reply-To
<20260902161047.476753-2-christian.couder@gmail.com>
Christian Couder <christian.couder@gmail.com> writes:
> So users must spell these specific options in full. This restriction
> could be lifted in the future though, once the scanner is adapted to
> accept a command's full option array, as this would give it the
> complete context needed for safe abbreviation matching.

It is unfortunate that end-users cannot tell if they are dealing with a system before of after "once the scanner is adapted" happened, so they must be trained to always spell the options in full to make use of the commands that use this feature. It at least does not regress relative to the ad-hoc early scanners these selected commands have that do not even understand what they are parsing, so it may not be too bad.

Stepping back a bit, the burden on programmers to use this would be to write in a separate notation what options there are in addition to what they feed the real parse_options(), which cuts both ways in the sense that because this does not take parse_options(), commands that do not use parse_options() can still use it, but those that do already use parse_options() need additional work to use eary_scan.

And then once the scanner is adapted to accept the full option array, the programmers only need to discard the struct early_scan_option[] they wrote and replace it with the struct option[] they already have? Or would the calling convention to the scanner also change when it happens (oother than replacing the pointer to struct early_scan_option[] with another pointer to struct option[])?

> +static const struct early_scan_option *
> +find_early_scan_option(const char *arg,
> +		       const struct early_scan_option *options,
> +		       const char **value)

Because you return one single element from the incoming array of options, it is mildly misleading to call the variable/parameter "options" here and everywhere else. Let's stick to "arrays are named singular, so that option[4] names 4th option" convention.

Show 9 quoted lines
> +{
> +	if (!skip_prefix(arg, "--", &arg))
> +		return NULL;
> +
> +	for (; options->name; options++) {
> +		const char *rest;
> +
> +		if (!skip_prefix(arg, options->name, &rest))
> +			continue;

"--option" on the command line, after getting stripped the leading "--", may begin with "option", and that name may be in the option[] table, in which case ...

> +		if (!*rest) {
> +			*value = NULL;
> +			return options;
> +		}

... we found a hit. But shouldn't option->takes_value be consulted before we return to signal the caller that the next arg is an option value before we return from here? It looks a bit uneven as we do that for stuck form "--option=value" here.

Show 5 quoted lines
> +		/* Only an option taking a value can be stuck to one. */
> +		if (*rest == '=' && options->takes_value) {
> +			*value = rest + 1;
> +			return options;
> +		}

And if the option[] table had "opt", then "--option" on the command line may begin with "--opt" but "ion" is an excess that is not a stuck value, so we do not consider it as a match. OK.

> +	}
> +	return NULL;
> +}

If we are to write a separate function anyway, I wonder how much more work to write a early_scan_option() parser that does take a real "struct option[]" array. Its elements already know if they take a value or not. For expediency, it may be OK to start by simplified parser that does not handle unique prefix and other complexities like callback functions of the real parser, but at least it would reduce the burden on the programmers quite a bit if we used the real struct option[] array, I suspect.

Previous: Christian CouderNext: Christian Couder
Message 3 of 21 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. 3/3 fast-import: use early_scan_options() for --allow-unsafe-featuresChristian Couder, Sep 23, 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.