]> git.ozlabs.org Git - ccan/commitdiff
opt: change / separator to |
authorRusty Russell <rusty@rustcorp.com.au>
Fri, 22 Oct 2010 01:25:43 +0000 (11:55 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 26 Oct 2010 08:47:52 +0000 (19:17 +1030)
Paul Wayper points out that "or" has a better mental mapping, and more
importantly that this is how Perl's Getopt::Long does it.

I'm still leaving the - and -- in there, for reasons previously articulated
(grep-friendly and harder to get wrong).

ccan/opt/opt.c
ccan/opt/opt.h
ccan/opt/test/run-correct-reporting.c
ccan/opt/test/run-usage.c
ccan/opt/test/run.c
ccan/opt/test/utils.c
tools/ccanlint/ccanlint.c

index de56299cc34a6259bd45650867e5b856d11116fc..0929daff65eaa3af8bade32de748d37dd387d026 100644 (file)
@@ -17,7 +17,7 @@ const char *opt_argv0;
 /* Returns string after first '-'. */
 static const char *first_name(const char *names, unsigned *len)
 {
 /* Returns string after first '-'. */
 static const char *first_name(const char *names, unsigned *len)
 {
-       *len = strcspn(names + 1, "/= ");
+       *len = strcspn(names + 1, "|= ");
        return names + 1;
 }
 
        return names + 1;
 }
 
@@ -275,7 +275,7 @@ static void parse_fail(void (*errlog)(const char *fmt, ...),
                errlog("%s: -%c: %s", opt_argv0, shortopt, problem);
        else
                errlog("%s: --%.*s: %s", opt_argv0,
                errlog("%s: -%c: %s", opt_argv0, shortopt, problem);
        else
                errlog("%s: --%.*s: %s", opt_argv0,
-                      strcspn(longopt, "/"), longopt, problem);
+                      strcspn(longopt, "|"), longopt, problem);
 }
 
 /* Parse your arguments. */
 }
 
 /* Parse your arguments. */
index 7d7bc571fc2363f40241497297bcdc32ff5f6716..034404e958e657d882de742f32c9fbf3351ee5dd 100644 (file)
@@ -6,8 +6,8 @@
 /* You can use this directly to build tables, but the macros will ensure
  * consistency and type safety. */
 enum opt_type {
 /* You can use this directly to build tables, but the macros will ensure
  * consistency and type safety. */
 enum opt_type {
-       OPT_NOARG = 1,          /* -f/--foo */
-       OPT_HASARG = 2,         /* -f arg/--foo=arg/--foo arg */
+       OPT_NOARG = 1,          /* -f|--foo */
+       OPT_HASARG = 2,         /* -f arg|--foo=arg|--foo arg */
        OPT_SUBTABLE = 4,       /* Actually, longopt points to a subtable... */
        OPT_END = 8,            /* End of the table. */
 };
        OPT_SUBTABLE = 4,       /* Actually, longopt points to a subtable... */
        OPT_END = 8,            /* End of the table. */
 };
