All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
To: Christian Couder <christian.couder@gmail.com>, git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
	Patrick Steinhardt <ps@pks.im>, Elijah Newren <newren@gmail.com>,
	Jeff King <peff@peff.net>,
	"brian m . carlson" <sandals@crustytoothpaste.net>,
	Johannes Schindelin <Johannes.Schindelin@gmx.de>,
	Justin Tobler <jltobler@gmail.com>
Subject: Re: [PATCH v2 2/3] parse-options: add early_scan_options()
Date: Wed, 30 Sep 2026 12:43:53 +0530	[thread overview]
Message-ID: <84f9d1c9-30b7-4d8b-82d6-9afd16d0ab08@gmail.com> (raw)
In-Reply-To: <20260923080928.1534413-3-christian.couder@gmail.com>

On 9/23/26 13:39, Christian Couder wrote:
>
 > [ snip ]
 >
> One consequence of staying simple is that abbreviated options are
> still not matched, even though the scan is now given the command's
> full option array. Resolving them the way parse_options() does would
> mean duplicating the ambiguity detection that parse_long_opt()
> performs. So the scan can fail to see an option that parse_options()
> would accept, and its callers have to cope with that, typically by
> erring on the safe side. This and the other differences with
> parse_options() are documented in "parse-options.h".
> 

I think not handling abbreviations could also have another potential 
problem. Consider a command as follows:

  $ git fast-import --quiet --export-pack --allow-unsafe-features

Here `--export-pack` is an abbreviation of `--export-pack-edges`. So, 
the arg next to it should ideally be considered as a value for it but
given the correct "ignore" logic, we will happily interpret is an 
argument which misaligns with parse_options()'s behaviour.

In the ideal world, we could say such weird names for files is unlikely 
and this isn't such a big concern. But given it is the scope of this 
series to make early scan more reliable, I think we should consider how 
to handle this better.

Would it make sense to actually err on the safe side and just stop 
walking the args as soon as we notice an unrecognized argument? This 
will the ensure the walk never misinterpret a value for an argument.

>   
> diff --git a/parse-options.c b/parse-options.c
> index a132c1ea12..559dad9061 100644
> --- a/parse-options.c
> +++ b/parse-options.c
>
 > [ snip ]
 >
