From 7e381e6c241957ff5840b1d8a0ab5f3bde6337c2 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 22 Oct 2010 11:55:43 +1030 Subject: [PATCH] opt: change / separator to | 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 | 4 ++-- ccan/opt/opt.h | 20 ++++++++++---------- ccan/opt/test/run-correct-reporting.c | 4 ++-- ccan/opt/test/run-usage.c | 22 +++++++++++----------- ccan/opt/test/run.c | 4 ++-- ccan/opt/test/utils.c | 8 ++++---- tools/ccanlint/ccanlint.c | 18 +++++++++--------- 7 files changed, 40 insertions(+), 40 deletions(-) diff --git a/ccan/opt/opt.c b/ccan/opt/opt.c index de56299c..0929daff 100644 --- a/ccan/opt/opt.c +++ b/ccan/opt/opt.c @@ -17,7 +17,7 @@ const char *opt_argv0; /* 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; } @@ -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, - strcspn(longopt, "/"), longopt, problem); + strcspn(longopt, "|"), longopt, problem); } /* Parse your arguments. */ diff --git a/ccan/opt/opt.h b/ccan/opt/opt.h index 7d7bc571..034404e9 100644 --- a/ccan/opt/opt.h +++ b/ccan/opt/opt.h @@ -6,8 +6,8 @@ /* 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. */ }; @@ -16,7 +16,7 @@ enum opt_type { #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 */ @@ -27,7 +27,7 @@ struct opt_table { /** * 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. @@ -41,7 +41,7 @@ struct opt_table { * 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: @@ -52,7 +52,7 @@ struct opt_table { /** * OPT_WITH_ARG() - macro for initializing long and short option (with arg) - * @names: the option names eg. "--foo=", "-f" or "-f/--foo ". + * @names: the option names eg. "--foo=", "-f" or "-f|--foo ". * @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 @@ -70,7 +70,7 @@ struct opt_table { * 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. @@ -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 - * @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. @@ -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 - * @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. @@ -172,7 +172,7 @@ void opt_register_table(const struct opt_table table[], const char *desc); * 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)) diff --git a/ccan/opt/test/run-correct-reporting.c b/ccan/opt/test/run-correct-reporting.c index 2dc62656..b51d53eb 100644 --- a/ccan/opt/test/run-correct-reporting.c +++ b/ccan/opt/test/run-correct-reporting.c @@ -12,7 +12,7 @@ int main(int argc, char *argv[]) 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); @@ -23,7 +23,7 @@ int main(int argc, char *argv[]) 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); diff --git a/ccan/opt/test/run-usage.c b/ccan/opt/test/run-usage.c index 60df988c..9d448dc4 100644 --- a/ccan/opt/test/run-usage.c +++ b/ccan/opt/test/run-usage.c @@ -21,13 +21,13 @@ int main(int argc, char *argv[]) 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, "...", "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 ")); + ok1(strstr(output, "--jjj|-j|--lll|-l ")); 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 ")); 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, "-h/--hhh ")); + ok1(strstr(output, "-h|--hhh ")); 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, "--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")); - ok1(strstr(output, "--jjj/-j/--lll/-l ")); + ok1(strstr(output, "--jjj|-j|--lll|-l ")); ok1(strstr(output, "...")); 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 ")); 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, "-h/--hhh ")); + ok1(strstr(output, "-h|--hhh ")); 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, "--mmm/-m")); + 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 2dd7a8d9..1e04ec16 100644 --- a/ccan/opt/test/run.c +++ b/ccan/opt/test/run.c @@ -37,7 +37,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, "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); @@ -55,7 +55,7 @@ int main(int argc, char *argv[]) /* 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); diff --git a/ccan/opt/test/utils.c b/ccan/opt/test/utils.c index 2a310495..5b054f87 100644 --- a/ccan/opt/test/utils.c +++ b/ccan/opt/test/utils.c @@ -84,17 +84,17 @@ struct opt_table long_table[] = { 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_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 */ - 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), diff --git a/tools/ccanlint/ccanlint.c b/tools/ccanlint/ccanlint.c index 51962de8..ab805806 100644 --- a/tools/ccanlint/ccanlint.c +++ b/tools/ccanlint/ccanlint.c @@ -363,24 +363,24 @@ int main(int argc, char *argv[]) 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"); - 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"); - opt_register_noarg("-l/--list-tests", list_tests, NULL, + opt_register_noarg("-l|--list-tests", list_tests, NULL, "list tests ccanlint performs (and exit)"); - opt_register_arg("-k/--keep ", keep_test, NULL, NULL, + opt_register_arg("-k|--keep ", keep_test, NULL, NULL, "keep results of (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"); - 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)"); - opt_register_arg("-x/--exclude ", skip_test, NULL, NULL, + opt_register_arg("-x|--exclude ", skip_test, NULL, NULL, "exclude (can be used multiple times)"); - opt_register_arg("-t/--timeout ", opt_set_uintval, + opt_register_arg("-t|--timeout ", opt_set_uintval, 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"); -- 2.39.2