Commit 7e381e6c authored by Rusty Russell's avatar Rusty Russell

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).
parent b23be29f
...@@ -17,7 +17,7 @@ const char *opt_argv0; ...@@ -17,7 +17,7 @@ const char *opt_argv0;
/* Returns string after first '-'. */ /* Returns string after first '-'. */
static const char *first_name(const char *names, unsigned *len) 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, ...), ...@@ -275,7 +275,7 @@ static void parse_fail(void (*errlog)(const char *fmt, ...),
errlog("%s: -%c: %s", opt_argv0, shortopt, problem); errlog("%s: -%c: %s", opt_argv0, shortopt, problem);
else else
errlog("%s: --%.*s: %s", opt_argv0, errlog("%s: --%.*s: %s", opt_argv0,
strcspn(longopt, "/"), longopt, problem); strcspn(longopt, "|"), longopt, problem);
} }
/* Parse your arguments. */ /* Parse your arguments. */
......
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
/* You can use this directly to build tables, but the macros will ensure /* You can use this directly to build tables, but the macros will ensure
* consistency and type safety. */ * consistency and type safety. */
enum opt_type { enum opt_type {
OPT_NOARG = 1, /* -f/--foo */ OPT_NOARG = 1, /* -f|--foo */
OPT_HASARG = 2, /* -f arg/--foo=arg/--foo arg */ OPT_HASARG = 2, /* -f arg|--foo=arg|--foo arg */
OPT_SUBTABLE = 4, /* Actually, longopt points to a subtable... */ OPT_SUBTABLE = 4, /* Actually, longopt points to a subtable... */
OPT_END = 8, /* End of the table. */ OPT_END = 8, /* End of the table. */
}; };
...@@ -16,7 +16,7 @@ enum opt_type { ...@@ -16,7 +16,7 @@ enum opt_type {
#define OPT_SHOW_LEN 80 #define OPT_SHOW_LEN 80
struct opt_table { 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; enum opt_type type;
char *(*cb)(void *arg); /* OPT_NOARG */ char *(*cb)(void *arg); /* OPT_NOARG */
char *(*cb_arg)(const char *optarg, void *arg); /* OPT_HASARG */ char *(*cb_arg)(const char *optarg, void *arg); /* OPT_HASARG */
...@@ -27,7 +27,7 @@ struct opt_table { ...@@ -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. * @cb: the callback when the option is found.
* @arg: the argument to hand to @cb. * @arg: the argument to hand to @cb.
* @desc: the description for opt_usage(), or opt_hidden. * @desc: the description for opt_usage(), or opt_hidden.
...@@ -41,7 +41,7 @@ struct opt_table { ...@@ -41,7 +41,7 @@ struct opt_table {
* string and return false. * string and return false.
* *
* Any number of equivalent short or long options can be listed in @names, * 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. * character, long options are two hypens followed by one or more characters.
* *
* See Also: * See Also:
...@@ -52,7 +52,7 @@ struct opt_table { ...@@ -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>). * @cb: the callback when the option is found (along with <arg>).
* @show: the callback to print the value in get_usage (or NULL) * @show: the callback to print the value in get_usage (or NULL)
* @arg: the argument to hand to @cb and @show * @arg: the argument to hand to @cb and @show
...@@ -70,7 +70,7 @@ struct opt_table { ...@@ -70,7 +70,7 @@ struct opt_table {
* nul-terminate that buffer. * nul-terminate that buffer.
* *
* Any number of equivalent short or long options can be listed in @names, * 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. * 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 * A space or equals in @names is ignored for parsing, and only used
* for printing the usage. * for printing the usage.
...@@ -128,7 +128,7 @@ void opt_register_table(const struct opt_table table[], const char *desc); ...@@ -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. * @cb: the callback when the option is found.
* @arg: the argument to hand to @cb. * @arg: the argument to hand to @cb.
* @desc: the verbose desction of the option (for opt_usage()), or NULL. * @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); ...@@ -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. * @cb: the callback when the option is found.
* @show: the callback to print the value in get_usage (or NULL) * @show: the callback to print the value in get_usage (or NULL)
* @arg: the argument to hand to @cb. * @arg: the argument to hand to @cb.
...@@ -172,7 +172,7 @@ void opt_register_table(const struct opt_table table[], const char *desc); ...@@ -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) \ #define opt_register_arg(names, cb, show, arg, desc) \
_opt_register((names), OPT_CB_ARG((cb), (show), (arg)), (desc)) _opt_register((names), OPT_CB_ARG((cb), (show), (arg)), (desc))
......
...@@ -12,7 +12,7 @@ int main(int argc, char *argv[]) ...@@ -12,7 +12,7 @@ int main(int argc, char *argv[])
plan_tests(12); plan_tests(12);
/* --aaa without args. */ /* --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(!parse_args(&argc, &argv, "--aaa", NULL));
ok1(strstr(err_output, ": --aaa: option requires an argument")); ok1(strstr(err_output, ": --aaa: option requires an argument"));
free(err_output); free(err_output);
...@@ -23,7 +23,7 @@ int main(int argc, char *argv[]) ...@@ -23,7 +23,7 @@ int main(int argc, char *argv[])
err_output = NULL; err_output = NULL;
/* Multiple */ /* 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(!parse_args(&argc, &argv, "--bbb", NULL));
ok1(strstr(err_output, ": --bbb: option requires an argument")); ok1(strstr(err_output, ": --bbb: option requires an argument"));
free(err_output); free(err_output);
......
...@@ -21,13 +21,13 @@ int main(int argc, char *argv[]) ...@@ -21,13 +21,13 @@ int main(int argc, char *argv[])
plan_tests(38); plan_tests(38);
opt_register_table(subtables, NULL); 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>...", opt_register_noarg("-?", opt_usage_and_exit, "<MyArgs>...",
"This message"); "This message");
output = opt_usage("my name", "ExTrA Args"); output = opt_usage("my name", "ExTrA Args");
diag("%s", output); diag("%s", output);
ok1(strstr(output, "Usage: my name")); 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, "ExTrA Args"));
ok1(strstr(output, "-a ")); ok1(strstr(output, "-a "));
ok1(strstr(output, " Description of a\n")); ok1(strstr(output, " Description of a\n"));
...@@ -38,21 +38,21 @@ int main(int argc, char *argv[]) ...@@ -38,21 +38,21 @@ int main(int argc, char *argv[])
ok1(strstr(output, "--eee <filename> ")); ok1(strstr(output, "--eee <filename> "));
ok1(strstr(output, " (default: eee)\n")); ok1(strstr(output, " (default: eee)\n"));
ok1(strstr(output, "long table options:\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")); ok1(strstr(output, "magic kkk option"));
/* This entry is hidden. */ /* This entry is hidden. */
ok1(!strstr(output, "--mmm/-m")); ok1(!strstr(output, "--mmm|-m"));
free(output); free(output);
/* NULL should use string from registered options. */ /* NULL should use string from registered options. */
output = opt_usage("my name", NULL); output = opt_usage("my name", NULL);
diag("%s", output); diag("%s", output);
ok1(strstr(output, "Usage: my name")); 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, "<MyArgs>..."));
ok1(strstr(output, "-a ")); ok1(strstr(output, "-a "));
ok1(strstr(output, " Description of a\n")); ok1(strstr(output, " Description of a\n"));
...@@ -63,14 +63,14 @@ int main(int argc, char *argv[]) ...@@ -63,14 +63,14 @@ int main(int argc, char *argv[])
ok1(strstr(output, "--eee <filename> ")); ok1(strstr(output, "--eee <filename> "));
ok1(strstr(output, " (default: eee)\n")); ok1(strstr(output, " (default: eee)\n"));
ok1(strstr(output, "long table options:\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")); ok1(strstr(output, "magic kkk option"));
/* This entry is hidden. */ /* This entry is hidden. */
ok1(!strstr(output, "--mmm/-m")); ok1(!strstr(output, "--mmm|-m"));
free(output); free(output);
return exit_status(); return exit_status();
......
...@@ -37,7 +37,7 @@ int main(int argc, char *argv[]) ...@@ -37,7 +37,7 @@ int main(int argc, char *argv[])
ok1(test_cb_called == 2); ok1(test_cb_called == 2);
/* Both long and short args. */ /* 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(parse_args(&argc, &argv, "--aaa", "-a", NULL));
ok1(argc == 1); ok1(argc == 1);
ok1(argv[0] == myname); ok1(argv[0] == myname);
...@@ -55,7 +55,7 @@ int main(int argc, char *argv[]) ...@@ -55,7 +55,7 @@ int main(int argc, char *argv[])
/* Argument variants. */ /* Argument variants. */
reset_options(); reset_options();
test_cb_called = 0; 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(parse_args(&argc, &argv, "--aaa", "aaa", NULL));
ok1(argc == 1); ok1(argc == 1);
ok1(argv[0] == myname); ok1(argv[0] == myname);
......
...@@ -84,17 +84,17 @@ struct opt_table long_table[] = { ...@@ -84,17 +84,17 @@ struct opt_table long_table[] = {
struct opt_table long_and_short_table[] = { struct opt_table long_and_short_table[] = {
/* Short and long, different args. */ /* Short and long, different args. */
OPT_WITHOUT_ARG("--ggg/-g", test_noarg, "ggg", "Description of ggg"), OPT_WITHOUT_ARG("--ggg|-g", test_noarg, "ggg", "Description of ggg"),
OPT_WITH_ARG("-h/--hhh", test_arg, NULL, "hhh", "Description of hhh"), OPT_WITH_ARG("-h|--hhh", test_arg, NULL, "hhh", "Description of hhh"),
OPT_ENDTABLE OPT_ENDTABLE
}; };
/* Sub-table test. */ /* Sub-table test. */
struct opt_table subtables[] = { struct opt_table subtables[] = {
/* Two short, and two long long, no description */ /* 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(short_table, NULL),
OPT_SUBTABLE(long_table, "long table options"), OPT_SUBTABLE(long_table, "long table options"),
OPT_SUBTABLE(long_and_short_table, NULL), OPT_SUBTABLE(long_and_short_table, NULL),
......
...@@ -363,24 +363,24 @@ int main(int argc, char *argv[]) ...@@ -363,24 +363,24 @@ int main(int argc, char *argv[])
cmdline_exclude = btree_new(btree_strcmp); cmdline_exclude = btree_new(btree_strcmp);
info_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, NULL, &timeout,
"ignore (terminate) tests that are slower than this"); "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" "\nA program for checking and guiding development"
" of CCAN modules.", " of CCAN modules.",
"This usage message"); "This usage message");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment