From: Jeff King <peff@peff.net>
To: "SZEDER Gábor" <szeder.dev@gmail.com>
Cc: Junio C Hamano <gitster@pobox.com>, git@vger.kernel.org
Subject: Re: [PATCH 2/3] revision.c: use skip_prefix() in handle_revision_opt()
Date: Sat, 10 Jun 2017 02:35:19 -0400 [thread overview]
Message-ID: <20170610063518.6ihim2za6vil24pt@sigill.intra.peff.net> (raw)
In-Reply-To: <20170609181733.6793-1-szeder.dev@gmail.com>
On Fri, Jun 09, 2017 at 08:17:28PM +0200, SZEDER Gábor wrote:
> > would let us do:
> >
> > if (match_opt(arg, "--early-output"), &optarg)) {
> > int count = optarg ? atoi(optarg) : 100;
> > ...
> > }
> >
> > which is a little nicer and could maybe help other options (I didn't see
> > any, though).
>
> Besides '--show-linear-break' and '--pretty', other options that could
> benefit from this, i.e. long options with an optional argument, are
> '--expand-tabs', '--abbrev' and '--no-walk'. These are handled
> differently than '--early--output' and '--show-linear-break': each is
> covered by two if branches, one with and one without the optional
> argument, i.e.:
>
> } else if (!strcmp(arg, "--option")) {
> ...
> } else if (starts_with(arg, "--option=")) {
> ...
> } else ...
I think those multi-branch cases end up as an improvement with a helper:
if (match_opt(arg, "--no-walk", &optarg)) {
if (!optarg || !strcmp(optarg, "sorted"))
revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
else if (!strcmp(optarg, "unsorted"))
revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
else
return error(...);
}
> '--pretty=' couldn't benefit, though, because it is special in that
> it's equivalent with '--format=', and the two are handled in the same
> branch.
I think you could still handle them both in the same branch, like:
if (match_opt(arg, "--pretty", &optarg) ||
skip_prefix(arg, "--format=", &optarg)) {
revs->verbose_header = 1;
revs->pretty-given = 1;
/* OK to pass NULL for --pretty case */
get_commit_format(optarg, revs);
}
> So inherently there are a few repeated option names and variable
> assignments, and that's not so good. However, refactoring these to
> use match_opt() adds 40% more lines than it removes and, more
> importantly, increases the number of nested conditions. Subjectively
> I don't think it's better, so I went with the "follow the conventions
> of the surrounding code" rule for the update.
I care less about lines of boilerplate code and more about repeated
logic. In the --pretty example above, the first two lines of the block
are common to both --pretty and --pretty=. If they ever need to change,
somebody has to update two spots.
Anyway. I certainly don't insist on you working on this, especially if
you don't agree with the aesthetics. Just fixing the actual bugs would
be sufficient for my review. ;)
> As far as I can tell, parse-options doesn't handle options with an
> optional argument by itself, but only with callback functions, so it
> is no help here as it is.
There's a flag, PARSE_OPT_OPTARG, which would do what you want. But I
agree that converting the whole thing to parse-options would be a lot of
work (quite a few of these really aren't just "this is a string", but
would need independent callback functions.
-Peff
next prev parent reply other threads:[~2017-06-10 6:35 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-06-02 19:10 [PATCH 0/3] Use skip_prefix() in handle_revision_{,pseudo_}opt() SZEDER Gábor
2017-06-02 19:10 ` [PATCH 1/3] revision.c: stricter parsing of '--no-{min,max}-parents' SZEDER Gábor
2017-06-02 19:10 ` [PATCH 2/3] revision.c: use skip_prefix() in handle_revision_opt() SZEDER Gábor
2017-06-02 20:11 ` Jeff King
2017-06-02 20:15 ` Jeff King
2017-06-09 18:17 ` SZEDER Gábor
2017-06-09 18:17 ` [PATCHv2 1/5] revision.h: turn rev_info.early_output back into an unsigned int SZEDER Gábor
2017-06-10 6:41 ` Jeff King
2017-06-10 11:41 ` [PATCHv2.1] " SZEDER Gábor
2017-06-12 21:30 ` Jeff King
2017-06-12 20:36 ` [PATCHv2 1/5] " Junio C Hamano
2017-06-12 21:59 ` Jeff King
2017-06-13 0:50 ` Jeff King
2017-06-09 18:17 ` [PATCHv2 2/5] revision.c: stricter parsing of '--no-{min,max}-parents' SZEDER Gábor
2017-06-09 18:17 ` [PATCHv2 3/5] revision.c: stricter parsing of '--early-output' SZEDER Gábor
2017-06-10 4:52 ` Junio C Hamano
2017-06-09 18:17 ` [PATCHv2 4/5] revision.c: use skip_prefix() in handle_revision_opt() SZEDER Gábor
2017-06-09 18:17 ` [PATCHv2 5/5] revision.c: use skip_prefix() in handle_revision_pseudo_opt() SZEDER Gábor
2017-06-10 6:35 ` Jeff King [this message]
2017-06-10 6:44 ` [PATCH 2/3] revision.c: use skip_prefix() in handle_revision_opt() Jeff King
2017-06-02 19:10 ` [PATCH 3/3] revision.c: use skip_prefix() in handle_revision_pseudo_opt() SZEDER Gábor
2017-06-02 20:17 ` [PATCH 0/3] Use skip_prefix() in handle_revision_{,pseudo_}opt() Jeff King
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=20170610063518.6ihim2za6vil24pt@sigill.intra.peff.net \
--to=peff@peff.net \
--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.
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).