git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: "SZEDER Gábor" <szeder.dev@gmail.com>
Cc: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH v2 09/20] parse-options: add support for parsing subcommands
Date: Fri, 19 Aug 2022 19:33:46 +0200	[thread overview]
Message-ID: <220819.86bksg6s5l.gmgdl@evledraar.gmail.com> (raw)
In-Reply-To: <20220819160411.1791200-10-szeder.dev@gmail.com>


On Fri, Aug 19 2022, SZEDER Gábor wrote:

> +static enum parse_opt_result parse_subcommand(const char *arg,
> +					      const struct option *options)
> +{
> +	for (; options->type != OPTION_END; options++)
> +		if (options->type == OPTION_SUBCOMMAND &&
> +		    !strcmp(options->long_name, arg)) {
> +			*(parse_opt_subcommand_fn **)options->value = options->subcommand_fn;

Nit: Maybe do the cast by assigning to an intermediate variable? Would
make this less dense, and since we already have {}-braces...

> +			return PARSE_OPT_SUBCOMMAND;
> +		}
> +
> +	return PARSE_OPT_UNKNOWN;
> +}
> +
>  static void check_typos(const char *arg, const struct option *options)
>  {
>  	if (strlen(arg) < 3)
> @@ -442,6 +457,7 @@ static void check_typos(const char *arg, const struct option *options)
>  static void parse_options_check(const struct option *opts)
>  {
>  	char short_opts[128];
> +	void *subcommand_value = NULL;
>  
>  	memset(short_opts, '\0', sizeof(short_opts));
>  	for (; opts->type != OPTION_END; opts++) {
> @@ -489,6 +505,14 @@ static void parse_options_check(const struct option *opts)
>  			       "Are you using parse_options_step() directly?\n"
>  			       "That case is not supported yet.");
>  			break;
> +		case OPTION_SUBCOMMAND:
> +			if (!opts->value || !opts->subcommand_fn)
> +				optbug(opts, "OPTION_SUBCOMMAND needs a value and a subcommand function");

Better split into two IMO:

	if (!opts->value)
		optbug(opts, "OPTION_SUBCOMMAND needs a value");
	if (!opts->subcommand_fn)
		optbug(opts, "OPTION_SUBCOMMAND needs a subcommand_fn");

Then if we have extra checks we don't need to keep adding to a dense ||
and amend an existing string.

> +static int has_subcommands(const struct option *options)
> +{
> +	for (; options->type != OPTION_END; options++)
> +		if (options->type == OPTION_SUBCOMMAND)
> +			return 1;
> +	return 0;
> +}

I wondered if parse_options_check() couldn't take a "int
*has_subcommands", since we already loop through the options to check it
as part of the checks there (but we'd need to re-arrange them).

Not for optimization, just wondering if it would make the code flow
clearer, maybe not.

> +	if (ctx->has_subcommands) {
> +		if (flags & PARSE_OPT_STOP_AT_NON_OPTION)
> +			BUG("subcommands are incompatible with PARSE_OPT_STOP_AT_NON_OPTION");
> +		if (!(flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)) {
> +			if (flags & PARSE_OPT_KEEP_UNKNOWN_OPT)
> +				BUG("subcommands are incompatible with PARSE_OPT_KEEP_UNKNOWN_OPT unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
> +			if (flags & PARSE_OPT_KEEP_DASHDASH)
> +				BUG("subcommands are incompatible with PARSE_OPT_KEEP_DASHDASH unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");

MM, very long lines. Maybe something like:

	BUG("%s flag cannot be combined with %s, unless %s is also provided", ...);

> +		}
> +	}
>  	if ((flags & PARSE_OPT_KEEP_UNKNOWN_OPT) &&
>  	    (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
>  	    !(flags & PARSE_OPT_ONE_SHOT))
> @@ -589,6 +634,7 @@ static int show_gitcomp(const struct option *opts, int show_all)
>  	int nr_noopts = 0;
>  
>  	for (; opts->type != OPTION_END; opts++) {
> +		const char *prefix = "--";

Ah, here we have a prefix variable, but it's more of an "infix"... :)

> +					return PARSE_OPT_DONE;
> +				error(_("unknown subcommand: `%s'"), arg);
> +				usage_with_options(usagestr, options);

ditto earlier patch comment about usage_msg_opt() (also applies below)

> +			case PARSE_OPT_NON_OPTION:
> +				/* Impossible. */

We could just skip this comment as...
> +				BUG("parse_subcommand() cannot return these");

...this makes it clear.

>  	case PARSE_OPT_DONE:
> +		if (ctx.has_subcommands &&
> +		    !(flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)) {
> +			error(_("need a subcommand"));
> +			usage_with_options(usagestr, options);

ditto.

> +typedef int parse_opt_subcommand_fn(int argc, const char **argv,
> +				    const char *prefix);

We usually define function typedefs like:

	typedef int (*parse_opt_subcommand_fn)(...);

But I see that...

>  	enum parse_opt_type type;
> @@ -145,6 +155,7 @@ struct option {
>  	intptr_t defval;
>  	parse_opt_ll_cb *ll_callback;
>  	intptr_t extra;
> +	parse_opt_subcommand_fn *subcommand_fn;

You're doing this consistently with parse_opt_ll_cb etc., fair enough.

> +#define OPT_SUBCOMMAND_F(l, v, fn, f) { \
> +	.type = OPTION_SUBCOMMAND, \
> +	.long_name = (l), \
> +	.value = (v), \
> +	.flags = (f), \

Nice to see this form

> +	.subcommand_fn = (fn) }

Almost all other such decls end with:

		.foo = bar, \
	}

I.e. we put the closing "}" at the start of the line.

> +#define OPT_SUBCOMMAND(l, v, fn)    OPT_SUBCOMMAND_F((l), (v), (fn), 0)

I think this would be more readable as:

	#define OPT_SUBCOMMAND(l, v, fn) \
		OPT_SUBCOMMAND_F((l), (v), (fn), 0)

Which both aligns nicely, and isn't using a \t in the middle of the line
(which in any case we'll end up aligning with nothing else, as the names
are all different lengths, if other things continue using this pattern.
>  	enum parse_opt_flags flags;
> +	unsigned has_subcommands;

Maybe make it: "unsigned has_subcommands:1"?

> +	printf("fn: subcmd_one\n");

s/printf/puts/g here

> +	print_args(argc, argv);
> +	return 0;
> +}
> +
> +static int subcmd_two(int argc, const char **argv, const char *prefix)
> +{
> +	printf("fn: subcmd_two\n");

ditto.


  reply	other threads:[~2022-08-19 18:02 UTC|newest]

Thread overview: 97+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-25 12:38 [PATCH 00/20] parse-options: handle subcommands SZEDER Gábor
2022-07-25 12:38 ` [PATCH 01/20] git.c: update NO_PARSEOPT markings SZEDER Gábor
2022-07-25 14:31   ` Ævar Arnfjörð Bjarmason
2022-08-02 17:37     ` SZEDER Gábor
2022-08-02 21:00       ` Junio C Hamano
2022-08-03 13:11         ` Ævar Arnfjörð Bjarmason
2022-08-03 21:34         ` SZEDER Gábor
2022-08-04  7:47           ` Ævar Arnfjörð Bjarmason
2022-08-11 21:35           ` Junio C Hamano
2022-08-12 15:28             ` SZEDER Gábor
2022-08-12 16:46               ` Junio C Hamano
2022-07-26 19:55   ` SZEDER Gábor
2022-07-25 12:38 ` [PATCH 02/20] t3301-notes.sh: check that default operation mode doesn't take arguments SZEDER Gábor
2022-07-25 12:38 ` [PATCH 03/20] t5505-remote.sh: check the behavior without a subcommand SZEDER Gábor
2022-07-25 14:37   ` Ævar Arnfjörð Bjarmason
2022-07-25 12:38 ` [PATCH 04/20] t0040-parse-options: test parse_options() with various 'parse_opt_flags' SZEDER Gábor
2022-07-25 14:38   ` Ævar Arnfjörð Bjarmason
2022-08-12 15:04     ` SZEDER Gábor
2022-07-25 12:38 ` [PATCH 05/20] api-parse-options.txt: fix description of OPT_CMDMODE SZEDER Gábor
2022-07-25 12:38 ` [PATCH 06/20] parse-options: PARSE_OPT_KEEP_UNKNOWN only applies to --options SZEDER Gábor
2022-07-25 12:38 ` [PATCH 07/20] parse-options: clarify the limitations of PARSE_OPT_NODASH SZEDER Gábor
2022-07-25 12:38 ` [PATCH 08/20] parse-options: drop leading space from '--git-completion-helper' output SZEDER Gábor
2022-07-25 12:38 ` [PATCH 09/20] parse-options: add support for parsing subcommands SZEDER Gábor
2022-07-25 14:43   ` Ævar Arnfjörð Bjarmason
2022-07-25 19:29     ` SZEDER Gábor
2022-07-25 19:41       ` Ævar Arnfjörð Bjarmason
2022-07-25 21:02         ` SZEDER Gábor
2022-08-12 15:15         ` SZEDER Gábor
2022-07-25 17:37   ` Junio C Hamano
2022-07-25 12:38 ` [PATCH 10/20] builtin/bundle.c: let parse-options parse subcommands SZEDER Gábor
2022-07-25 12:38 ` [PATCH 11/20] builtin/commit-graph.c: " SZEDER Gábor
2022-07-25 12:38 ` [PATCH 12/20] builtin/gc.c: let parse-options parse 'git maintenance's subcommands SZEDER Gábor
2022-07-25 12:38 ` [PATCH 13/20] builtin/hook.c: let parse-option parse subcommands SZEDER Gábor
2022-07-25 12:38 ` [PATCH 14/20] builtin/multi-pack-index.c: let parse-options " SZEDER Gábor
2022-07-25 12:38 ` [PATCH 15/20] builtin/notes.c: " SZEDER Gábor
2022-07-25 16:49   ` Junio C Hamano
2022-07-25 12:38 ` [PATCH 16/20] builtin/reflog.c: " SZEDER Gábor
2022-07-25 12:38 ` [PATCH 17/20] builtin/remote.c: " SZEDER Gábor
2022-07-25 12:38 ` [PATCH 18/20] builtin/sparse-checkout.c: " SZEDER Gábor
2022-07-25 12:38 ` [PATCH 19/20] builtin/stash.c: " SZEDER Gábor
2022-07-25 12:38 ` [PATCH 20/20] builtin/worktree.c: " SZEDER Gábor
2022-07-25 13:15 ` [PATCH 00/20] parse-options: handle subcommands Derrick Stolee
2022-07-25 16:00   ` SZEDER Gábor
2022-07-25 16:08     ` Derrick Stolee
2022-07-25 17:13 ` Ævar Arnfjörð Bjarmason
2022-07-25 17:56 ` Junio C Hamano
2022-07-26 15:42   ` Johannes Schindelin
2022-07-26 18:02     ` Ævar Arnfjörð Bjarmason
2022-08-19 16:03 ` [PATCH v2 " SZEDER Gábor
2022-08-19 16:03   ` [PATCH v2 01/20] git.c: update NO_PARSEOPT markings SZEDER Gábor
2022-08-19 16:03   ` [PATCH v2 02/20] t3301-notes.sh: check that default operation mode doesn't take arguments SZEDER Gábor
2022-08-19 16:03   ` [PATCH v2 03/20] t5505-remote.sh: check the behavior without a subcommand SZEDER Gábor
2022-08-19 16:03   ` [PATCH v2 04/20] t0040-parse-options: test parse_options() with various 'parse_opt_flags' SZEDER Gábor
2022-08-19 17:23     ` Ævar Arnfjörð Bjarmason
2022-08-20 11:14       ` SZEDER Gábor
2022-08-19 18:18     ` Junio C Hamano
2022-08-20 10:31       ` SZEDER Gábor
2022-08-20 21:27         ` Junio C Hamano
2022-08-19 16:03   ` [PATCH v2 05/20] api-parse-options.txt: fix description of OPT_CMDMODE SZEDER Gábor
2022-08-19 16:03   ` [PATCH v2 06/20] parse-options: PARSE_OPT_KEEP_UNKNOWN only applies to --options SZEDER Gábor
2022-08-19 16:03   ` [PATCH v2 07/20] parse-options: clarify the limitations of PARSE_OPT_NODASH SZEDER Gábor
2022-08-19 16:03   ` [PATCH v2 08/20] parse-options: drop leading space from '--git-completion-helper' output SZEDER Gábor
2022-08-19 17:30     ` Ævar Arnfjörð Bjarmason
2022-08-19 18:35       ` SZEDER Gábor
2022-08-19 16:04   ` [PATCH v2 09/20] parse-options: add support for parsing subcommands SZEDER Gábor
2022-08-19 17:33     ` Ævar Arnfjörð Bjarmason [this message]
2022-08-19 19:03     ` Ævar Arnfjörð Bjarmason
2022-08-19 16:04   ` [PATCH v2 10/20] builtin/bundle.c: let parse-options parse subcommands SZEDER Gábor
2022-08-19 17:50     ` Ævar Arnfjörð Bjarmason
2022-08-19 16:04   ` [PATCH v2 11/20] builtin/commit-graph.c: " SZEDER Gábor
2022-08-19 17:53     ` Ævar Arnfjörð Bjarmason
2022-08-19 17:56       ` Ævar Arnfjörð Bjarmason
2022-08-19 18:22       ` SZEDER Gábor
2022-08-19 16:04   ` [PATCH v2 12/20] builtin/gc.c: let parse-options parse 'git maintenance's subcommands SZEDER Gábor
2022-08-19 20:59     ` Junio C Hamano
2022-08-19 16:04   ` [PATCH v2 13/20] builtin/hook.c: let parse-options parse subcommands SZEDER Gábor
2022-08-19 16:04   ` [PATCH v2 14/20] builtin/multi-pack-index.c: " SZEDER Gábor
2022-08-19 17:57     ` Ævar Arnfjörð Bjarmason
2022-08-19 16:04   ` [PATCH v2 15/20] builtin/notes.c: " SZEDER Gábor
2022-08-19 18:01     ` Ævar Arnfjörð Bjarmason
2022-08-21 17:56       ` SZEDER Gábor
2022-08-19 16:04   ` [PATCH v2 16/20] builtin/reflog.c: " SZEDER Gábor
2022-08-19 18:08     ` Ævar Arnfjörð Bjarmason
2022-08-19 16:04   ` [PATCH v2 17/20] builtin/remote.c: " SZEDER Gábor
2022-08-19 16:04   ` [PATCH v2 18/20] builtin/sparse-checkout.c: " SZEDER Gábor
2022-08-19 16:04   ` [PATCH v2 19/20] builtin/stash.c: " SZEDER Gábor
2022-08-19 19:06     ` Ævar Arnfjörð Bjarmason
2022-08-20 10:27       ` SZEDER Gábor
2022-08-19 16:04   ` [PATCH v2 20/20] builtin/worktree.c: " SZEDER Gábor
2022-09-05 18:50   ` [PATCH 0/5] parse-options: minor cleanups for handling subcommands SZEDER Gábor
2022-09-05 18:50     ` [PATCH 1/5] t0040-parse-options: remove leftover debugging SZEDER Gábor
2022-09-05 18:50     ` [PATCH 2/5] test-parse-options.c: don't use for loop initial declaration SZEDER Gábor
2022-09-05 18:50     ` [PATCH 3/5] test-parse-options.c: fix style of comparison with zero SZEDER Gábor
2022-09-05 18:50     ` [PATCH 4/5] notes: simplify default operation mode arguments check SZEDER Gábor
2022-09-05 18:50     ` [PATCH 5/5] notes, remote: show unknown subcommands between `' SZEDER Gábor
2022-09-07 19:12     ` [PATCH 0/5] parse-options: minor cleanups for handling subcommands Junio C Hamano
2022-09-07 21:22       ` SZEDER Gábor

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=220819.86bksg6s5l.gmgdl@evledraar.gmail.com \
    --to=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=szeder.dev@gmail.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).