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

Re: [PATCH v2 1/4] git-compat-util: introduce skip_to_optional_val()

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 4, 2017, 14:31 UTC
Message-ID
<xmqq8teio991.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<20171204125617.4931-1-chriscool@tuxfamily.org>
Christian Couder <christian.couder@gmail.com> writes:
Show 40 quoted lines
> From: Christian Couder <christian.couder@gmail.com>
>
> We often accept both a "--key" option and a "--key=<val>" option.
>
> These options currently are parsed using something like:
>
> if (!strcmp(arg, "--key")) {
> 	/* do something */
> } else if (skip_prefix(arg, "--key=", &arg)) {
> 	/* do something with arg */
> }
>
> which is a bit cumbersome compared to just:
>
> if (skip_to_optional_val(arg, "--key", &arg)) {
> 	/* do something with arg */
> }
>
> This also introduces skip_to_optional_val_default() for the few
> cases where something different should be done when the first
> argument is exactly "--key" than when it is exactly "--key=".
>
> In general it is better for UI consistency and simplicity if
> "--key" and "--key=" do the same thing though, so that using
> skip_to_optional_val() should be encouraged compared to
> skip_to_optional_val_default().
>
> Note that these functions can be used to parse any "key=value"
> string where "key" is also considered as valid, not just
> command line options.
>
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  git-compat-util.h | 23 +++++++++++++++++++++++
>  strbuf.c          | 20 ++++++++++++++++++++
>  2 files changed, 43 insertions(+)
>
> After thinking about it a bit more and taking a look at the
> current code, I thought that it was probably best to have
> both skip_to_optional_val() and skip_to_optional_val_default().

I guess we came to the same conclusion independently while our mails crossed ;-)

>   - 2 new functions instead of 1
>   - "optional" instead of "opt" in the function names

I thought that the more important part you agreed was s/val/arg/, though.

I had a few small comments on later steps, but I think these are moving in the right direction.

Thanks.
Previous: Junio C Hamano
Message 7 of 7 in “git-compat-util: introduce skip_to_optional_val()”
  1. 1/4 git-compat-util: introduce skip_to_optional_val()Christian Couder, Dec 4, 2017
  2. 2/4 index-pack: use skip_to_optional_val()Christian Couder, Dec 4, 2017
  3. 3/4 diff: use skip_to_optional_val()Christian Couder, Dec 4, 2017
  4. Junio C HamanoDec 4, 2017
  5. 4/4 diff: use skip_to_optional_val_default()Christian Couder, Dec 4, 2017
  6. Junio C HamanoDec 4, 2017
  7. Junio C HamanoDec 4, 2017

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.