Subject: Re: [PATCH 6/8] builtin/config: introduce subcommands

From: Patrick Steinhardt <[email-masked]>
Date: Thu, 7 Mar 2024 08:14:58 +0100

On Wed, Mar 06, 2024 at 01:38:25PM -0800, Karthik Nayak wrote:
> Patrick Steinhardt <[email-masked]> writes:
> 
> > @@ -910,6 +930,20 @@ int cmd_config(int argc, const char **argv, const char *prefix)
> >  {
> >  	given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
> >
> > +	/*
> > +	 * This is somewhat hacky: we first parse the command line while
> > +	 * keeping all args intact in order to determine whether a subcommand
> > +	 * has been specified. If so, we re-parse it a second time, but this
> > +	 * time we drop KEEP_ARGV0. This is so that we don't munge the command
> > +	 * line in case no subcommand was given, which would otherwise confuse
> > +	 * us when parsing the implicit modes.
> > +	 */
> > +	argc = parse_options(argc, argv, prefix, builtin_subcommand_options, builtin_config_usage,
> > +			     PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_ARGV0|PARSE_OPT_KEEP_UNKNOWN_OPT);
> > +	if (subcommand)
> > +		argc = parse_options(argc, argv, prefix, builtin_subcommand_options, builtin_config_usage,
> > +				     PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_UNKNOWN_OPT);
> > +
> 
> I wonder if we can drop the PARSE_OPT_SUBCOMMAND_OPTIONAL in the second
> iteration to make it stricter. But this is OK.

We can't due to a restriction in the parse-options interface: when
subcommands are present, then PARSE_OPT_KEEP_UNKNOWN_OPT requires us to
also pass PARSE_OPT_SUBCOMMAND_OPTIONAL. Otherwise we trigger a `BUG()`.

Patrick