# parse-options: ambiguous LASTARG_DEFAULT and OPTARG

9 messages from 2009-06-05 to 2009-06-12. Participants: Stephen Boyd, René Scharfe, Junio C Hamano, Pierre Habouzit.
Thread: https://gitlist.dev/t/19670

## Stephen Boyd, 2009-06-05 05:43

Subject: parse-options: ambiguous LASTARG_DEFAULT and OPTARG
Message-ID: <4A28B072.8030006@gmail.com>
URL: https://gitlist.dev/e/4A28B072.8030006%40gmail.com

```
Hi,

This in builtin-branch.c

        {
		OPTION_CALLBACK, 0, "merged", &merge_filter_ref,
		"commit", "print only merged branches",
		PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG,
		opt_parse_merge_filter, (intptr_t) "HEAD",
	},

and the usage message for "git-branch -h" will print out

    --merged <commit>

when I'm expecting

    --merged[=<commit>]

This is because the PARSE_OPT_OPTARG flag is not used. Is this correct?
The default value is still set correctly in some cases, but become
ambiguous in other cases. Take this for example

    $ git branch --merged --verbose
    fatal: malformed object name --verbose

but

    $ git branch --verbose --merged

works fine.

The simple fix is to just add PARSE_OPT_OPTARG to the flags, and fix a
test or two. But I'm wondering if doing that will become problematic for
end-users. Essentially you can no longer do git branch --merged master,
you must do git branch --merged=master.

```

## René Scharfe, 2009-06-06 10:30

Subject: Re: parse-options: ambiguous LASTARG_DEFAULT and OPTARG
Message-ID: <4A2A4534.80604@lsrfire.ath.cx>
URL: https://gitlist.dev/e/4A2A4534.80604%40lsrfire.ath.cx
In-Reply-To: <4A28B072.8030006@gmail.com>

```
Stephen Boyd schrieb:
> Hi,
> 
> This in builtin-branch.c
> 
>         {
> 		OPTION_CALLBACK, 0, "merged", &merge_filter_ref,
> 		"commit", "print only merged branches",
> 		PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG,
> 		opt_parse_merge_filter, (intptr_t) "HEAD",
> 	},
> 
> and the usage message for "git-branch -h" will print out
> 
>     --merged <commit>
> 
> when I'm expecting
> 
>     --merged[=<commit>]
> 
> This is because the PARSE_OPT_OPTARG flag is not used. Is this correct?

> The default value is still set correctly in some cases, but become
> ambiguous in other cases. Take this for example
> 
>     $ git branch --merged --verbose
>     fatal: malformed object name --verbose
> 
> but
> 
>     $ git branch --verbose --merged
> 
> works fine.
> 
> The simple fix is to just add PARSE_OPT_OPTARG to the flags, and fix a
> test or two. But I'm wondering if doing that will become problematic for
> end-users. Essentially you can no longer do git branch --merged master,
> you must do git branch --merged=master.

PARSE_OPT_OPTARG overrides PARSE_OPT_LASTARG_DEFAULT, as Pierre noted in
commit 1cc6985c, which introduced the latter, so the two should not be
used together.

PARSE_OPT_LASTARG_DEFAULT uses the default value if the option is the
last one on the command line and requires an explicit argument if it's
not the last, as you found out above.  That's also what the code says 
and its name implies; the comment in parse-options.h (by yours truly) is 
probably misleading because it doesn't mention this condition.

I don't remember any other program having options with such a behaviour; 
I'm not sure how to stress that --merged needs to be the last option, as 
implied by the help message.

```

## Stephen Boyd, 2009-06-06 20:14

Subject: Re: parse-options: ambiguous LASTARG_DEFAULT and OPTARG
Message-ID: <4A2ACE32.8080504@gmail.com>
URL: https://gitlist.dev/e/4A2ACE32.8080504%40gmail.com
In-Reply-To: <4A2A4534.80604@lsrfire.ath.cx>

```
René Scharfe wrote:
> PARSE_OPT_OPTARG overrides PARSE_OPT_LASTARG_DEFAULT, as Pierre noted in
> commit 1cc6985c, which introduced the latter, so the two should not be
> used together.

Ok, thanks. This means I used it wrong when I switched over show-branch
:-/ I'll have to send a follow-up patch for that.

> PARSE_OPT_LASTARG_DEFAULT uses the default value if the option is the
> last one on the command line and requires an explicit argument if it's
> not the last, as you found out above.  That's also what the code says
> and its name implies; the comment in parse-options.h (by yours truly)
> is probably misleading because it doesn't mention this condition.

I was mislead. When I read it I thought I had to use the flag to say
that the default value will be used in the case when no argument is
given. I completely ignored the LASTARG part (I thought it was
referencing the default arg). I think just adding what you said here to
parse-options.h will help others to avoid this.

> I don't remember any other program having options with such a
> behaviour; I'm not sure how to stress that --merged needs to be the
> last option, as implied by the help message.

"git tag --contains" is the same. Figuring out a way to say that the
syntax changes when it's the last option versus in the middle is not
obvious to me either.

```

