2 #include <ccan/tap/tap.h>
8 #include <ccan/opt/opt.c>
9 #include <ccan/opt/usage.c>
10 #include <ccan/opt/helpers.c>
11 #include <ccan/opt/parse.c>
13 static char *my_cb(void *p)
18 static void reset_options(void)
22 opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
26 int main(int argc, char *argv[])
29 char *longname = strdup("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
30 char *shortname = strdup("shortname");
33 opt_register_table(subtables, NULL);
34 opt_register_noarg("--kkk|-k", my_cb, NULL, "magic kkk option");
35 opt_register_noarg("-?", opt_usage_and_exit, "<MyArgs>...",
37 opt_register_arg("--longname", opt_set_charp, opt_show_charp,
38 &longname, "a really long option default");
39 opt_register_arg("--shortname", opt_set_charp, opt_show_charp,
40 &shortname, "a short option default");
41 output = opt_usage("my name", "ExTrA Args");
43 ok1(strstr(output, "Usage: my name"));
44 ok1(strstr(output, "--jjj|-j|--lll|-l <arg>"));
45 ok1(strstr(output, "ExTrA Args"));
46 ok1(strstr(output, "-a "));
47 ok1(strstr(output, " Description of a\n"));
48 ok1(strstr(output, "-b <arg>"));
49 ok1(strstr(output, " Description of b (default: b)\n"));
50 ok1(strstr(output, "--ddd "));
51 ok1(strstr(output, " Description of ddd\n"));
52 ok1(strstr(output, "--eee <filename> "));
53 ok1(strstr(output, " (default: eee)\n"));
54 ok1(strstr(output, "long table options:\n"));
55 ok1(strstr(output, "--ggg|-g "));
56 ok1(strstr(output, " Description of ggg\n"));
57 ok1(strstr(output, "-h|--hhh <arg>"));
58 ok1(strstr(output, " Description of hhh\n"));
59 ok1(strstr(output, "--kkk|-k"));
60 ok1(strstr(output, "magic kkk option"));
61 /* This entry is hidden. */
62 ok1(!strstr(output, "--mmm|-m"));
65 /* NULL should use string from registered options. */
66 output = opt_usage("my name", NULL);
68 ok1(strstr(output, "Usage: my name"));
69 ok1(strstr(output, "--jjj|-j|--lll|-l <arg>"));
70 ok1(strstr(output, "<MyArgs>..."));
71 ok1(strstr(output, "-a "));
72 ok1(strstr(output, " Description of a\n"));
73 ok1(strstr(output, "-b <arg>"));
74 ok1(strstr(output, " Description of b (default: b)\n"));
75 ok1(strstr(output, "--ddd "));
76 ok1(strstr(output, " Description of ddd\n"));
77 ok1(strstr(output, "--eee <filename> "));
78 ok1(strstr(output, " (default: eee)\n"));
79 ok1(strstr(output, "long table options:\n"));
80 ok1(strstr(output, "--ggg|-g "));
81 ok1(strstr(output, " Description of ggg\n"));
82 ok1(strstr(output, "-h|--hhh <arg>"));
83 ok1(strstr(output, " Description of hhh\n"));
84 ok1(strstr(output, "--kkk|-k"));
85 ok1(strstr(output, "magic kkk option"));
86 ok1(strstr(output, "--longname"));
87 ok1(strstr(output, "a really long option default"));
88 ok1(strstr(output, "(default: \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"...)"));
89 ok1(strstr(output, "--shortname"));
90 ok1(strstr(output, "a short option default"));
91 ok1(strstr(output, "(default: \"shortname\")"));
92 /* This entry is hidden. */
93 ok1(!strstr(output, "--mmm|-m"));
97 /* Empty table test. */
98 output = opt_usage("nothing", NULL);
99 ok1(strstr(output, "Usage: nothing \n"));
103 opt_register_noarg("--aaa", test_noarg, NULL, "AAAAll");
104 output = opt_usage("onearg", NULL);
105 ok1(strstr(output, "Usage: onearg \n"));
106 ok1(strstr(output, "--aaa"));
107 ok1(strstr(output, "AAAAll"));
110 return exit_status();