From d89e5744f30b584ac4909ce1164af1289c41359b Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sun, 3 Oct 2010 22:23:29 +1030 Subject: [PATCH] opt: Put actual options inside names. This is more explicit than separate short and long (great for grep!) and simpler. --- ccan/opt/opt.c | 181 ++++++++++++++++++++++++++------- ccan/opt/opt.h | 85 ++++++++-------- ccan/opt/private.h | 7 +- ccan/opt/test/run-helpers.c | 26 ++--- ccan/opt/test/run-iter.c | 76 ++++++++++++++ ccan/opt/test/run-no-options.c | 28 +++++ ccan/opt/test/run-usage.c | 19 ++-- ccan/opt/test/run.c | 12 +-- ccan/opt/test/utils.c | 20 ++-- ccan/opt/usage.c | 39 ++----- 10 files changed, 346 insertions(+), 147 deletions(-) create mode 100644 ccan/opt/test/run-iter.c create mode 100644 ccan/opt/test/run-no-options.c diff --git a/ccan/opt/opt.c b/ccan/opt/opt.c index 236d2af0..155d7aaf 100644 --- a/ccan/opt/opt.c +++ b/ccan/opt/opt.c @@ -11,15 +11,117 @@ #include "private.h" struct opt_table *opt_table; -unsigned int opt_count; +unsigned int opt_count, opt_num_short, opt_num_short_arg, opt_num_long; const char *opt_argv0; +static const char *first_name(const char *names, unsigned *len) +{ + *len = strcspn(names + 1, "/"); + return names + 1; +} + +static const char *next_name(const char *names, unsigned *len) +{ + names += *len; + if (!names[0]) + return NULL; + return first_name(names + 1, len); +} + +/* FIXME: Combine with next_opt */ +static const char *first_opt(bool want_long, unsigned *i, unsigned *len) +{ + const char *p; + for (*i = 0; *i < opt_count; (*i)++) { + if (opt_table[*i].flags == OPT_SUBTABLE) + continue; + for (p = first_name(opt_table[*i].names, len); + p; + p = next_name(p, len)) { + if ((p[0] == '-') == want_long) { + if (want_long) { + /* Skip leading "-" */ + (*len)--; + p++; + } + return p; + } + } + } + return NULL; +} + +static const char *next_opt(const char *names, bool want_long, + unsigned *i, unsigned *len) +{ + const char *p = next_name(names, len); + for (;;) { + while (p) { + if ((p[0] == '-') == want_long) { + if (want_long) { + /* Skip leading "-" */ + (*len)--; + p++; + } + return p; + } + p = next_name(p, len); + } + do { + (*i)++; + } while (*i < opt_count && opt_table[*i].flags == OPT_SUBTABLE); + if (*i == opt_count) + return NULL; + p = first_name(opt_table[*i].names, len); + } +} + +static const char *first_lopt(unsigned *i, unsigned *len) +{ + return first_opt(true, i, len); +} + +static const char *next_lopt(const char *names, unsigned *i, unsigned *len) +{ + return next_opt(names, true, i, len); +} + +const char *first_sopt(unsigned *i) +{ + unsigned unused_len; + return first_opt(false, i, &unused_len); +} + +const char *next_sopt(const char *names, unsigned *i) +{ + unsigned unused_len = 1; + + return next_opt(names, false, i, &unused_len); +} + static void check_opt(const struct opt_table *entry) { + const char *p; + unsigned len; + assert(entry->flags == OPT_HASARG || entry->flags == OPT_NOARG); - assert(entry->shortopt || entry->longopt); - assert(entry->shortopt != ':'); - assert(entry->shortopt != '?' || entry->flags == OPT_NOARG); + + assert(entry->names[0] == '-'); + for (p = first_name(entry->names, &len); p; p = next_name(p, &len)) { + if (*p == '-') { + assert(len > 1); + opt_num_long++; + } else { + assert(len == 1); + assert(*p != ':'); + opt_num_short++; + if (entry->flags == OPT_HASARG) { + opt_num_short_arg++; + /* FIXME: -? with ops breaks getopt_long */ + assert(*p != '?'); + } + } + } } static void add_opt(const struct opt_table *entry) @@ -28,15 +130,14 @@ static void add_opt(const struct opt_table *entry) opt_table[opt_count++] = *entry; } -void _opt_register(const char *longopt, char shortopt, enum opt_flags flags, +void _opt_register(const char *names, enum opt_flags flags, char *(*cb)(void *arg), char *(*cb_arg)(const char *optarg, void *arg), void (*show)(char buf[OPT_SHOW_LEN], const void *arg), void *arg, const char *desc) { struct opt_table opt; - opt.longopt = longopt; - opt.shortopt = shortopt; + opt.names = names; opt.flags = flags; opt.cb = cb; opt.cb_arg = cb_arg; @@ -71,61 +172,66 @@ void opt_register_table(const struct opt_table entry[], const char *desc) static char *make_optstring(void) { - /* Worst case, each one is ":x:", plus nul term. */ - char *str = malloc(1 + opt_count * 2 + 1); - unsigned int num, i; + char *str = malloc(1 + opt_num_short + opt_num_short_arg + 1); + const char *p; + unsigned int i, num = 0; /* This tells getopt_long we want a ':' returned for missing arg. */ - str[0] = ':'; - num = 1; - for (i = 0; i < opt_count; i++) { - if (!opt_table[i].shortopt) - continue; - str[num++] = opt_table[i].shortopt; + str[num++] = ':'; + for (p = first_sopt(&i); p; p = next_sopt(p, &i)) { + str[num++] = *p; if (opt_table[i].flags == OPT_HASARG) str[num++] = ':'; } - str[num] = '\0'; + str[num++] = '\0'; + assert(num == 1 + opt_num_short + opt_num_short_arg + 1); return str; } static struct option *make_options(void) { - struct option *options = malloc(sizeof(*options) * (opt_count + 1)); - unsigned int i, num; + struct option *options = malloc(sizeof(*options) * (opt_num_long + 1)); + unsigned int i, num = 0, len; + const char *p; - for (num = i = 0; i < opt_count; i++) { - if (!opt_table[i].longopt) - continue; - options[num].name = opt_table[i].longopt; + for (p = first_lopt(&i, &len); p; p = next_lopt(p, &i, &len)) { + char *buf = malloc(len + 1); + memcpy(buf, p, len); + buf[len] = 0; + options[num].name = buf; options[num].has_arg = (opt_table[i].flags == OPT_HASARG); options[num].flag = NULL; options[num].val = 0; num++; } memset(&options[num], 0, sizeof(options[num])); + assert(num == opt_num_long); return options; } static struct opt_table *find_short(char shortopt) { unsigned int i; - for (i = 0; i < opt_count; i++) { - if (opt_table[i].shortopt == shortopt) + const char *p; + + for (p = first_sopt(&i); p; p = next_sopt(p, &i)) { + if (*p == shortopt) return &opt_table[i]; } abort(); } /* We want the index'th long entry. */ -static struct opt_table *find_long(int index) +static struct opt_table *find_long(int index, const char **name) { - unsigned int i; - for (i = 0; i < opt_count; i++) { - if (!opt_table[i].longopt) - continue; - if (index == 0) + unsigned int i, len; + const char *p; + + for (p = first_lopt(&i, &len); p; p = next_lopt(p, &i, &len)) { + if (index == 0) { + *name = p; return &opt_table[i]; + } index--; } abort(); @@ -144,7 +250,8 @@ static void parse_fail(void (*errlog)(const char *fmt, ...), if (shortopt) errlog("%s: -%c: %s", opt_argv0, shortopt, problem); else - errlog("%s: --%s: %s", opt_argv0, longopt, problem); + errlog("%s: --%.*s: %s", opt_argv0, + strcspn(longopt, "/"), longopt, problem); } void dump_optstate(void); @@ -168,10 +275,11 @@ bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...)) /* Reset in case we're called more than once. */ optopt = 0; - optind = 1; + optind = 0; while ((ret = getopt_long(*argc, argv, optstring, options, &longidx)) != -1) { char *problem; + const char *name; bool missing = false; /* optopt is 0 if it's an unknown long option, *or* if @@ -190,11 +298,11 @@ bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...)) if (ret != 0) e = find_short(ret); else - e = find_long(longidx); + e = find_long(longidx, &name); /* Missing argument */ if (missing) { - parse_fail(errlog, e->shortopt, e->longopt, + parse_fail(errlog, ret, name, "option requires an argument"); break; } @@ -205,8 +313,7 @@ bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...)) problem = e->cb(e->arg); if (problem) { - parse_fail(errlog, e->shortopt, e->longopt, - problem); + parse_fail(errlog, ret, name, problem); free(problem); break; } diff --git a/ccan/opt/opt.h b/ccan/opt/opt.h index 74bda85b..253d03c4 100644 --- a/ccan/opt/opt.h +++ b/ccan/opt/opt.h @@ -16,8 +16,7 @@ enum opt_flags { #define OPT_SHOW_LEN 80 struct opt_table { - const char *longopt; /* --longopt, or NULL */ - char shortopt; /* -s, or 0 */ + const char *names; /* slash-separated names, --longopt or -s */ enum opt_flags flags; char *(*cb)(void *arg); /* OPT_NOARG */ char *(*cb_arg)(const char *optarg, void *arg); /* OPT_HASARG */ @@ -28,8 +27,7 @@ struct opt_table { /** * OPT_WITHOUT_ARG() - macro for initializing an opt_table entry (without arg) - * @longopt: the name of the argument (eg. "foo" for "--foo"), or NULL. - * @shortopt: the character of the argument (eg. 'f' for "-f"), or 0. + * @names: the names of the option eg. "--foo", "-f" or "--foo/-f/--foobar". * @cb: the callback when the option is found. * @arg: the argument to hand to @cb. * @@ -37,20 +35,19 @@ struct opt_table { * of type "char *cb(type *)", "char *cb(const type *)" or "char *cb(void *)", * where "type" is the type of the @arg argument. * - * At least one of @longopt and @shortopt must be non-zero. If the - * @cb returns non-NULL, opt_parse() will stop parsing, use the returned - * string to form an error message, free() the string and return false. + * If the @cb returns non-NULL, opt_parse() will stop parsing, use the + * returned string to form an error message for errlog(), free() the + * string and return false. * * See Also: * OPT_WITH_ARG() */ -#define OPT_WITHOUT_ARG(longopt, shortopt, cb, arg) \ - (longopt), (shortopt), OPT_CB_NOARG((cb), (arg)) +#define OPT_WITHOUT_ARG(names, cb, arg) \ + (names), OPT_CB_NOARG((cb), (arg)) /** * OPT_WITH_ARG() - macro for initializing long and short option (with arg) - * @longopt: the name of the argument (eg. "foo" for "--foo "), or NULL. - * @shortopt: the character of the argument (eg. 'f' for "-f "), or 0. + * @names: the names of the option eg. "--foo", "-f" or "--foo/-f/--foobar". * @cb: the callback when the option is found (along with ). * @show: the callback to print the value in get_usage (or NULL) * @arg: the argument to hand to @cb and @show @@ -66,41 +63,31 @@ struct opt_table { * argument; unless it uses the entire OPT_SHOW_LEN bytes it should * nul-terminate that buffer. * - * At least one of @longopt and @shortopt must be non-zero. If the - * @cb returns false, opt_parse() will stop parsing and return false. + * If the @cb returns non-NULL, opt_parse() will stop parsing, use the + * returned string to form an error message for errlog(), free() the + * string and return false. * * See Also: * OPT_WITHOUT_ARG() */ -#define OPT_WITH_ARG(longopt, shortopt, cb, show, arg) \ - (longopt), (shortopt), OPT_CB_ARG((cb), (show), (arg)) +#define OPT_WITH_ARG(name, cb, show, arg) \ + (name), OPT_CB_ARG((cb), (show), (arg)) /** * OPT_SUBTABLE() - macro for including another table inside a table. * @table: the table to include in this table. * @desc: description of this subtable (for opt_usage()) or NULL. - * - * The @desc field can be opt_table_hidden to hide the options from opt_usage(). */ #define OPT_SUBTABLE(table, desc) \ - { (const char *)(table), sizeof(_check_is_entry(table)), \ - OPT_SUBTABLE, NULL, NULL, NULL, NULL, (desc) } - -/** - * opt_table_hidden - string for undocumented option tables. - * - * This can be used as the desc option to OPT_SUBTABLE or passed to - * opt_register_table() if you want the options not to be shown by - * opt_usage(). - */ -extern const char opt_table_hidden[]; + { (const char *)(table), OPT_SUBTABLE, \ + sizeof(_check_is_entry(table)) ? NULL : NULL, NULL, NULL, NULL, (desc) } /** * OPT_ENDTABLE - macro to create final entry in table. * * This must be the final element in the opt_table array. */ -#define OPT_ENDTABLE { NULL, 0, OPT_END } +#define OPT_ENDTABLE { NULL, OPT_END } /** * opt_register_table - register a table of options @@ -111,9 +98,11 @@ extern const char opt_table_hidden[]; * * Example: * static struct opt_table opts[] = { - * { OPT_WITHOUT_ARG("verbose", 'v', opt_inc_intval, &verbose), + * { OPT_WITHOUT_ARG("--verbose", opt_inc_intval, &verbose), * "Verbose mode (can be specified more than once)" }, - * { OPT_WITHOUT_ARG("usage", 0, opt_usage_and_exit, + * { OPT_WITHOUT_ARG("-v", opt_inc_intval, &verbose), + * "Verbose mode (can be specified more than once)" }, + * { OPT_WITHOUT_ARG("--usage", opt_usage_and_exit, * "args...\nA silly test program."), * "Print this message." }, * OPT_ENDTABLE @@ -126,8 +115,7 @@ void opt_register_table(const struct opt_table table[], const char *desc); /** * opt_register_noarg - register an option with no arguments - * @longopt: the name of the argument (eg. "foo" for "--foo"), or NULL. - * @shortopt: the character of the argument (eg. 'f' for "-f"), or 0. + * @names: the names of the option eg. "--foo", "-f" or "--foo/-f/--foobar". * @cb: the callback when the option is found. * @arg: the argument to hand to @cb. * @desc: the verbose desction of the option (for opt_usage()), or NULL. @@ -139,16 +127,16 @@ void opt_register_table(const struct opt_table table[], const char *desc); * or "char *cb(void *)", where "type" is the type of the @arg * argument. * - * At least one of @longopt and @shortopt must be non-zero. If the - * @cb returns false, opt_parse() will stop parsing and return false. + * If the @cb returns non-NULL, opt_parse() will stop parsing, use the + * returned string to form an error message for errlog(), free() the + * string and return false. */ -#define opt_register_noarg(longopt, shortopt, cb, arg, desc) \ - _opt_register((longopt), (shortopt), OPT_CB_NOARG((cb), (arg)), (desc)) +#define opt_register_noarg(names, cb, arg, desc) \ + _opt_register((names), OPT_CB_NOARG((cb), (arg)), (desc)) /** * opt_register_arg - register an option with an arguments - * @longopt: the name of the argument (eg. "foo" for "--foo"), or NULL. - * @shortopt: the character of the argument (eg. 'f' for "-f"), or 0. + * @names: the names of the option eg. "--foo", "-f" or "--foo/-f/--foobar". * @cb: the callback when the option is found. * @show: the callback when the option is found. * @arg: the argument to hand to @cb. @@ -166,11 +154,11 @@ void opt_register_table(const struct opt_table table[], const char *desc); * @cb returns false, opt_parse() will stop parsing and return false. * * Example: - * opt_register_arg("explode", 'e', explode_cb, NULL, + * opt_register_arg("--explode", explode_cb, NULL, * "Make the machine explode (developers only)"); */ -#define opt_register_arg(longopt, shortopt, cb, show, arg, desc) \ - _opt_register((longopt), (shortopt), OPT_CB_ARG((cb), (show), (arg)), (desc)) +#define opt_register_arg(names, cb, show, arg, desc) \ + _opt_register((names), OPT_CB_ARG((cb), (show), (arg)), (desc)) /** * opt_parse - parse arguments. @@ -216,12 +204,21 @@ char *opt_invalid_argument(const char *arg); * @extra: extra details to print after the initial command, or NULL. * * Creates a usage message, with the program name, arguments, some extra details - * and a table of all the options with their descriptions. + * and a table of all the options with their descriptions. If an option has + * description opt_hidden, it is not shown here. * * The result should be passed to free(). */ char *opt_usage(const char *argv0, const char *extra); +/** + * opt_hidden - string for undocumented options. + * + * This can be used as the desc parameter if you want an option not to be + * shown by opt_usage(). + */ +extern const char opt_hidden[]; + /* Standard helpers. You can write your own: */ /* Sets the @b to true. */ char *opt_set_bool(bool *b); @@ -279,7 +276,7 @@ char *opt_usage_and_exit(const char *extra); (arg) /* Non-typesafe register function. */ -void _opt_register(const char *longopt, char shortopt, enum opt_flags flags, +void _opt_register(const char *names, enum opt_flags flags, char *(*cb)(void *arg), char *(*cb_arg)(const char *optarg, void *arg), void (*show)(char buf[OPT_SHOW_LEN], const void *arg), diff --git a/ccan/opt/private.h b/ccan/opt/private.h index b1a18923..5d9eca23 100644 --- a/ccan/opt/private.h +++ b/ccan/opt/private.h @@ -2,10 +2,13 @@ #define CCAN_OPT_PRIVATE_H extern struct opt_table *opt_table; -extern unsigned int opt_count; +extern unsigned int opt_count, opt_num_short, opt_num_short_arg, opt_num_long; extern const char *opt_argv0; -#define subtable_of(entry) ((struct opt_table *)((entry)->longopt)) +#define subtable_of(entry) ((struct opt_table *)((entry)->names)) + +const char *first_sopt(unsigned *i); +const char *next_sopt(const char *names, unsigned *i); #endif /* CCAN_OPT_PRIVATE_H */ diff --git a/ccan/opt/test/run-helpers.c b/ccan/opt/test/run-helpers.c index 8e1a751f..f8041ead 100644 --- a/ccan/opt/test/run-helpers.c +++ b/ccan/opt/test/run-helpers.c @@ -20,7 +20,7 @@ static void reset_options(void) { free(opt_table); opt_table = NULL; - opt_count = 0; + opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0; } static char *output = NULL; @@ -54,10 +54,10 @@ int main(int argc, char *argv[]) { bool arg = false; reset_options(); - opt_register_noarg(NULL, 'a', opt_set_bool, &arg, NULL); + opt_register_noarg("-a", opt_set_bool, &arg, NULL); ok1(parse_args(&argc, &argv, "-a", NULL)); ok1(arg); - opt_register_arg(NULL, 'b', opt_set_bool_arg, NULL, &arg, NULL); + opt_register_arg("-b", opt_set_bool_arg, NULL, &arg, NULL); ok1(parse_args(&argc, &argv, "-b", "no", NULL)); ok1(!arg); ok1(parse_args(&argc, &argv, "-b", "yes", NULL)); @@ -71,10 +71,10 @@ int main(int argc, char *argv[]) { bool arg = true; reset_options(); - opt_register_noarg(NULL, 'a', opt_set_invbool, &arg, NULL); + opt_register_noarg("-a", opt_set_invbool, &arg, NULL); ok1(parse_args(&argc, &argv, "-a", NULL)); ok1(!arg); - opt_register_arg(NULL, 'b', opt_set_invbool_arg, NULL, + opt_register_arg("-b", opt_set_invbool_arg, NULL, &arg, NULL); ok1(parse_args(&argc, &argv, "-b", "no", NULL)); ok1(arg); @@ -89,7 +89,7 @@ int main(int argc, char *argv[]) { char *arg = (char *)"wrong"; reset_options(); - opt_register_arg(NULL, 'a', opt_set_charp, NULL, &arg, NULL); + opt_register_arg("-a", opt_set_charp, NULL, &arg, NULL); ok1(parse_args(&argc, &argv, "-a", "string", NULL)); ok1(strcmp(arg, "string") == 0); } @@ -97,7 +97,7 @@ int main(int argc, char *argv[]) { int arg = 1000; reset_options(); - opt_register_arg(NULL, 'a', opt_set_intval, NULL, &arg, NULL); + opt_register_arg("-a", opt_set_intval, NULL, &arg, NULL); ok1(parse_args(&argc, &argv, "-a", "9999", NULL)); ok1(arg == 9999); ok1(parse_args(&argc, &argv, "-a", "-9999", NULL)); @@ -114,7 +114,7 @@ int main(int argc, char *argv[]) { unsigned int arg = 1000; reset_options(); - opt_register_arg(NULL, 'a', opt_set_uintval, NULL, &arg, NULL); + opt_register_arg("-a", opt_set_uintval, NULL, &arg, NULL); ok1(parse_args(&argc, &argv, "-a", "9999", NULL)); ok1(arg == 9999); ok1(!parse_args(&argc, &argv, "-a", "-9999", NULL)); @@ -127,7 +127,7 @@ int main(int argc, char *argv[]) { long int arg = 1000; reset_options(); - opt_register_arg(NULL, 'a', opt_set_longval, NULL, &arg, NULL); + opt_register_arg("-a", opt_set_longval, NULL, &arg, NULL); ok1(parse_args(&argc, &argv, "-a", "9999", NULL)); ok1(arg == 9999); ok1(parse_args(&argc, &argv, "-a", "-9999", NULL)); @@ -146,7 +146,7 @@ int main(int argc, char *argv[]) { unsigned long int arg = 1000; reset_options(); - opt_register_arg(NULL, 'a', opt_set_ulongval, NULL, &arg, NULL); + opt_register_arg("-a", opt_set_ulongval, NULL, &arg, NULL); ok1(parse_args(&argc, &argv, "-a", "9999", NULL)); ok1(arg == 9999); ok1(!parse_args(&argc, &argv, "-a", "-9999", NULL)); @@ -164,7 +164,7 @@ int main(int argc, char *argv[]) { int arg = 1000; reset_options(); - opt_register_noarg(NULL, 'a', opt_inc_intval, &arg, NULL); + opt_register_noarg("-a", opt_inc_intval, &arg, NULL); ok1(parse_args(&argc, &argv, "-a", NULL)); ok1(arg == 1001); ok1(parse_args(&argc, &argv, "-a", "-a", NULL)); @@ -177,7 +177,7 @@ int main(int argc, char *argv[]) { int exitval; reset_options(); - opt_register_noarg(NULL, 'a', + opt_register_noarg("-a", opt_version_and_exit, "1.2.3", NULL); exitval = setjmp(exited); if (exitval == 0) { @@ -195,7 +195,7 @@ int main(int argc, char *argv[]) { int exitval; reset_options(); - opt_register_noarg(NULL, 'a', + opt_register_noarg("-a", opt_usage_and_exit, "[args]", NULL); exitval = setjmp(exited); if (exitval == 0) { diff --git a/ccan/opt/test/run-iter.c b/ccan/opt/test/run-iter.c new file mode 100644 index 00000000..147470ff --- /dev/null +++ b/ccan/opt/test/run-iter.c @@ -0,0 +1,76 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include "utils.h" +#include +#include + +/* Test iterators. */ +int main(int argc, char *argv[]) +{ + unsigned i, len; + const char *p; + + plan_tests(37); + opt_register_table(subtables, NULL); + + p = first_lopt(&i, &len); + ok1(i == 0); + ok1(len == 3); + ok1(strncmp(p, "jjj", len) == 0); + p = next_lopt(p, &i, &len); + ok1(i == 0); + ok1(len == 3); + ok1(strncmp(p, "lll", len) == 0); + p = next_lopt(p, &i, &len); + ok1(i == 1); + ok1(len == 3); + ok1(strncmp(p, "mmm", len) == 0); + p = next_lopt(p, &i, &len); + ok1(i == 5); + ok1(len == 3); + ok1(strncmp(p, "ddd", len) == 0); + p = next_lopt(p, &i, &len); + ok1(i == 6); + ok1(len == 3); + ok1(strncmp(p, "eee", len) == 0); + p = next_lopt(p, &i, &len); + ok1(i == 7); + ok1(len == 3); + ok1(strncmp(p, "ggg", len) == 0); + p = next_lopt(p, &i, &len); + ok1(i == 8); + ok1(len == 3); + ok1(strncmp(p, "hhh", len) == 0); + p = next_lopt(p, &i, &len); + ok1(!p); + + p = first_sopt(&i); + ok1(i == 0); + ok1(*p == 'j'); + p = next_sopt(p, &i); + ok1(i == 0); + ok1(*p == 'l'); + p = next_sopt(p, &i); + ok1(i == 1); + ok1(*p == 'm'); + p = next_sopt(p, &i); + ok1(i == 2); + ok1(*p == 'a'); + p = next_sopt(p, &i); + ok1(i == 3); + ok1(*p == 'b'); + p = next_sopt(p, &i); + ok1(i == 7); + ok1(*p == 'g'); + p = next_sopt(p, &i); + ok1(i == 8); + ok1(*p == 'h'); + p = next_sopt(p, &i); + ok1(!p); + + return exit_status(); +} diff --git a/ccan/opt/test/run-no-options.c b/ccan/opt/test/run-no-options.c new file mode 100644 index 00000000..98ab8d90 --- /dev/null +++ b/ccan/opt/test/run-no-options.c @@ -0,0 +1,28 @@ +/* Make sure we still work with no options registered */ +#include +#include +#include +#include +#include "utils.h" + +int main(int argc, char *argv[]) +{ + const char *myname = argv[0]; + + plan_tests(7); + + /* Simple short arg.*/ + ok1(!parse_args(&argc, &argv, "-a", NULL)); + /* Simple long arg.*/ + ok1(!parse_args(&argc, &argv, "--aaa", NULL)); + + /* Extra arguments preserved. */ + ok1(parse_args(&argc, &argv, "extra", "args", NULL)); + ok1(argc == 3); + ok1(argv[0] == myname); + ok1(strcmp(argv[1], "extra") == 0); + ok1(strcmp(argv[2], "args") == 0); + + return exit_status(); +} + diff --git a/ccan/opt/test/run-usage.c b/ccan/opt/test/run-usage.c index 2e4ce400..82140344 100644 --- a/ccan/opt/test/run-usage.c +++ b/ccan/opt/test/run-usage.c @@ -17,13 +17,14 @@ static char *my_cb(void *p) int main(int argc, char *argv[]) { char *output; - plan_tests(18); + + plan_tests(19); opt_register_table(subtables, NULL); - opt_register_noarg("kkk", 'k', my_cb, NULL, "magic kkk option"); + opt_register_noarg("--kkk/-k", my_cb, NULL, "magic kkk option"); output = opt_usage("my name", "ExTrA Args"); diag("%s", output); ok1(strstr(output, "Usage: my name")); - ok1(strstr(output, "--jjj/-j ")); + ok1(strstr(output, "--jjj/-j/--lll/-l ")); ok1(strstr(output, "ExTrA Args")); ok1(strstr(output, "-a ")); ok1(strstr(output, " Description of a\n")); @@ -34,13 +35,15 @@ int main(int argc, char *argv[]) ok1(strstr(output, "--eee ")); ok1(strstr(output, " (default: eee)\n")); ok1(strstr(output, "long table options:\n")); - /* This table is hidden. */ - ok1(!strstr(output, "--ggg/-g ")); - ok1(!strstr(output, " Description of ggg\n")); - ok1(!strstr(output, "--hhh/-h ")); - ok1(!strstr(output, " Description of hhh\n")); + ok1(strstr(output, "--ggg/-g ")); + ok1(strstr(output, " Description of ggg\n")); + ok1(strstr(output, "-h/--hhh ")); + ok1(strstr(output, " Description of hhh\n")); ok1(strstr(output, "--kkk/-k")); ok1(strstr(output, "magic kkk option")); + /* This entry is hidden. */ + ok1(!strstr(output, "--mmm/-m")); + free(output); return exit_status(); diff --git a/ccan/opt/test/run.c b/ccan/opt/test/run.c index 26c69dff..4eb78c93 100644 --- a/ccan/opt/test/run.c +++ b/ccan/opt/test/run.c @@ -8,7 +8,7 @@ static void reset_options(void) { free(opt_table); opt_table = NULL; - opt_count = 0; + opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0; free(err_output); err_output = NULL; } @@ -20,7 +20,7 @@ int main(int argc, char *argv[]) plan_tests(148); /* Simple short arg.*/ - opt_register_noarg(NULL, 'a', test_noarg, NULL, NULL); + opt_register_noarg("-a", test_noarg, NULL, NULL); ok1(parse_args(&argc, &argv, "-a", NULL)); ok1(argc == 1); ok1(argv[0] == myname); @@ -28,7 +28,7 @@ int main(int argc, char *argv[]) ok1(test_cb_called == 1); /* Simple long arg. */ - opt_register_noarg("aaa", 0, test_noarg, NULL, NULL); + opt_register_noarg("--aaa", test_noarg, NULL, NULL); ok1(parse_args(&argc, &argv, "--aaa", NULL)); ok1(argc == 1); ok1(argv[0] == myname); @@ -36,7 +36,7 @@ int main(int argc, char *argv[]) ok1(test_cb_called == 2); /* Both long and short args. */ - opt_register_noarg("aaa", 'a', test_noarg, NULL, NULL); + opt_register_noarg("--aaa/-a", test_noarg, NULL, NULL); ok1(parse_args(&argc, &argv, "--aaa", "-a", NULL)); ok1(argc == 1); ok1(argv[0] == myname); @@ -54,7 +54,7 @@ int main(int argc, char *argv[]) /* Argument variants. */ reset_options(); test_cb_called = 0; - opt_register_arg("aaa", 'a', test_arg, NULL, "aaa", NULL); + opt_register_arg("-a/--aaa", test_arg, NULL, "aaa", NULL); ok1(parse_args(&argc, &argv, "--aaa", "aaa", NULL)); ok1(argc == 1); ok1(argv[0] == myname); @@ -201,7 +201,7 @@ int main(int argc, char *argv[]) reset_options(); /* glibc's getopt does not handle ? with arguments. */ - opt_register_noarg(NULL, '?', test_noarg, NULL, NULL); + opt_register_noarg("-?", test_noarg, NULL, NULL); ok1(parse_args(&argc, &argv, "-?", NULL)); ok1(test_cb_called == 1); ok1(parse_args(&argc, &argv, "-a", NULL) == false); diff --git a/ccan/opt/test/utils.c b/ccan/opt/test/utils.c index a012d40b..6870af74 100644 --- a/ccan/opt/test/utils.c +++ b/ccan/opt/test/utils.c @@ -70,33 +70,35 @@ bool parse_args(int *argc, char ***argv, ...) struct opt_table short_table[] = { /* Short opts, different args. */ - { OPT_WITHOUT_ARG(NULL, 'a', test_noarg, "a"), "Description of a" }, - { OPT_WITH_ARG(NULL, 'b', test_arg, show_arg, "b"), "Description of b" }, + { OPT_WITHOUT_ARG("-a", test_noarg, "a"), "Description of a" }, + { OPT_WITH_ARG("-b", test_arg, show_arg, "b"), "Description of b" }, OPT_ENDTABLE }; struct opt_table long_table[] = { /* Long opts, different args. */ - { OPT_WITHOUT_ARG("ddd", 0, test_noarg, "ddd"), "Description of ddd" }, - { OPT_WITH_ARG("eee", 0, test_arg, show_arg, "eee"), }, + { OPT_WITHOUT_ARG("--ddd", test_noarg, "ddd"), "Description of ddd" }, + { OPT_WITH_ARG("--eee", test_arg, show_arg, "eee"), }, OPT_ENDTABLE }; struct opt_table long_and_short_table[] = { /* Short and long, different args. */ - { OPT_WITHOUT_ARG("ggg", 'g', test_noarg, "ggg"), + { OPT_WITHOUT_ARG("--ggg/-g", test_noarg, "ggg"), "Description of ggg" }, - { OPT_WITH_ARG("hhh", 'h', test_arg, NULL, "hhh"), + { OPT_WITH_ARG("-h/--hhh", test_arg, NULL, "hhh"), "Description of hhh"}, OPT_ENDTABLE }; /* Sub-table test. */ struct opt_table subtables[] = { - /* Short and long, no description */ - { OPT_WITH_ARG("jjj", 'j', test_arg, show_arg, "jjj") }, + /* Two short, and two long long, no description */ + { OPT_WITH_ARG("--jjj/-j/--lll/-l", test_arg, show_arg, "jjj") }, + /* Hidden option */ + { OPT_WITH_ARG("--mmm/-m", test_arg, show_arg, "mmm"), opt_hidden }, OPT_SUBTABLE(short_table, NULL), OPT_SUBTABLE(long_table, "long table options"), - OPT_SUBTABLE(long_and_short_table, opt_table_hidden), + OPT_SUBTABLE(long_and_short_table, NULL), OPT_ENDTABLE }; diff --git a/ccan/opt/usage.c b/ccan/opt/usage.c index d76c3b03..cbe1231a 100644 --- a/ccan/opt/usage.c +++ b/ccan/opt/usage.c @@ -6,21 +6,16 @@ #include "private.h" /* We only use this for pointer comparisons. */ -const char opt_table_hidden[1]; +const char opt_hidden[1]; static unsigned write_short_options(char *str) { unsigned int i, num = 0; + const char *p; - for (i = 0; i < opt_count; i++) { - if (opt_table[i].flags == OPT_SUBTABLE) { - if (opt_table[i].desc == opt_table_hidden) { - /* Skip these options. */ - i += (intptr_t)opt_table[i].arg - 1; - continue; - } - } else if (opt_table[i].shortopt) - str[num++] = opt_table[i].shortopt; + for (p = first_sopt(&i); p; p = next_sopt(p, &i)) { + if (opt_table[i].desc != opt_hidden) + str[num++] = *p; } return num; } @@ -35,7 +30,7 @@ char *opt_usage(const char *argv0, const char *extra) /* An overestimate of our length. */ len = strlen("Usage: %s ") + strlen(argv0) - + strlen("[-%.*s]") + opt_count + 1 + + strlen("[-%.*s]") + opt_num_short + 1 + strlen(" ") + strlen(extra) + strlen("\n"); @@ -43,10 +38,8 @@ char *opt_usage(const char *argv0, const char *extra) if (opt_table[i].flags == OPT_SUBTABLE) { len += strlen("\n") + strlen(opt_table[i].desc) + strlen(":\n"); - } else { - len += strlen("--%s/-%c") + strlen(" "); - if (opt_table[i].longopt) - len += strlen(opt_table[i].longopt); + } else if (opt_table[i].desc != opt_hidden) { + len += strlen(opt_table[i].names) + strlen(" "); if (opt_table[i].desc) { len += strlen(OPT_SPACE_PAD) + strlen(opt_table[i].desc) + 1; @@ -78,23 +71,13 @@ char *opt_usage(const char *argv0, const char *extra) p += sprintf(p, "\n"); for (i = 0; i < opt_count; i++) { + if (opt_table[i].desc == opt_hidden) + continue; if (opt_table[i].flags == OPT_SUBTABLE) { - if (opt_table[i].desc == opt_table_hidden) { - /* Skip these options. */ - i += (intptr_t)opt_table[i].arg - 1; - continue; - } p += sprintf(p, "%s:\n", opt_table[i].desc); continue; } - if (opt_table[i].shortopt && opt_table[i].longopt) - len = sprintf(p, "--%s/-%c", - opt_table[i].longopt, - opt_table[i].shortopt); - else if (opt_table[i].shortopt) - len = sprintf(p, "-%c", opt_table[i].shortopt); - else - len = sprintf(p, "--%s", opt_table[i].longopt); + len = sprintf(p, "%s", opt_table[i].names); if (opt_table[i].flags == OPT_HASARG) len += sprintf(p + len, " "); if (opt_table[i].desc || opt_table[i].show) -- 2.39.2