## Stephen Boyd, 2009-06-07 23:39

Subject: [PATCH] show-branch: don't use LASTARG_DEFAULT with OPTARG
Message-ID: <1244417955-21226-1-git-send-email-bebarino@gmail.com>
URL: https://gitlist.dev/e/1244417955-21226-1-git-send-email-bebarino%40gmail.com
In-Reply-To: <4A2ACE32.8080504@gmail.com>

```
5734365 (show-branch: migrate to parse-options API 2009-05-21)
incorrectly set the --more option's flags to be
PARSE_OPT_LASTARG_DEFAULT and PARSE_OPT_OPTARG. These two flags
shouldn't be used together. An option taking a default should just set
the default value desired and parse options will take care of the rest.

Update the header comment to better convey this information.

Signed-off-by: Stephen Boyd <bebarino@gmail.com>
---
 builtin-show-branch.c |    3 +--
 parse-options.h       |    7 +++++--
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/builtin-show-branch.c b/builtin-show-branch.c
index 9433811..01bea3b 100644
--- a/builtin-show-branch.c
+++ b/builtin-show-branch.c
@@ -657,8 +657,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 			    "color '*!+-' corresponding to the branch"),
 		{ OPTION_INTEGER, 0, "more", &extra, "n",
 			    "show <n> more commits after the common ancestor",
-			    PARSE_OPT_OPTARG | PARSE_OPT_LASTARG_DEFAULT,
-			    NULL, (intptr_t)1 },
+			    PARSE_OPT_OPTARG, NULL, (intptr_t)1 },
 		OPT_SET_INT(0, "list", &extra, "synonym to more=-1", -1),
 		OPT_BOOLEAN(0, "no-name", &no_name, "suppress naming strings"),
 		OPT_BOOLEAN(0, "current", &with_current_branch,
diff --git a/parse-options.h b/parse-options.h
index b374ade..5653dba 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -71,8 +71,11 @@ typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
  *   PARSE_OPT_NONEG: says that this option cannot be negated
  *   PARSE_OPT_HIDDEN: this option is skipped in the default usage, and
  *                     shown only in the full usage.
- *   PARSE_OPT_LASTARG_DEFAULT: if no argument is given, the default value
- *                              is used.
+ *   PARSE_OPT_LASTARG_DEFAULT: says that this option will take the default
+ *				value if no argument is given when the option
+ *				is last on the command line. If the option is
+ *				not last it will require an argument.
+ *				Should not be used with PARSE_OPT_OPTARG.
  *   PARSE_OPT_NODASH: this option doesn't start with a dash.
  *   PARSE_OPT_LITERAL_ARGHELP: says that argh shouldn't be enclosed in brackets
  *				(i.e. '<argh>') in the help message.
-- 
1.6.3.2.202.g26c11

```

## René Scharfe, 2009-06-08 17:24

Subject: Re: [PATCH] show-branch: don't use LASTARG_DEFAULT with OPTARG
Message-ID: <4A2D494E.3030803@lsrfire.ath.cx>
URL: https://gitlist.dev/e/4A2D494E.3030803%40lsrfire.ath.cx
In-Reply-To: <1244417955-21226-1-git-send-email-bebarino@gmail.com>

```
Stephen Boyd schrieb:
> 5734365 (show-branch: migrate to parse-options API 2009-05-21)
> incorrectly set the --more option's flags to be
> PARSE_OPT_LASTARG_DEFAULT and PARSE_OPT_OPTARG. These two flags
> shouldn't be used together. An option taking a default should just set
> the default value desired and parse options will take care of the rest.
> 
> Update the header comment to better convey this information.

Thank you!

```

## Junio C Hamano, 2009-06-08 21:56

Subject: Re: [PATCH] show-branch: don't use LASTARG_DEFAULT with OPTARG
Message-ID: <7vd49ewfsi.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vd49ewfsi.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1244417955-21226-1-git-send-email-bebarino@gmail.com>

