From: Jeff King <peff@peff.net>
To: Pranit Bauva <pranit.bauva@gmail.com>
Cc: Git List <git@vger.kernel.org>
Subject: Re: parse-options does not recognize "unspecified" behavior
Date: Wed, 16 Mar 2016 16:49:13 -0400 [thread overview]
Message-ID: <20160316204912.GA1890@sigill.intra.peff.net> (raw)
In-Reply-To: <CAFZEwPPd2wFqFq2LFEzN2CzhTV6C420SLPcXi1SWE=z2epOYLw@mail.gmail.com>
On Thu, Mar 17, 2016 at 01:21:49AM +0530, Pranit Bauva wrote:
> I noticed that parse-options does not recognize the variable which is
> set to -1 so as to denote the "unspecified" value.
Right. Like all of the stock parse-options handlers, it does not ever
read or understand the value passed to it by the caller. It only
increments or decrements.
> I did the following changes in builtin/commit.c (in master branch not
> the patch I am working on) :
> - static int verbose = -1
> - introduced a printf statement after parsing the options to print
> the value of verbose.
>
> When I ran `git commit` :
> I get the output that verbose is set to -1.
>
> When I ran `git commit -v` :
> I get the output that verbose is set to 0.
>
> When I ran `git commit -v -v` :
> I get the output that verbose is set to 1.
>
> When I ran `git commit --no-verbose` :
> I get the out that verbose is set to 0.
> [...]
> It seems that parse-options just increments the value without
> considering the -1 flag to denote "unspecified value".
>
> Is this a bug?
Not in parse-options, though I think setting verbose to "-1" in the
first place is wrong.
In general, parse-options does not know or care about the default values
that callers assign to variables; it just writes to them based on the
option-type specified by the caller. So the behavior for "commit",
"commit -v", and "commit -v -v" you show are perfectly reasonable.
But the one for "--no-verbose" is wrong. Parse-options has to write some
"reset" value, and it does not know what the initial default was. So it
writes 0. This is the same for options like OPT_SET_INT, and similar for
string options (where we set it to NULL).
So I think the caller choosing "-1" here as the "not set" value is the
bug.
-Peff
next prev parent reply other threads:[~2016-03-16 20:49 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-03-16 19:51 parse-options does not recognize "unspecified" behavior Pranit Bauva
2016-03-16 20:49 ` Jeff King [this message]
2016-03-16 21:06 ` Pranit Bauva
2016-03-16 21:23 ` Jeff King
2016-03-16 21:37 ` Eric Sunshine
2016-03-16 21:41 ` Pranit Bauva
2016-03-16 21:44 ` Jeff King
2016-03-16 21:53 ` Stefan Beller
2016-03-16 23:16 ` Jeff King
2016-03-16 23:33 ` Stefan Beller
2016-03-17 1:43 ` Jeff King
2016-03-17 5:32 ` Eric Sunshine
2016-03-17 5:40 ` Jeff King
2016-03-19 11:25 ` SZEDER Gábor
2016-03-19 16:55 ` Pranit Bauva
2016-03-25 14:58 ` SZEDER Gábor
2016-03-25 15:03 ` Pranit Bauva
2016-03-16 21:37 ` Pranit Bauva
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=20160316204912.GA1890@sigill.intra.peff.net \
--to=peff@peff.net \
--cc=git@vger.kernel.org \
--cc=pranit.bauva@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.
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).