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

Re: [PATCH 2/4] parse-options: name flags passed to usage_with_options_internal

From
D. Ben Knoble <ben.knoble+github@gmail.com>
Date
Jul 30, 2025, 22:05 UTC
Message-ID
<CALnO6CDg5tJXNG2V1qXb_Qds+3tDM6cUgbiDO7i3Fy_wc0=FPw@mail.gmail.com>
In-Reply-To
<xmqqjz3sxro3.fsf@gitster.g>
On Mon, Jul 28, 2025 at 11:26 AM Junio C Hamano <gitster@pobox.com> wrote:
Show 12 quoted lines
>
> "D. Ben Knoble" <ben.knoble+github@gmail.com> writes:
>
> > When reading or editing calls to usage_with_options_internal, it is
> > difficult to tell what trailing "0, 0", "0, 1", "1, 0" arguments mean
> > (NB there is never a "1, 1" case).
> >
> > Give the flags readable names to improve call-sites.
>
> It is a good idea to explicitly say that this step introduces no
> change in behaviour, and only changes the way how these 0/1 are
> spelled.
Woops; definitely meant for that to be clearer. Will touch up.
Show 51 quoted lines
>
> > Signed-off-by: D. Ben Knoble <ben.knoble+github@gmail.com>
> > ---
> >  parse-options.c | 32 ++++++++++++++++++++++----------
> >  1 file changed, 22 insertions(+), 10 deletions(-)
> >
> > diff --git a/parse-options.c b/parse-options.c
> > index 5224203ffe..c3222cc9bb 100644
> > --- a/parse-options.c
> > +++ b/parse-options.c
> > @@ -953,10 +953,21 @@ static void free_preprocessed_options(struct option *options)
> >       free(options);
> >  }
> >
> > +enum usage_style {
> > +     style_normal = 0,
> > +     style_full = 1,
> > +};
> > +
> > +enum usage_output {
> > +     to_out = 0,
> > +     to_err = 1,
> > +};
>
> These are very much internal implementation detail, so I am not sure
> if this churn is a good thing, though.
>
> For example, it ought to be sufficient, for the purpose of improved
> readability, to instead doing this
>
> >  static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t *,
> >                                                        const char * const *,
> >                                                        const struct option *,
> > -                                                      int, int);
> > +                                                      enum usage_style,
> > +                                                      enum usage_output);
>
> just do
>
>                 int full_usage,
>                 int usage_to_stderr);
>
> here.  Dropping the parameter names in the function prototype is
> allowed, and we encourage to do so in our codebase but _only_ when
> the meaning of each parameter is obvious from their type.  The first
> 3 parameters we see above are of distinct types and except for the
> second one being the usage string given to the users, they should be
> obvious.  But the last two unnamed integers are not obvious and they
> should have been spelled out---otherwise a developer who is adding
> a new callsite cannot work from the prototype alone and has to go to
> the implementation to figure out what to pass.

Yeah, but that relies on folks reading the prototype, no? I wanted it to be easier to read at the call sites (_without_ special tooling, preferably).

I'll snip the rest, though, due to your downthread suggestion to use #define's instead, which I think gives the result I want without extra churn in other places.

Show 9 quoted lines
> > @@ -1088,7 +1099,7 @@ enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
> >               }
> >
> >               if (internal_help && !strcmp(arg + 2, "help-all"))
> > -                     return usage_with_options_internal(ctx, usagestr, options, 1, 0);
> > +                     return usage_with_options_internal(ctx, usagestr, options, style_full, to_out);
>
> But this is not an improvement as-is.  Wrap long lines or the result
> is even harder to read.
Ah, indeed: I wasn't sure where to draw the line there.
Previous: Junio C HamanoNext: D. Ben Knoble
Message 8 of 30 in “permit -h/--help-all in more scenarios”
  1. 0/4 permit -h/--help-all in more scenariosD. Ben Knoble, Jul 26, 2025
  2. 1/4 t1517: fixup for ua/t1517-short-help-testsD. Ben Knoble, Jul 26, 2025
  3. Usman AkinyemiJul 26, 2025
  4. Junio C HamanoJul 28, 2025
  5. 2/4 parse-options: name flags passed to usage_with_options_internalD. Ben Knoble, Jul 26, 2025
  6. Junio C HamanoJul 28, 2025
  7. Junio C HamanoJul 28, 2025
  8. D. Ben KnobleJul 30, 2025
  9. 3/4 builtin: also setup gently for --help-allD. Ben Knoble, Jul 26, 2025
  10. Junio C HamanoJul 28, 2025
  11. D. Ben KnobleJul 30, 2025
  12. 4/4 builtins: show help on "-h"/"--help-all" with more than 2 arguments leftD. Ben Knoble, Jul 26, 2025
  13. Junio C HamanoJul 27, 2025
  14. D. Ben KnobleJul 30, 2025
  15. Jeff KingAug 2, 2025
  16. D. Ben KnobleAug 2, 2025
  17. Jeff KingAug 2, 2025
  18. D. Ben KnobleAug 2, 2025
  19. 0/3 permit -h/--help-all in more scenariosD. Ben Knoble, Aug 3, 2025
  20. 0/3 permit -h/--help-all in more scenariosD. Ben Knoble, Aug 3, 2025
  21. Junio C HamanoAug 4, 2025
  22. D. Ben KnobleAug 5, 2025
  23. 1/3 t1517: fixup for ua/t1517-short-help-testsD. Ben Knoble, Aug 3, 2025
  24. Junio C HamanoAug 3, 2025
  25. D. Ben KnobleAug 3, 2025
  26. 2/3 parse-options: refactor flags for usage_with_options_internalD. Ben Knoble, Aug 3, 2025
  27. 3/3 builtin: also setup gently for --help-allD. Ben Knoble, Aug 3, 2025
  28. 1/3 t1517: fixup for ua/t1517-short-help-testsD. Ben Knoble, Aug 3, 2025
  29. 2/3 parse-options: refactor flags for usage_with_options_internalD. Ben Knoble, Aug 3, 2025
  30. 3/3 builtin: also setup gently for --help-allD. Ben Knoble, Aug 3, 2025

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.