@@ -16,7 +16,7 @@ enum opt_type {
 #define OPT_SHOW_LEN 80
 
 struct opt_table {
 #define OPT_SHOW_LEN 80
 
 struct opt_table {
-       const char *names; /* slash-separated names, --longopt or -s */
+       const char *names; /* pipe-separated names, --longopt or -s */
        enum opt_type type;
        char *(*cb)(void *arg); /* OPT_NOARG */
        char *(*cb_arg)(const char *optarg, void *arg); /* OPT_HASARG */
        enum opt_type type;
        char *(*cb)(void *arg); /* OPT_NOARG */
        char *(*cb_arg)(const char *optarg, void *arg); /* OPT_HASARG */
@@ -27,7 +27,7 @@ struct opt_table {
 
 /**
  * OPT_WITHOUT_ARG() - macro for initializing an opt_table entry (without arg)
 
 /**
  * OPT_WITHOUT_ARG() - macro for initializing an opt_table entry (without arg)
- * @names: the names of the option eg. "--foo", "-f" or "--foo/-f/--foobar".
+ * @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 description for opt_usage(), or opt_hidden.
  * @cb: the callback when the option is found.
  * @arg: the argument to hand to @cb.
  * @desc: the description for opt_usage(), or opt_hidden.
@@ -41,7 +41,7 @@ struct opt_table {
  * string and return false.
  *
  * Any number of equivalent short or long options can be listed in @names,
  * string and return false.
  *
  * Any number of equivalent short or long options can be listed in @names,
- * separated by '/'.  Short options are a single hyphen followed by a single
+ * separated by '|'.  Short options are a single hyphen followed by a single
  * character, long options are two hypens followed by one or more characters.
  *
  * See Also:
  * character, long options are two hypens followed by one or more characters.
  *
  * See Also:
@@ -52,7 +52,7 @@ struct opt_table {
 
 /**
  * OPT_WITH_ARG() - macro for initializing long and short option (with arg)
 
 /**
  * OPT_WITH_ARG() - macro for initializing long and short option (with arg)
- * @names: the option names eg. "--foo=<arg>", "-f" or "-f/--foo <arg>".
+ * @names: the option names eg. "--foo=<arg>", "-f" or "-f|--foo <arg>".
  * @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
  * @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
@@ -70,7 +70,7 @@ struct opt_table {
  * nul-terminate that buffer.
  *
  * Any number of equivalent short or long options can be listed in @names,
  * nul-terminate that buffer.
  *
  * Any number of equivalent short or long options can be listed in @names,
- * separated by '/'.  Short options are a single hyphen followed by a single
+ * separated by '|'.  Short options are a single hyphen followed by a single
  * character, long options are two hypens followed by one or more characters.
  * A space or equals in @names is ignored for parsing, and only used
  * for printing the usage.
  * character, long options are two hypens followed by one or more characters.
  * A space or equals in @names is ignored for parsing, and only used
  * for printing the usage.
@@ -128,7 +128,7 @@ void opt_register_table(const struct opt_table table[], const char *desc);
 
 /**
  * opt_register_noarg - register an option with no arguments
 
 /**
  * opt_register_noarg - register an option with no arguments
- * @names: the names of the option eg. "--foo", "-f" or "--foo/-f/--foobar".
+ * @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.
  * @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.
@@ -149,7 +149,7 @@ void opt_register_table(const struct opt_table table[], const char *desc);
 
 /**
  * opt_register_arg - register an option with an arguments
 
 /**
  * opt_register_arg - register an option with an arguments
- * @names: the names of the option eg. "--foo", "-f" or "--foo/-f/--foobar".
+ * @names: the names of the option eg. "--foo", "-f" or "--foo|-f|--foobar".
  * @cb: the callback when the option is found.
  * @show: the callback to print the value in get_usage (or NULL)
  * @arg: the argument to hand to @cb.
  * @cb: the callback when the option is found.
  * @show: the callback to print the value in get_usage (or NULL)
  * @arg: the argument to hand to @cb.
@@ -172,7 +172,7 @@ void opt_register_table(const struct opt_table table[], const char *desc);
  *     errx(1, "BOOM! %s", optarg);
  * }
  * ...
  *     errx(1, "BOOM! %s", optarg);
  * }
  * ...
- *     opt_register_arg("--explode/--boom", explode, NULL, NULL, opt_hidden);
+ *     opt_register_arg("--explode|--boom", explode, NULL, NULL, opt_hidden);
  */
 #define opt_register_arg(names, cb, show, arg, desc)                   \
        _opt_register((names), 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))
index 2dc62656ff7c9fc7253d85c2081dafd8bf86946f..b51d53eb3faff7f0745db3514ac1764ed65adca1 100644 (file)
@@ -12,7 +12,7 @@ int main(int argc, char *argv[])
        plan_tests(12);
 
        /* --aaa without args. */
        plan_tests(12);
 
        /* --aaa without args. */
-       opt_register_arg("-a/--aaa", test_arg, NULL, "aaa", "");
+       opt_register_arg("-a|--aaa", test_arg, NULL, "aaa", "");
        ok1(!parse_args(&argc, &argv, "--aaa", NULL));
        ok1(strstr(err_output, ": --aaa: option requires an argument"));
        free(err_output);
        ok1(!parse_args(&argc, &argv, "--aaa", NULL));
        ok1(strstr(err_output, ": --aaa: option requires an argument"));
        free(err_output);
@@ -23,7 +23,7 @@ int main(int argc, char *argv[])
        err_output = NULL;
 
        /* Multiple */
        err_output = NULL;
 
        /* Multiple */
-       opt_register_arg("--bbb/-b/-c/--ccc", test_arg, NULL, "aaa", "");
+       opt_register_arg("--bbb|-b|-c|--ccc", test_arg, NULL, "aaa", "");
        ok1(!parse_args(&argc, &argv, "--bbb", NULL));
        ok1(strstr(err_output, ": --bbb: option requires an argument"));
        free(err_output);
        ok1(!parse_args(&argc, &argv, "--bbb", NULL));
        ok1(strstr(err_output, ": --bbb: option requires an argument"));
        free(err_output);
index 60df988c97bdcb6f2163aad172723740dbabf094..9d448dc460b7cf6db90ff398ac0558138bfc51c1 100644 (file)
@@ -21,13 +21,13 @@ int main(int argc, char *argv[])
 
        plan_tests(38);
        opt_register_table(subtables, NULL);
 
        plan_tests(38);
        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");
        opt_register_noarg("-?", opt_usage_and_exit, "<MyArgs>...",
                           "This message");
        output = opt_usage("my name", "ExTrA Args");
        diag("%s", output);
        ok1(strstr(output, "Usage: my name"));
        opt_register_noarg("-?", opt_usage_and_exit, "<MyArgs>...",
                           "This message");
        output = opt_usage("my name", "ExTrA Args");
        diag("%s", output);
        ok1(strstr(output, "Usage: my name"));
-       ok1(strstr(output, "--jjj/-j/--lll/-l <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, "ExTrA Args"));
        ok1(strstr(output, "-a "));
        ok1(strstr(output, " Description of a\n"));
@@ -38,21 +38,21 @@ int main(int argc, char *argv[])
        ok1(strstr(output, "--eee <filename> "));
        ok1(strstr(output, " (default: eee)\n"));
        ok1(strstr(output, "long table options:\n"));
        ok1(strstr(output, "--eee <filename> "));
        ok1(strstr(output, " (default: eee)\n"));
        ok1(strstr(output, "long table options:\n"));
-       ok1(strstr(output, "--ggg/-g "));
+       ok1(strstr(output, "--ggg|-g "));
        ok1(strstr(output, " Description of ggg\n"));
        ok1(strstr(output, " Description of ggg\n"));
-       ok1(strstr(output, "-h/--hhh <arg>"));
+       ok1(strstr(output, "-h|--hhh <arg>"));
        ok1(strstr(output, " Description of hhh\n"));
        ok1(strstr(output, " Description of hhh\n"));
-       ok1(strstr(output, "--kkk/-k"));
+       ok1(strstr(output, "--kkk|-k"));
        ok1(strstr(output, "magic kkk option"));
        /* This entry is hidden. */
        ok1(strstr(output, "magic kkk option"));
        /* This entry is hidden. */
-       ok1(!strstr(output, "--mmm/-m"));
+       ok1(!strstr(output, "--mmm|-m"));
        free(output);
 
        /* NULL should use string from registered options. */
        output = opt_usage("my name", NULL);
        diag("%s", output);
        ok1(strstr(output, "Usage: my name"));
        free(output);
 
        /* NULL should use string from registered options. */
        output = opt_usage("my name", NULL);
        diag("%s", output);
        ok1(strstr(output, "Usage: my name"));
-       ok1(strstr(output, "--jjj/-j/--lll/-l <arg>"));
+       ok1(strstr(output, "--jjj|-j|--lll|-l <arg>"));
        ok1(strstr(output, "<MyArgs>..."));
        ok1(strstr(output, "-a "));
        ok1(strstr(output, " Description of a\n"));
        ok1(strstr(output, "<MyArgs>..."));
        ok1(strstr(output, "-a "));
        ok1(strstr(output, " Description of a\n"));
@@ -63,14 +63,14 @@ int main(int argc, char *argv[])
        ok1(strstr(output, "--eee <filename> "));
        ok1(strstr(output, " (default: eee)\n"));
        ok1(strstr(output, "long table options:\n"));
        ok1(strstr(output, "--eee <filename> "));
        ok1(strstr(output, " (default: eee)\n"));
        ok1(strstr(output, "long table options:\n"));
-       ok1(strstr(output, "--ggg/-g "));
+       ok1(strstr(output, "--ggg|-g "));
        ok1(strstr(output, " Description of ggg\n"));
        ok1(strstr(output, " Description of ggg\n"));
-       ok1(strstr(output, "-h/--hhh <arg>"));
+       ok1(strstr(output, "-h|--hhh <arg>"));
        ok1(strstr(output, " Description of hhh\n"));
        ok1(strstr(output, " Description of hhh\n"));
-       ok1(strstr(output, "--kkk/-k"));
+       ok1(strstr(output, "--kkk|-k"));
        ok1(strstr(output, "magic kkk option"));
        /* This entry is hidden. */
        ok1(strstr(output, "magic kkk option"));
        /* This entry is hidden. */
-       ok1(!strstr(output, "--mmm/-m"));
+       ok1(!strstr(output, "--mmm|-m"));
        free(output);
 
        return exit_status();
        free(output);
 
        return exit_status();
index 2dd7a8d9a734e5becdd20c644f5f4857e64d4fac..1e04ec169ac2b3dea21ba83909e312accc50a565 100644 (file)
@@ -37,7 +37,7 @@ int main(int argc, char *argv[])
        ok1(test_cb_called == 2);
 
        /* Both long and short args. */
        ok1(test_cb_called == 2);
 
        /* Both long and short args. */
-       opt_register_noarg("--aaa/-a", test_noarg, NULL, "AAAAAAll");
+       opt_register_noarg("--aaa|-a", test_noarg, NULL, "AAAAAAll");
        ok1(parse_args(&argc, &argv, "--aaa", "-a", NULL));
        ok1(argc == 1);
        ok1(argv[0] == myname);
        ok1(parse_args(&argc, &argv, "--aaa", "-a", NULL));
        ok1(argc == 1);
        ok1(argv[0] == myname);
@@ -55,7 +55,7 @@ int main(int argc, char *argv[])
        /* Argument variants. */
        reset_options();
        test_cb_called = 0;
        /* Argument variants. */
        reset_options();
        test_cb_called = 0;
-       opt_register_arg("-a/--aaa", test_arg, NULL, "aaa", "AAAAAAll");
+       opt_register_arg("-a|--aaa", test_arg, NULL, "aaa", "AAAAAAll");
        ok1(parse_args(&argc, &argv, "--aaa", "aaa", NULL));
        ok1(argc == 1);
        ok1(argv[0] == myname);
        ok1(parse_args(&argc, &argv, "--aaa", "aaa", NULL));
        ok1(argc == 1);
        ok1(argv[0] == myname);
index 2a310495fba5cd96eff4d45c8a96446430a0e7c6..5b054f87f315ce7524dd5c90456467c29e80e777 100644 (file)
@@ -84,17 +84,17 @@ struct opt_table long_table[] = {
 
 struct opt_table long_and_short_table[] = {
        /* Short and long, different args. */
 
 struct opt_table long_and_short_table[] = {
        /* Short and long, different args. */
-       OPT_WITHOUT_ARG("--ggg/-g", test_noarg, "ggg", "Description of ggg"),
-       OPT_WITH_ARG("-h/--hhh", test_arg, NULL, "hhh", "Description of hhh"),
+       OPT_WITHOUT_ARG("--ggg|-g", test_noarg, "ggg", "Description of ggg"),
+       OPT_WITH_ARG("-h|--hhh", test_arg, NULL, "hhh", "Description of hhh"),
        OPT_ENDTABLE
 };
 
 /* Sub-table test. */
 struct opt_table subtables[] = {
        /* Two short, and two long long, no description */
        OPT_ENDTABLE
 };
 
 /* Sub-table test. */
 struct opt_table subtables[] = {
        /* Two short, and two long long, no description */
-       OPT_WITH_ARG("--jjj/-j/--lll/-l", test_arg, show_arg, "jjj", ""),
+       OPT_WITH_ARG("--jjj|-j|--lll|-l", test_arg, show_arg, "jjj", ""),
        /* Hidden option */
        /* Hidden option */
-       OPT_WITH_ARG("--mmm/-m", test_arg, show_arg, "mmm", opt_hidden),
+       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, NULL),
        OPT_SUBTABLE(short_table, NULL),
        OPT_SUBTABLE(long_table, "long table options"),
        OPT_SUBTABLE(long_and_short_table, NULL),
index 51962de8582f9a290b272e95b75cc19347065c51..ab805806144670e29dc01e05db63c80706c8030c 100644 (file)
@@ -363,24 +363,24 @@ int main(int argc, char *argv[])
        cmdline_exclude = btree_new(btree_strcmp);
        info_exclude = btree_new(btree_strcmp);
 
        cmdline_exclude = btree_new(btree_strcmp);
        info_exclude = btree_new(btree_strcmp);
 
-       opt_register_arg("--dir/-d", opt_set_charp, opt_show_charp, &dir,
+       opt_register_arg("--dir|-d", opt_set_charp, opt_show_charp, &dir,
                         "use this directory");
                         "use this directory");
-       opt_register_noarg("-n/--safe-mode", opt_set_bool, &safe_mode,
+       opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
                         "do not compile anything");
                         "do not compile anything");
-       opt_register_noarg("-l/--list-tests", list_tests, NULL,
+       opt_register_noarg("-l|--list-tests", list_tests, NULL,
                         "list tests ccanlint performs (and exit)");
                         "list tests ccanlint performs (and exit)");
-       opt_register_arg("-k/--keep <testname>", keep_test, NULL, NULL,
+       opt_register_arg("-k|--keep <testname>", keep_test, NULL, NULL,
                           "keep results of <testname> (can be used multiple times)");
                           "keep results of <testname> (can be used multiple times)");
-       opt_register_noarg("--summary/-s", opt_set_bool, &summary,
+       opt_register_noarg("--summary|-s", opt_set_bool, &summary,
                           "simply give one line summary");
                           "simply give one line summary");
-       opt_register_noarg("--verbose/-v", opt_inc_intval, &verbose,
+       opt_register_noarg("--verbose|-v", opt_inc_intval, &verbose,
                           "verbose mode (can specify more than once)");
                           "verbose mode (can specify more than once)");
-       opt_register_arg("-x/--exclude <testname>", skip_test, NULL, NULL,
+       opt_register_arg("-x|--exclude <testname>", skip_test, NULL, NULL,
                         "exclude <testname> (can be used multiple times)");
                         "exclude <testname> (can be used multiple times)");
-       opt_register_arg("-t/--timeout <milleseconds>", opt_set_uintval,
+       opt_register_arg("-t|--timeout <milleseconds>", opt_set_uintval,
                         NULL, &timeout,
                         "ignore (terminate) tests that are slower than this");
                         NULL, &timeout,
                         "ignore (terminate) tests that are slower than this");
-       opt_register_noarg("-?/-h/--help", opt_usage_and_exit,
+       opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
                           "\nA program for checking and guiding development"
                           " of CCAN modules.",
                           "This usage message");
                           "\nA program for checking and guiding development"
                           " of CCAN modules.",
                           "This usage message");