Re: [PATCH] Make builtin-tag.c use parse_options.
From: Pierre Habouzit <hidden>
Date: 2016-06-15 22:43:49
On Sat, Nov 10, 2007 at 12:25:44PM +0000, Carlos Rica wrote:
2007/11/10, Junio C Hamano [off-list ref]:quoted
Carlos Rica [off-list ref] writes:
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 Attachments
- (unnamed) [application/pgp-signature] 189 bytes