git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] parse-options: add PARSE_OPT_FAKELASTARG flag.

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 9, 2008, 01:15 UTC
Message-ID
<7vvdzfoo1s.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<20080708103408.GC19202@artemis.madism.org>
Pierre Habouzit <madcoder@debian.org> writes:
Show 16 quoted lines
> If you set this for a given flag, and the flag appears without a value on
> the command line, then the `defval' is used to fake a new argument.
>
> Note that this flag is meaningless in presence of OPTARG or NOARG flags.
> (in the current implementation it will be ignored, but don't rely on it).
>
> Signed-off-by: Pierre Habouzit <madcoder@debian.org>
> ---
>
>     >   (3) inspired from (1) and (2), have a flag for options that says
>     >       "I do take an argument, but if I'm the last option on the
>     >       command line, please fake this argument for me.
>     >
>     > I really like (3) more FWIW as it doesn't generate ambiguous
>     > parsers like (2) would, and it's not horrible like (1). And cherry
>     > on top it's pretty trivial to implement I think.

Yeah, I do not particularly want a major rewrite that only introduces possible ambiguity to the option parser (even though arguably it would add to the usability very much, just like we accept revs and paths when unambiguous), so I think this is a reasonable compromise.

It feels more like LASTARG_DEFAULT but that is bikeshedding ;-)

But I see one thinko (fix below) and another issue I am not sure what the best fix would be.

---
diff --git a/parse-options.c b/parse-options.c
index b6735a5..cba20d7 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -26,11 +26,11 @@ static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
 	if (p->opt) {
 		*arg = p->opt;
 		p->opt = NULL;
+	} else if (p->argc == 1 && (opt->flags & PARSE_OPT_FAKELASTARG)) {
+		*arg = (const char *)opt->defval;
 	} else if (p->argc) {
 		p->argc--;
 		*arg = *++p->argv;
-	} else if (opt->flags & PARSE_OPT_FAKELASTARG) {
-		*arg = (const char *)opt->defval;
 	} else
 		return opterror(opt, "requires a value", flags);
 	return 0;
Previous: Pierre HabouzitNext: Junio C Hamano
Message 4 of 9 in “What should "git branch --merged master" do?”
  1. Junio C HamanoJul 8, 2008
  2. Pierre HabouzitJul 8, 2008
  3. parse-options: add PARSE_OPT_FAKELASTARG flag.Pierre Habouzit, Jul 8, 2008
  4. Junio C HamanoJul 9, 2008
  5. 1/2 branch --contains: default to HEADJunio C Hamano, Jul 9, 2008
  6. 2/2 branch --merged/--not-merged: allow specifying arbitrary commitJunio C Hamano, Jul 9, 2008
  7. Pierre HabouzitJul 9, 2008
  8. Junio C HamanoJul 9, 2008
  9. Pierre HabouzitJul 9, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.