#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)
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;
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();
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);
/* 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
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;
}
problem = e->cb(e->arg);
if (problem) {
- parse_fail(errlog, e->shortopt, e->longopt,
- problem);
+ parse_fail(errlog, ret, name, problem);
free(problem);
break;
}
#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 */
/**
* 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.
*
* 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 <arg>"), or NULL.
- * @shortopt: the character of the argument (eg. 'f' for "-f <arg>"), 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 <arg>).
* @show: the callback to print the value in get_usage (or NULL)
* @arg: the argument to hand to @cb and @show
* 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
*
* 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
/**
* 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.
* 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.
* @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.
* @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);
(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),
#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 */
{
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;
{
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));
{
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);
{
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);
}
{
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));
{
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));
{
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));
{
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));
{
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));
{
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) {
{
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) {
--- /dev/null
+#define _GNU_SOURCE
+#include <ccan/tap/tap.h>
+#include <stdarg.h>
+#include <setjmp.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include "utils.h"
+#include <ccan/opt/opt.c>
+#include <ccan/opt/usage.c>
+
+/* 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();
+}
--- /dev/null
+/* Make sure we still work with no options registered */
+#include <ccan/tap/tap.h>
+#include <stdlib.h>
+#include <ccan/opt/opt.c>
+#include <ccan/opt/usage.c>
+#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();
+}
+
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 <arg>"));
+ ok1(strstr(output, "--jjj/-j/--lll/-l <arg>"));
ok1(strstr(output, "ExTrA Args"));
ok1(strstr(output, "-a "));
ok1(strstr(output, " Description of a\n"));
ok1(strstr(output, "--eee <arg> "));
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 <arg>"));
- ok1(!strstr(output, " Description of hhh\n"));
+ ok1(strstr(output, "--ggg/-g "));
+ ok1(strstr(output, " Description of ggg\n"));
+ ok1(strstr(output, "-h/--hhh <arg>"));
+ 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();
{
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;
}
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);
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);
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);
/* 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);
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);
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
};
#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;
}
/* 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");
if (opt_table[i].flags == OPT_SUBTABLE) {
len += strlen("\n") + strlen(opt_table[i].desc)
+ strlen(":\n");
- } else {
- len += strlen("--%s/-%c") + strlen(" <arg>");
- 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(" <arg>");
if (opt_table[i].desc) {
len += strlen(OPT_SPACE_PAD)
+ strlen(opt_table[i].desc) + 1;
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, " <arg>");
if (opt_table[i].desc || opt_table[i].show)