parseopt: add OPT_NUMBER_CALLBACK

Add a way to recognize numerical options.  The number is passed to
a callback function as a string.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
René Scharfe
2009-05-07 21:45:08 +02:00
committed by Junio C Hamano
parent 2f4b97f910
commit e0319ff5ed
5 changed files with 63 additions and 1 deletions

View File

@ -19,6 +19,12 @@ int length_callback(const struct option *opt, const char *arg, int unset)
return 0;
}
int number_callback(const struct option *opt, const char *arg, int unset)
{
*(int *)opt->value = strtol(arg, NULL, 10);
return 0;
}
int main(int argc, const char **argv)
{
const char *usage[] = {
@ -46,6 +52,8 @@ int main(int argc, const char **argv)
"set string to default", (unsigned long)"default"),
OPT_GROUP("Magic arguments"),
OPT_ARGUMENT("quux", "means --quux"),
OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
number_callback),
OPT_GROUP("Standard options"),
OPT__ABBREV(&abbrev),
OPT__VERBOSE(&verbose),