]> git.ozlabs.org Git - ccan/blob - ccan/opt/test/run-usage.c
37e8993fe8bde451bdbf713ba65c222e2b623171
[ccan] / ccan / opt / test / run-usage.c
1 #define _GNU_SOURCE
2 #include <ccan/tap/tap.h>
3 #include <stdarg.h>
4 #include <setjmp.h>
5 #include <stdlib.h>
6 #include <stdarg.h>
7 #include "utils.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>
12
13 static char *my_cb(void *p)
14 {
15         return NULL;
16 }
17
18 static void reset_options(void)
19 {
20         free(opt_table);
21         opt_table = NULL;
22         opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
23 }
24
25 /* Test helpers. */
26 int main(int argc, char *argv[])
27 {
28         char *output;
29         char *longname = strdup("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
30         char *shortname = strdup("shortname");
31
32         plan_tests(48);
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>...",
36                            "This message");
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");
42         diag("%s", output);
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"));
63         free(output);
64
65         /* NULL should use string from registered options. */
66         output = opt_usage("my name", NULL);
67         diag("%s", output);
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"));
94         free(output);
95
96         reset_options();
97         /* Empty table test. */
98         output = opt_usage("nothing", NULL);
99         ok1(strstr(output, "Usage: nothing \n"));
100         free(output);
101
102         /* No short args. */
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"));
108         free(output);
109
110         return exit_status();
111 }