git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Cc: git@vger.kernel.org, Matthieu Moy <Matthieu.Moy@grenoble-inp.fr>,
	Jeff King <peff@peff.net>,
	philipoakley@iee.org
Subject: Re: [PATCH 1/3] rev-parse: support OPT_NUMBER_CALLBACK in --parseopt
Date: Tue, 04 Mar 2014 10:28:22 -0800	[thread overview]
Message-ID: <xmqqiortlsxl.fsf@gitster.dls.corp.google.com> (raw)
In-Reply-To: <1393728794-29566-2-git-send-email-pclouds@gmail.com> ("Nguyễn	Thái Ngọc Duy"'s message of "Sun, 2 Mar 2014 09:53:12 +0700")

Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:

> If the option spec is
>
> -NUM Help string
>
> then rev-parse will accept and parse -([0-9]+) and return "-NUM $1"

Even though the hardcoded "NUM" token initially gave me a knee-jerk
"Yuck" reaction, that literal option name is very unlikely to be
desired by scripts/commands for their real option names, and being
in all uppercase it is very clear that it is magic convention
between the parsing mechanism and the script it uses.

It however felt funny to me without a matching (possibly hidden)
mechanism to allow parse-options machinery to consume such an output
as its input.  In a script that uses this mechanism to parse out the
numeric option "-NUM 3" out of "git script -3" and uses that "three"
to drive an underlying command (e.g. "git grep -3"), wouldn't it be
more natural if that underlying command can be told to accept the
same notation (i.e. "git grep -NUM 3")?  For that to be consistent
with the rest of the system, "-NUM" would not be a good token; being
it multi-character, it must be "--NUM" or something with double-dash
prefix.

I kind of like the basic idea, the capability it tries to give
scripted Porcelain implementations.  But my impression is that
"rebase -i -4", which this mechanism was invented for, is not
progressing, so perhaps we should wait until the real user of this
feature appears.

Thanks.

> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
> ---
>  builtin/rev-parse.c | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
> index 45901df..b37676f 100644
> --- a/builtin/rev-parse.c
> +++ b/builtin/rev-parse.c
> @@ -331,6 +331,8 @@ static int parseopt_dump(const struct option *o, const char *arg, int unset)
>  	struct strbuf *parsed = o->value;
>  	if (unset)
>  		strbuf_addf(parsed, " --no-%s", o->long_name);
> +	else if (o->type == OPTION_NUMBER)
> +		strbuf_addf(parsed, " -NUM");
>  	else if (o->short_name && (o->long_name == NULL || !stuck_long))
>  		strbuf_addf(parsed, " -%c", o->short_name);
>  	else
> @@ -338,7 +340,7 @@ static int parseopt_dump(const struct option *o, const char *arg, int unset)
>  	if (arg) {
>  		if (!stuck_long)
>  			strbuf_addch(parsed, ' ');
> -		else if (o->long_name)
> +		else if (o->long_name || o->type == OPTION_NUMBER)
>  			strbuf_addch(parsed, '=');
>  		sq_quote_buf(parsed, arg);
>  	}
> @@ -439,7 +441,10 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
>  
>  		if (s - sb.buf == 1) /* short option only */
>  			o->short_name = *sb.buf;
> -		else if (sb.buf[1] != ',') /* long option only */
> +		else if (s - sb.buf == 4 && !strncmp(sb.buf, "-NUM", 4)) {
> +			o->type = OPTION_NUMBER;
> +			o->flags = PARSE_OPT_NOARG | PARSE_OPT_NONEG;
> +		} else if (sb.buf[1] != ',') /* long option only */
>  			o->long_name = xmemdupz(sb.buf, s - sb.buf);
>  		else {
>  			o->short_name = *sb.buf;

  reply	other threads:[~2014-03-04 18:28 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-27 13:01 [PATCH/RFC] rebase: new convenient option to edit a single commit Nguyễn Thái Ngọc Duy
2014-02-27 13:52 ` Matthieu Moy
2014-02-28  6:58 ` Jeff King
2014-02-28  7:34   ` Duy Nguyen
2014-02-28  7:38     ` Jeff King
2014-02-28 17:14   ` Philip Oakley
2014-03-02  2:53 ` [PATCH 0/3] rebase's convenient options Nguyễn Thái Ngọc Duy
2014-03-02  2:53   ` [PATCH 1/3] rev-parse: support OPT_NUMBER_CALLBACK in --parseopt Nguyễn Thái Ngọc Duy
2014-03-04 18:28     ` Junio C Hamano [this message]
2014-03-02  2:53   ` [PATCH 2/3] rebase: accept -<number> as another way of saying HEAD~<number> Nguyễn Thái Ngọc Duy
2014-03-02  8:37     ` Eric Sunshine
2014-03-02  8:45       ` Duy Nguyen
2014-03-02  8:53     ` Eric Sunshine
2014-03-02  8:55       ` Eric Sunshine
2014-03-02 15:55         ` Matthieu Moy
2014-03-03  9:16           ` Michael Haggerty
2014-03-03  9:37             ` Matthieu Moy
2014-03-03 10:04               ` Duy Nguyen
2014-03-03 10:11                 ` David Kastrup
2014-03-03 10:12                 ` Matthieu Moy
2014-03-03 10:13               ` Jeff King
2014-03-03 21:48               ` Junio C Hamano
2014-03-03 22:39                 ` Matthieu Moy
2014-03-03 21:44             ` Junio C Hamano
2014-03-02  2:53   ` [PATCH 3/3] rebase: new convenient option to edit a single commit Nguyễn Thái Ngọc Duy
2014-03-02  9:04     ` Eric Sunshine
2014-03-02  9:09       ` Eric Sunshine
2014-03-03 10:10         ` Michael Haggerty
2014-03-03 10:15           ` Duy Nguyen
2014-03-03 10:37             ` David Kastrup
2014-03-03 20:28     ` Eric Sunshine
2014-03-04  2:08       ` Duy Nguyen
2014-03-04  8:59         ` Michael Haggerty
2014-03-04 10:24           ` Duy Nguyen
2014-03-04 13:11             ` Michael Haggerty
2014-03-04 18:37           ` Junio C Hamano
2014-03-09  2:49           ` [PATCH/RFC] rebase: new convenient option to edit/reword/delete " Nguyễn Thái Ngọc Duy
2014-03-09 16:30             ` Matthieu Moy
2014-03-10  8:30             ` Michael Haggerty
2014-03-10  8:41               ` Matthieu Moy

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

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqiortlsxl.fsf@gitster.dls.corp.google.com \
    --to=gitster@pobox.com \
    --cc=Matthieu.Moy@grenoble-inp.fr \
    --cc=git@vger.kernel.org \
    --cc=pclouds@gmail.com \
    --cc=peff@peff.net \
    --cc=philipoakley@iee.org \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).