static const char *first_opt(unsigned *i, unsigned *len)
{
for (*i = 0; *i < opt_count; (*i)++) {
- if (opt_table[*i].type == OPT_SUBTABLE)
+ if (opt_table[*i].type & OPT_SUBTABLE)
continue;
return first_name(opt_table[*i].names, len);
}
static const char *next_opt(const char *p, unsigned *i, unsigned *len)
{
for (; *i < opt_count; (*i)++) {
- if (opt_table[*i].type == OPT_SUBTABLE)
+ if (opt_table[*i].type & OPT_SUBTABLE)
continue;
if (!p)
return first_name(opt_table[*i].names, len);
{
const char *p;
unsigned len;
+ enum opt_type type = entry->type & (OPT_USER_MIN-1);
- if (entry->type != OPT_HASARG && entry->type != OPT_NOARG
- && entry->type != (OPT_EARLY|OPT_HASARG)
- && entry->type != (OPT_EARLY|OPT_NOARG))
+ if (type != OPT_HASARG && type != OPT_NOARG
+ && type != (OPT_EARLY|OPT_HASARG)
+ && type != (OPT_EARLY|OPT_NOARG))
failmsg("Option %s: unknown entry type %u",
entry->names, entry->type);
int found = -1, i;
for (i = 0; i < opt_count; i++) {
- if (opt_table[i].type == OPT_SUBTABLE)
+ if (opt_table[i].type & OPT_SUBTABLE)
continue;
if (strcmp(opt_table[i].names, names) == 0)
found = i;
add_opt(&heading);
}
for (i = 0; entry[i].type != OPT_END; i++) {
- if (entry[i].type == OPT_SUBTABLE)
+ if (entry[i].type & OPT_SUBTABLE)
opt_register_table(subtable_of(&entry[i]),
entry[i].desc);
else {
*/
struct opt_table *opt_find_short(char arg);
+/* opt_type bits reserved for users to play with (ignored!).
+ * You can set bits in type e.g. (1<<OPT_USER_START) to (1<<OPT_USER_END)
+ * when calling _opt_register. */
+#define OPT_USER_START 8
+#define OPT_USER_END 15
+
/* Below here are private declarations. */
/* You can use this directly to build tables, but the macros will ensure
* consistency and type safety. */
OPT_SUBTABLE = 4, /* Actually, longopt points to a subtable... */
OPT_EARLY = 8, /* Parse this from opt_early_parse() only. */
OPT_END = 16, /* End of the table. */
+
+ /* Make sure no compiler will assume we never have large
+ * values in the enum! */
+ OPT_USER_MIN = (1 << OPT_USER_START),
+ OPT_USER_MAX = (1 << OPT_USER_END),
};
struct opt_table {
len = 2;
}
- if ((ot->type & ~OPT_EARLY) == OPT_NOARG) {
+ if (ot->type & OPT_NOARG) {
if (optarg)
return parse_err(errlog, argv[0], o, len,
"doesn't allow an argument");
--- /dev/null
+#include <ccan/tap/tap.h>
+#include <stdlib.h>
+#include <ccan/opt/opt.c>
+#include <ccan/opt/usage.c>
+#include <ccan/opt/helpers.c>
+#include <ccan/opt/parse.c>
+#include "utils.h"
+
+int main(int argc, char *argv[])
+{
+ const char *myname = argv[0];
+
+ plan_tests(28);
+
+ opt_register_noarg("-a", test_noarg, NULL, "All");
+ opt_register_noarg("--aaa", test_noarg, NULL, "AAAAll");
+ opt_register_arg("-b|--bbb", test_arg, NULL, "bbb", "AAAAAAll");
+
+ ok1(strcmp(opt_table[0].names, "-a") == 0);
+ ok1(opt_table[0].type == OPT_NOARG);
+ ok1(strcmp(opt_table[1].names, "--aaa") == 0);
+ ok1(opt_table[1].type == OPT_NOARG);
+ ok1(strcmp(opt_table[2].names, "-b|--bbb") == 0);
+ ok1(opt_table[2].type == OPT_HASARG);
+
+ opt_table[0].type |= (1 << OPT_USER_START);
+ opt_table[1].type |= ((1 << OPT_USER_END)-1) - ((1 << OPT_USER_START)-1);
+ opt_table[2].type |= (1 << OPT_USER_END);
+
+ /* Should all work fine! */
+ ok1(parse_args(&argc, &argv, "-a", NULL));
+ ok1(argc == 1);
+ ok1(argv[0] == myname);
+ ok1(test_cb_called == 1);
+
+ ok1(parse_args(&argc, &argv, "--aaa", NULL));
+ ok1(argc == 1);
+ ok1(argv[0] == myname);
+ ok1(test_cb_called == 2);
+
+ /* This one needs an arg. */
+ ok1(parse_args(&argc, &argv, "-b", NULL) == false);
+ ok1(test_cb_called == 2);
+ ok1(parse_args(&argc, &argv, "-b", "bbb", NULL));
+ ok1(argc == 1);
+ ok1(argv[0] == myname);
+ ok1(argv[1] == NULL);
+ ok1(test_cb_called == 3);
+
+ ok1(parse_args(&argc, &argv, "--bbb", "bbb", NULL));
+ ok1(argc == 1);
+ ok1(argv[0] == myname);
+ ok1(argv[1] == NULL);
+ ok1(test_cb_called == 4);
+
+ /* parse_args allocates argv */
+ free(argv);
+ return exit_status();
+}
size_t l;
if (opt_table[i].desc == opt_hidden)
continue;
- if (opt_table[i].type == OPT_SUBTABLE)
+ if (opt_table[i].type & OPT_SUBTABLE)
continue;
l = strlen(opt_table[i].names);
- if (opt_table[i].type == OPT_HASARG
+ if ((opt_table[i].type & OPT_HASARG)
&& !strchr(opt_table[i].names, ' ')
&& !strchr(opt_table[i].names, '='))
l += strlen(" <arg>");
for (i = 0; i < opt_count; i++) {
if (opt_table[i].desc == opt_hidden)
continue;
- if (opt_table[i].type == OPT_SUBTABLE) {
+ if (opt_table[i].type & OPT_SUBTABLE) {
ret = add_str(ret, &len, &max, opt_table[i].desc);
ret = add_str(ret, &len, &max, ":\n");
continue;