> +int early_scan_options(int argc, const char **argv,
> +		       const struct option *option,
> +		       enum early_scan_flags flags,
> +		       early_scan_fn *fn, void *data)
> +{
> +	for (int i = 0; i < argc; i++) {
> +		const char *arg = argv[i];
> +		const char *value;
> +		const struct option *opt;
> +		int pos = i;
> +
> +		/*
> +		 * parse_options() always stops parsing options at these,
> +		 * whatever its flags, so nothing after them is an option.
> +		 */
> +		if (!strcmp(arg, "--") || !strcmp(arg, "--end-of-options"))
> +			return i;
> +
> +		opt = find_early_scan_option(arg, option, &value);
> +		if (!opt) {
> +			if ((flags & EARLY_SCAN_STOP_AT_NON_OPTION) &&
> +			    (*arg != '-' || !arg[1]))
> +				return i;
> +			continue;
> +		}
> +
> +		/*
> +		 * When an option takes a value, but that value is not
> +		 * stuck to it with '=', then the next argument is the
> +		 * value and it has to be skipped so that it isn't
> +		 * taken for an option itself.
> +		 */
> +		if (parse_options_takes_argument(opt) && !value && i + 1 < argc)
> +			value = argv[++i];
> +

The 'i +1 < argc' part is an appropriate guard to have. But this means a 
command such as the following:

   test-tool early-scan-options --wanted-value

... would reult in 'value' being NULL. I suppose this is kind of 
expected for the early scan code and is not something we need to worry 
about?

> +		if (opt->flags & PARSE_OPT_EARLY && fn(opt, value, pos, data))
> +			return i;
> +	}
> +
> +	return argc;
> +}
> +
>   static int usage_argh(const struct option *opts, FILE *outfile)
>   {
>   	const char *s;
> diff --git a/parse-options.h b/parse-options.h
> index f29e73f85c..3ef64744a4 100644
> --- a/parse-options.h
> +++ b/parse-options.h
 >
> [ snip ]
>
> +/*
> + * Scan `argv` for the options described by `option`, calling `fn` for
> + * each of those that have PARSE_OPT_EARLY set. `argv` is not
> + * modified.
> + *
> + * `fn` may be NULL when no option has PARSE_OPT_EARLY set, which is
> + * useful to only find out where the scan stops.
> + *
> + * The scan always stops at "--" and at "--end-of-options", as
> + * parse_options() always stops parsing options there too, whatever its
> + * flags. PARSE_OPT_KEEP_DASHDASH and PARSE_OPT_KEEP_UNKNOWN_OPT only
> + * decide if the terminator is left in argv, not if it terminates.
> + *
> + * Returns the index at which the scan stopped, which is `argc` when the
> + * whole array was scanned.
> + *

As for the return index, when the callback stops the scan the index 
returned is that of the option's value rather than the option itself. 
Would it be better to capture this more clearly?

Also, would it be helpful to also have a test for this?

> + * This scan is for now deliberately much simpler than
> + * parse_options(), so it differs from it in the following ways:
> + *
> + *  - Only the long form of an option is matched, and it has to be
> + *    spelled in full: short options and abbreviations are ignored.
> + *
> + *  - Negated forms ("--no-<name>") are not matched. This is harmless,
> + *    as they never take a value to skip.
> + *
> + *  - Options with PARSE_OPT_OPTARG or PARSE_OPT_LASTARG_DEFAULT are
> + *    treated as not taking a separate value.
> + *
> + *  - OPTION_SUBCOMMAND entries are skipped.
> + *
> + *  - OPTION_ALIAS entries are not resolved to the option they stand
> + *    for.
> + *
> + * So the scan can fail to see an option that parse_options() would
> + * accept, and callers have to cope with that, typically by erring on
> + * the safe side.
> + */
> +int early_scan_options(int argc, const char **argv,
> +		       const struct option *option,
> +		       enum early_scan_flags flags,
> +		       early_scan_fn *fn, void *data);
 >
> [ snip ]>
> diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh
> index 449fff4d34..b796d96b9a 100755
>
 > [ snip ]> +
> +test_expect_success 'early_scan_options() takes values from struct option' '
> +	test-tool early-scan-options --number --wanted >actual &&
> +	cat >expect <<-\EOF &&
> +	stopped at: 2 of 2
> +	EOF
> +	test_cmp expect actual &&
> +	test-tool early-scan-options --number=5 --wanted >actual &&
> +	cat >expect <<-\EOF &&
> +	found: wanted at 1
> +	stopped at: 2 of 2
> +	EOF
> +	test_cmp expect actual
> +'

Compared to others, I'm not quite sure this test is testing something 
special. Do we need it?

> +test_expect_success 'early_scan_options() does not skip an optional value' '
> +	test-tool early-scan-options --optarg --wanted >actual &&
> +	cat >expect <<-\EOF &&
> +	found: wanted at 1
> +	stopped at: 2 of 2
> +	EOF
> +	test_cmp expect actual &&
> +	test-tool early-scan-options --lastarg --wanted >actual &&
> +	cat >expect <<-\EOF &&
> +	found: wanted at 1
> +	stopped at: 2 of 2
> +	EOF
> +	test_cmp expect actual
> +'
> +
> +test_expect_success 'early_scan_options() matches a stuck optional value' '
> +	test-tool early-scan-options --early-optarg=one >actual &&
> +	cat >expect <<-\EOF &&
> +	found: early-optarg at 0 value: one
> +	stopped at: 1 of 1
> +	EOF
> +	test_cmp expect actual &&
> +	test-tool early-scan-options --early-lastarg=two >actual &&
> +	cat >expect <<-\EOF &&
> +	found: early-lastarg at 0 value: two
> +	stopped at: 1 of 1
> +	EOF
> +	test_cmp expect actual
> +'

Would the following be a useful part to also add to the above?

          test-tool early-scan-options --optarg=5 --wanted >actual &&
          cat >expect <<-\EOF &&
          found: wanted at 1
          stopped at: 2 of 2
          EOF
          test_cmp expect actual

-- 
Sivaraam


  reply	other threads:[~2026-09-30  7:13 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-02 16:10 [PATCH 0/6] Standardize early option scanning to fix argument parsing bugs Christian Couder
2026-09-02 16:10 ` [PATCH 1/6] parse-options: add early_scan_options() Christian Couder
2026-09-02 22:11   ` Junio C Hamano
2026-09-23  8:10     ` Christian Couder
2026-09-23 17:25       ` Junio C Hamano
2026-09-02 16:10 ` [PATCH 2/6] bisect: fix "--" detection when a term name is "--" Christian Couder
2026-09-02 22:30   ` Junio C Hamano
2026-09-23  8:11     ` Christian Couder
2026-09-23 17:27       ` Junio C Hamano
2026-09-02 16:10 ` [PATCH 3/6] rev-parse: fix "--" detection when it is an option value Christian Couder
2026-09-02 16:10 ` [PATCH 4/6] parse-options: add parse_options_takes_argument() Christian Couder
2026-09-02 16:10 ` [PATCH 5/6] parse-options: build early scan options from a struct option array Christian Couder
2026-09-02 16:10 ` [PATCH 6/6] fast-import: use early_scan_options() for --allow-unsafe-features Christian Couder
2026-09-04  3:38   ` Junio C Hamano
2026-09-02 18:52 ` [PATCH 0/6] Standardize early option scanning to fix argument parsing bugs Junio C Hamano
2026-09-23  8:10   ` Christian Couder
2026-09-23  8:09 ` [PATCH v2 0/3] Standardize early option scanning Christian Couder
2026-09-23  8:09   ` [PATCH v2 1/3] parse-options: add parse_options_takes_argument() Christian Couder
2026-09-23  8:09   ` [PATCH v2 2/3] parse-options: add early_scan_options() Christian Couder
2026-09-30  7:13     ` Kaartic Sivaraam [this message]
2026-09-23  8:09   ` [PATCH v2 3/3] fast-import: use early_scan_options() for --allow-unsafe-features Christian Couder

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=84f9d1c9-30b7-4d8b-82d6-9afd16d0ab08@gmail.com \
    --to=kaartic.sivaraam@gmail.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=christian.couder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jltobler@gmail.com \
    --cc=newren@gmail.com \
    --cc=peff@peff.net \
    --cc=ps@pks.im \
    --cc=sandals@crustytoothpaste.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.