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

Re: [PATCH] Make builtin-tag.c use parse_options.

From
Pierre Habouzit <madcoder@debian.org>
Date
Nov 10, 2007, 13:13 UTC
Message-ID
<20071110131327.GC25204@artemis.corp>
In-Reply-To
<1b46aba20711100425o2f351ac5o81537adc6f09dc80@mail.gmail.com>
On Sat, Nov 10, 2007 at 12:25:44PM +0000, Carlos Rica wrote:
> 2007/11/10, Junio C Hamano <gitster@pobox.com>:
> > Carlos Rica <jasampler@gmail.com> writes:
Show 8 quoted lines
> A solution not needing memory allocation into the option parser
> could be setting a callback running over the repeated option
> arguments, passing them to the function one per each call.
> Then, the user will be able to decide if he wants the arguments
> concatenated or only need one of them and prefers erroring out.
> 
> Is this already possible with the current parser or the callback
> mode only calls using the last option?
  Everything is possible, you just have to code it. With a callback
you have in the struct option two places to store "things". The void*
value pointer and the intptr_t defval. _Usually_ the void* is the
pointer to the data that will be _written_ and the defval the data that
will be put into the void* under some circumstances (e.g. when your
option is negated).
  For Your case I'd go with some kind of string list pointed into the
void * value, defval has no or little use. You don't really care about
allocating memory in the option parser, I mean, option parsing is done
once at the initialization phase. It's not evil. In pseudo-C here is how
I would write the callback:
int parse_opt_stringlist(const struct option *opt, const char *arg, int unset)
{
    string_list **l = opt->value;
    string_list_elem *e;
    if (unset) { /* negationg option clears the list */
	while (*l) {
	    string_list_elem_free(string_list_pop(l));
	}
	return 0;
    }
    e = string_list_elem_new();
    e->data = arg;
    string_list_push(l, e);
    return 0;
}
  And you're done, you can do what you want with that list from the caller.
There probably is such a structure in git, if not, it can probably be hacked
in a few lines.
  Remember, callbacks give you _full_ control on what you can do in the option
parser, and if you're not happy with Turing complete expressivity, there isn't
anything I can do for you :P Note that if you do write such a generic
callback, it belongs to parse-options.[hc].
-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org
Previous: Carlos RicaNext: Carlos Rica
Message 8 of 11 in “Make builtin-tag.c use parse_options.”
  1. Make builtin-tag.c use parse_options.Carlos Rica, Nov 9, 2007
  2. Jakub NarebskiNov 9, 2007
  3. Johannes SchindelinNov 9, 2007
  4. Junio C HamanoNov 10, 2007
  5. Junio C HamanoNov 10, 2007
  6. Junio C HamanoNov 10, 2007
  7. Carlos RicaNov 10, 2007
  8. Pierre HabouzitNov 10, 2007
  9. Carlos RicaNov 12, 2007
  10. Pierre HabouzitNov 12, 2007
  11. Kristian HøgsbergNov 12, 2007

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.