From: "SZEDER Gábor" <szeder.dev@gmail.com>
To: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Cc: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH v2 15/20] builtin/notes.c: let parse-options parse subcommands
Date: Sun, 21 Aug 2022 19:56:09 +0200 [thread overview]
Message-ID: <20220821175609.GF3373722@szeder.dev> (raw)
In-Reply-To: <220819.86pmgw5cpk.gmgdl@evledraar.gmail.com>
On Fri, Aug 19, 2022 at 08:01:55PM +0200, Ævar Arnfjörð Bjarmason wrote:
>
> On Fri, Aug 19 2022, SZEDER Gábor wrote:
>
> > - int result;
> > const char *override_notes_ref = NULL;
> > + parse_opt_subcommand_fn *fn = list;
> > struct option options[] = {
> > OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
> > N_("use notes from <notes-ref>")),
> > + OPT_SUBCOMMAND("list", &fn, list),
> > + OPT_SUBCOMMAND("add", &fn, add),
> > + OPT_SUBCOMMAND("copy", &fn, copy),
> > + OPT_SUBCOMMAND("append", &fn, append_edit),
> > + OPT_SUBCOMMAND("edit", &fn, append_edit),
> > + OPT_SUBCOMMAND("show", &fn, show),
> > + OPT_SUBCOMMAND("merge", &fn, merge),
> > + OPT_SUBCOMMAND("remove", &fn, remove_cmd),
> > + OPT_SUBCOMMAND("prune", &fn, prune),
> > + OPT_SUBCOMMAND("get-ref", &fn, get_ref),
> > OPT_END()
> > };
> >
> > git_config(git_default_config, NULL);
> > argc = parse_options(argc, argv, prefix, options, git_notes_usage,
> > - PARSE_OPT_STOP_AT_NON_OPTION);
> > + PARSE_OPT_SUBCOMMAND_OPTIONAL);
> > + if (fn == list && argc && strcmp(argv[0], "list")) {
> > + error(_("unknown subcommand: %s"), argv[0]);
This should have been `%s' here, and in cmd_remote() as well.
> > + usage_with_options(git_notes_usage, options);
> > + }
>
> I wanted to ask why the API can't smartly handle this, but your "Found
> an unknown option given to a command with" comment in an earlier patch
> answered it.
It's not about unknown options but rather about (non-option) arguments.
'git notes list' doesn't accept any --options, and since this 'list'
is the default operation mode, parse_options() is invoked without the
PARSE_OPT_KEEP_UNKNOWN_OPT flag, so 'git notes --foo' errors out even
without any of the extra checks in the above hunk.
However, while 'git notes list' does accept non-option arguments
(objects or refs), 'git notes' does not. Alas, currently there is no
way to tell parse_options() to error out upon finding a (non-option
and non-subcommand) argument, it always keeps them in 'argv'; that's
why we need these additional checks.
Now, while we could add such a flag, of course, it would not be
limited to this one particular use case, so when the error is
triggered inside parse_options() I doubt that we could have this
specific "unknown subcommand" error message.
> I think something in this direction would be a bit more readble/obvious,
> as it avoids hardcoding "list":
>
> diff --git a/builtin/notes.c b/builtin/notes.c
> index 42cbae46598..43d59b1a98e 100644
> --- a/builtin/notes.c
> +++ b/builtin/notes.c
> @@ -995,7 +995,7 @@ static int get_ref(int argc, const char **argv, const char *prefix)
> int cmd_notes(int argc, const char **argv, const char *prefix)
> {
> const char *override_notes_ref = NULL;
> - parse_opt_subcommand_fn *fn = list;
> + parse_opt_subcommand_fn *fn = NULL;
> struct option options[] = {
> OPT_STRING(0, "ref", &override_notes_ref, N_("notes-ref"),
> N_("use notes from <notes-ref>")),
> @@ -1015,10 +1015,11 @@ int cmd_notes(int argc, const char **argv, const char *prefix)
> git_config(git_default_config, NULL);
> argc = parse_options(argc, argv, prefix, options, git_notes_usage,
> PARSE_OPT_SUBCOMMAND_OPTIONAL);
> - if (fn == list && argc && strcmp(argv[0], "list")) {
> - error(_("unknown subcommand: %s"), argv[0]);
> - usage_with_options(git_notes_usage, options);
> - }
> + if (!fn && argc)
> + usage_msg_optf(_("unknown subcommand: %s"),
> + git_notes_usage, options, argv[0]);
> + else if (!fn)
> + fn = list;
>
> if (override_notes_ref) {
> struct strbuf sb = STRBUF_INIT;
>
> I.e. we rely on the API setting it to non-NULL if it finds a subcommand,
> otherwise we just set it to "list" after checking whether we have excess
> arguments.
Oh, that does look nicer indeed.
> > [...]
> > - else if (!strcmp(argv[0], "get-ref"))
> > - result = get_ref(argc, argv, prefix);
> > - else {
> > - result = error(_("unknown subcommand: %s"), argv[0]);
> > - usage_with_options(git_notes_usage, options);
> > - }
> > -
> > - return result ? 1 : 0;
> > + return !!fn(argc, argv, prefix);
> > }
>
> In any case this is a lot nicer, ditto previous comment about maybe
> skipping the refactoring of this end code, but I'm also fine with
> keeping it.
next prev parent reply other threads:[~2022-08-21 17:56 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
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 [this message]
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=20220821175609.GF3373722@szeder.dev \
--to=szeder.dev@gmail.com \
--cc=avarab@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.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).