]> git.ozlabs.org Git - ccan/commitdiff
ccan: allow user to set some bits in opt_table.type.
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 8 May 2023 01:30:22 +0000 (11:00 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 20 Jun 2023 03:08:36 +0000 (12:38 +0930)
In particular, Core Lightning wants to use this to flag arguments
which can be specified multiple times (without overriding).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/opt/opt.c
ccan/opt/opt.h
ccan/opt/parse.c
ccan/opt/test/run-userbits.c [new file with mode: 0644]
ccan/opt/usage.c

index d376a598da932445de592c8f12e2466d4bc431bd..ef711d2c51889190c4190cc4f85c54c470808abc 100644 (file)
@@ -34,7 +34,7 @@ static const char *next_name(const char *names, unsigned *len)
 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);
        }
@@ -44,7 +44,7 @@ static const char *first_opt(unsigned *i, unsigned *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);
@@ -114,10 +114,11 @@ static void check_opt(const struct opt_table *entry)
 {
        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);
 
@@ -181,7 +182,7 @@ bool opt_unregister(const char *names)
        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;
@@ -203,7 +204,7 @@ void opt_register_table(const struct opt_table entry[], const char *desc)
                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 {
index 4b5a2c6ceea137c9b4247f83b7fa6396aade88ed..9eec73854bb144f9051484578ff67f1af94ac386 100644 (file)
@@ -527,6 +527,12 @@ struct opt_table *opt_find_long(const char *arg, const char **optarg);
  */
 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. */
@@ -536,6 +542,11 @@ enum opt_type {
        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 {
index 56c2a35ba4a1532ce2967d062e32ed8980591d32..b932bf333571620d0e19edca1b10a82cc70d8397 100644 (file)
@@ -140,7 +140,7 @@ int parse_one(int *argc, char *argv[], enum opt_type is_early, unsigned *offset,
                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");
diff --git a/ccan/opt/test/run-userbits.c b/ccan/opt/test/run-userbits.c
new file mode 100644 (file)
index 0000000..7f102f0
--- /dev/null
@@ -0,0 +1,59 @@
+#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();
+}
index 8ee4ebd03ad5083e2f8a36649319190edb14e7b5..4ed279197fe808ac391d39a93df1dbb9e8b14126 100644 (file)
@@ -182,10 +182,10 @@ char *opt_usage(const char *argv0, const char *extra)
                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>");
@@ -221,7 +221,7 @@ char *opt_usage(const char *argv0, const char *extra)
        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;