]> git.ozlabs.org Git - ccan/blob - ccan/opt/test/run-usage.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / opt / test / run-usage.c
1 #include <ccan/tap/tap.h>
2 #include <stdarg.h>
3 #include <setjmp.h>
4 #include <stdlib.h>
5 #include <stdarg.h>
6 #include "utils.h"
7
8 /* Ensure width is sane. */
9 static const char *getenv_override(const char *name UNNEEDED)
10 {
11         return "100";
12 }
13
14 #define getenv getenv_override
15
16 #include <ccan/opt/opt.c>
17 #include <ccan/opt/usage.c>
18 #include <ccan/opt/helpers.c>
19 #include <ccan/opt/parse.c>
20
21 static char *my_cb(void *p UNNEEDED)
22 {
23         return NULL;
24 }
25
26 /* Test helpers. */
27 int main(void)
28 {
29         char *output;
30         char *longname = strdup("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
31         char *shortname = strdup("shortname");
32
33         plan_tests(51);
34         opt_register_table(subtables, NULL);
35         opt_register_noarg("--kkk|-k", my_cb, NULL, "magic kkk option");
36         opt_register_noarg("-?", opt_usage_and_exit, "<MyArgs>...",
37                            "This message");
38         opt_register_arg("--longname", opt_set_charp, opt_show_charp,
39                          &longname, "a really long option default");
40         opt_register_arg("--shortname", opt_set_charp, opt_show_charp,
41                          &shortname, "a short option default");
42         output = opt_usage("my name", "ExTrA Args");
43         diag("%s", output);
44         ok1(strstr(output, "Usage: my name"));
45         ok1(strstr(output, "--jjj|-j|--lll|-l <arg>"));
46         ok1(strstr(output, "ExTrA Args"));
47         ok1(strstr(output, "-a "));
48         ok1(strstr(output, " Description of a\n"));
49         ok1(strstr(output, "-b <arg>"));
50         ok1(strstr(output, " Description of b (default: b)\n"));
51         ok1(strstr(output, "--ddd "));
52         ok1(strstr(output, " Description of ddd\n"));
53         ok1(strstr(output, "--eee <filename> "));
54         ok1(strstr(output, " (default: eee)\n"));
55         ok1(strstr(output, "long table options:\n"));
56         ok1(strstr(output, "--ggg|-g "));
57         ok1(strstr(output, " Description of ggg\n"));
58         ok1(strstr(output, "-h|--hhh <arg>"));
59         ok1(strstr(output, " Description of hhh\n"));
60         ok1(strstr(output, "--kkk|-k"));
61         ok1(strstr(output, "magic kkk option"));
62         /* This entry is hidden. */
63         ok1(!strstr(output, "--mmm|-m"));
64         free(output);
65
66         /* NULL should use string from registered options. */
67         output = opt_usage("my name", NULL);
68         diag("%s", output);
69         ok1(strstr(output, "Usage: my name"));
70         ok1(strstr(output, "--jjj|-j|--lll|-l <arg>"));
71         ok1(strstr(output, "<MyArgs>..."));
72         ok1(strstr(output, "-a "));
73         ok1(strstr(output, " Description of a\n"));
74         ok1(strstr(output, "-b <arg>"));
75         ok1(strstr(output, " Description of b (default: b)\n"));
76         ok1(strstr(output, "--ddd "));
77         ok1(strstr(output, " Description of ddd\n"));
78         ok1(strstr(output, "--eee <filename> "));
79         ok1(strstr(output, " (default: eee)\n"));
80         ok1(strstr(output, "long table options:\n"));
81         ok1(strstr(output, "--ggg|-g "));
82         ok1(strstr(output, " Description of ggg\n"));
83         ok1(strstr(output, "-h|--hhh <arg>"));
84         ok1(strstr(output, " Description of hhh\n"));
85         ok1(strstr(output, "--kkk|-k"));
86         ok1(strstr(output, "magic kkk option"));
87         ok1(strstr(output, "--longname"));
88         ok1(strstr(output, "a really long option default"));
89         ok1(strstr(output, "(default: \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"...)"));
90         ok1(strstr(output, "--shortname"));
91         ok1(strstr(output, "a short option default"));
92         ok1(strstr(output, "(default: \"shortname\")"));
93         /* This entry is hidden. */
94         ok1(!strstr(output, "--mmm|-m"));
95         free(output);
96
97         reset_options();
98         /* Empty table test. */
99         output = opt_usage("nothing", NULL);
100         ok1(strstr(output, "Usage: nothing \n"));
101         free(output);
102
103         /* No short args. */
104         opt_register_noarg("--aaa", test_noarg, NULL, "AAAAll");
105         output = opt_usage("onearg", NULL);
106         ok1(strstr(output, "Usage: onearg \n"));
107         ok1(strstr(output, "--aaa"));
108         ok1(strstr(output, "AAAAll"));
109         free(output);
110
111         reset_options();
112         /* Valgrind nails this to 100 anyway :( */
113         setenv("COLUMNS", "100", 1);
114         opt_register_noarg("--long", my_cb, NULL, "Extremely long option which requires more than one line for its full description to be shown in the usage message.");
115         opt_register_noarg("--split", my_cb, NULL, "New line in\nlong option which requires more than one line for its full description to be shown in the usage message.");
116         output = opt_usage("longarg", NULL);
117         diag("%s", output);
118         ok1(strstr(output, "Usage: longarg \n"));
119         ok1(strstr(output, "\n"
120                    "--long   Extremely long option which requires more than one line for its full description to be\n"
121                    "         shown in the usage message.\n"));
122         ok1(strstr(output, "\n"
123                    "--split  New line in\n"
124                    "         long option which requires more than one line for its full description to be shown in the\n"
125                    "         usage message.\n"));
126         free(output);
127
128         free(shortname);
129         free(longname);
130         return exit_status();
131 }