git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* parse-options: ambiguous LASTARG_DEFAULT and OPTARG
@ 2009-06-05  5:43 Stephen Boyd
  2009-06-06 10:30 ` René Scharfe
  0 siblings, 1 reply; 9+ messages in thread
From: Stephen Boyd @ 2009-06-05  5:43 UTC (permalink / raw)
  To: git list; +Cc: René Scharfe, Junio C Hamano

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.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: parse-options: ambiguous LASTARG_DEFAULT and OPTARG
  2009-06-05  5:43 parse-options: ambiguous LASTARG_DEFAULT and OPTARG Stephen Boyd
@ 2009-06-06 10:30 ` René Scharfe
  2009-06-06 20:14   ` Stephen Boyd
  0 siblings, 1 reply; 9+ messages in thread
From: René Scharfe @ 2009-06-06 10:30 UTC (permalink / raw)
  To: Stephen Boyd; +Cc: git list, Junio C Hamano, Pierre Habouzit

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.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: parse-options: ambiguous LASTARG_DEFAULT and OPTARG
  2009-06-06 10:30 ` René Scharfe
@ 2009-06-06 20:14   ` Stephen Boyd
  2009-06-07 23:39     ` [PATCH] show-branch: don't use LASTARG_DEFAULT with OPTARG Stephen Boyd
  0 siblings, 1 reply; 9+ messages in thread
From: Stephen Boyd @ 2009-06-06 20:14 UTC (permalink / raw)
  To: René Scharfe; +Cc: git list, Junio C Hamano, Pierre Habouzit

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.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH] show-branch: don't use LASTARG_DEFAULT with OPTARG
  2009-06-06 20:14   ` Stephen Boyd
@ 2009-06-07 23:39     ` Stephen Boyd
  2009-06-08 17:24       ` René Scharfe
  2009-06-08 21:56       ` Junio C Hamano
  0 siblings, 2 replies; 9+ messages in thread
From: Stephen Boyd @ 2009-06-07 23:39 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git list, René Scharfe, Pierre Habouzit

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

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] show-branch: don't use LASTARG_DEFAULT with OPTARG
  2009-06-07 23:39     ` [PATCH] show-branch: don't use LASTARG_DEFAULT with OPTARG Stephen Boyd
@ 2009-06-08 17:24       ` René Scharfe
  2009-06-08 21:56       ` Junio C Hamano
  1 sibling, 0 replies; 9+ messages in thread
From: René Scharfe @ 2009-06-08 17:24 UTC (permalink / raw)
  To: Stephen Boyd; +Cc: Junio C Hamano, git list, Pierre Habouzit

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!

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] show-branch: don't use LASTARG_DEFAULT with OPTARG
  2009-06-07 23:39     ` [PATCH] show-branch: don't use LASTARG_DEFAULT with OPTARG Stephen Boyd
  2009-06-08 17:24       ` René Scharfe
@ 2009-06-08 21:56       ` Junio C Hamano
  2009-06-09  8:23         ` [PATCH] parse-options: add parse_options_check to validate option specs Pierre Habouzit
  1 sibling, 1 reply; 9+ messages in thread
From: Junio C Hamano @ 2009-06-08 21:56 UTC (permalink / raw)
  To: Stephen Boyd; +Cc: git list, René Scharfe, Pierre Habouzit

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?

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH] parse-options: add parse_options_check to validate option specs.
  2009-06-08 21:56       ` Junio C Hamano
@ 2009-06-09  8:23         ` Pierre Habouzit
  2009-06-12 19:31           ` Pierre Habouzit
  0 siblings, 1 reply; 9+ messages in thread
From: Pierre Habouzit @ 2009-06-09  8:23 UTC (permalink / raw)
  To: git; +Cc: Pierre Habouzit, Junio C Hamano, René Scharfe

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

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] parse-options: add parse_options_check to validate option specs.
  2009-06-09  8:23         ` [PATCH] parse-options: add parse_options_check to validate option specs Pierre Habouzit
@ 2009-06-12 19:31           ` Pierre Habouzit
  2009-06-12 21:25             ` René Scharfe
  0 siblings, 1 reply; 9+ messages in thread
From: Pierre Habouzit @ 2009-06-12 19:31 UTC (permalink / raw)
  To: Pierre Habouzit; +Cc: git, Junio C Hamano, René Scharfe

[-- Attachment #1: Type: text/plain, Size: 718 bytes --]

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

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] parse-options: add parse_options_check to validate option specs.
  2009-06-12 19:31           ` Pierre Habouzit
@ 2009-06-12 21:25             ` René Scharfe
  0 siblings, 0 replies; 9+ messages in thread
From: René Scharfe @ 2009-06-12 21:25 UTC (permalink / raw)
  To: Pierre Habouzit; +Cc: Pierre Habouzit, git, Junio C Hamano

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).

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2009-06-12 21:26 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-06-05  5:43 parse-options: ambiguous LASTARG_DEFAULT and OPTARG Stephen Boyd
2009-06-06 10:30 ` René Scharfe
2009-06-06 20:14   ` Stephen Boyd
2009-06-07 23:39     ` [PATCH] show-branch: don't use LASTARG_DEFAULT with OPTARG Stephen Boyd
2009-06-08 17:24       ` René Scharfe
2009-06-08 21:56       ` Junio C Hamano
2009-06-09  8:23         ` [PATCH] parse-options: add parse_options_check to validate option specs Pierre Habouzit
2009-06-12 19:31           ` Pierre Habouzit
2009-06-12 21:25             ` René Scharfe

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).