```
Stephen Boyd <bebarino@gmail.com> writes:

> 5734365 (show-branch: migrate to parse-options API 2009-05-21)
> incorrectly set the --more option's flags to be
> PARSE_OPT_LASTARG_DEFAULT and PARSE_OPT_OPTARG. These two flags
> shouldn't be used together. An option taking a default should just set
> the default value desired and parse options will take care of the rest.

Thanks.  Perhaps as a follow-up patch the runtime can check and barf when
parse_options() is called and finds this combination?

```

## Pierre Habouzit, 2009-06-09 08:23

Subject: [PATCH] parse-options: add parse_options_check to validate option specs.
Message-ID: <1244535824-11970-1-git-send-email-madcoder@debian.org>
URL: https://gitlist.dev/e/1244535824-11970-1-git-send-email-madcoder%40debian.org
In-Reply-To: <7vd49ewfsi.fsf@alter.siamese.dyndns.org>

```
It only searches for now for the dreaded LASTARG_DEFAULT | OPTARG
combination, but can be extended to check for any other forbidden
combination.

Options are checked each time we call parse_options_start.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
 parse-options.c |   24 ++++++++++++++++++++++++
 1 files changed, 24 insertions(+), 0 deletions(-)

diff --git a/parse-options.c b/parse-options.c
index e469fc0..34282ad 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -306,6 +306,28 @@ static void check_typos(const char *arg, const struct option *options)
 	}
 }
 
+static void parse_options_check(const struct option *opts)
+{
+	int err = 0;
+
+	for (; opts->type != OPTION_END; opts++) {
+		if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
+		    (opts->flags & PARSE_OPT_OPTARG)) {
+			if (opts->long_name) {
+				error("`--%s` uses incompatible flags "
+				      "LASTARG_DEFAULT and OPTARG", opts->long_name);
+			} else {
+				error("`-%c` uses incompatible flags "
+				      "LASTARG_DEFAULT and OPTARG", opts->short_name);
+			}
+			err |= 1;
+		}
+	}
+
+	if (err)
+		exit(129);
+}
+
 void parse_options_start(struct parse_opt_ctx_t *ctx,
 			 int argc, const char **argv, const char *prefix,
 			 int flags)
@@ -331,6 +353,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
 {
 	int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
 
+	parse_options_check(options);
+
 	/* we must reset ->opt, unknown short option leave it dangling */
 	ctx->opt = NULL;
 
-- 
1.6.3.2.323.gcd28f

```

## Pierre Habouzit, 2009-06-12 19:31

Subject: Re: [PATCH] parse-options: add parse_options_check to validate option specs.
Message-ID: <20090612193156.GC3129@artemis.corp>
URL: https://gitlist.dev/e/20090612193156.GC3129%40artemis.corp
In-Reply-To: <1244535824-11970-1-git-send-email-madcoder@debian.org>

```
On Tue, Jun 09, 2009 at 10:23:44AM +0200, Pierre Habouzit wrote:
> It only searches for now for the dreaded LASTARG_DEFAULT | OPTARG
> combination, but can be extended to check for any other forbidden
> combination.
> 
> Options are checked each time we call parse_options_start.
> 
> Signed-off-by: Pierre Habouzit <madcoder@debian.org>
> ---
>  parse-options.c |   24 ++++++++++++++++++++++++
>  1 files changed, 24 insertions(+), 0 deletions(-)

Has this patch been missed, or has it any kind of flaw that _I_ missed ?
:)

-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org

```

## René Scharfe, 2009-06-12 21:25

Subject: Re: [PATCH] parse-options: add parse_options_check to validate option specs.
Message-ID: <4A32C7E5.5090604@lsrfire.ath.cx>
URL: https://gitlist.dev/e/4A32C7E5.5090604%40lsrfire.ath.cx
In-Reply-To: <20090612193156.GC3129@artemis.corp>

```
Pierre Habouzit schrieb:
> On Tue, Jun 09, 2009 at 10:23:44AM +0200, Pierre Habouzit wrote:
>> It only searches for now for the dreaded LASTARG_DEFAULT | OPTARG
>> combination, but can be extended to check for any other forbidden
>> combination.
>>
>> Options are checked each time we call parse_options_start.
>>
>> Signed-off-by: Pierre Habouzit <madcoder@debian.org>
>> ---
>>  parse-options.c |   24 ++++++++++++++++++++++++
>>  1 files changed, 24 insertions(+), 0 deletions(-)
> 
> Has this patch been missed, or has it any kind of flaw that _I_ missed ?
> :)
> 

It's in master (cb9d398c).

```
