command-line: Add function to print command usage.
[cascardo/ovs.git] / tests / ovstest.c
1 /*
2  * Copyright (c) 2014 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /* The mother of all test programs that links with libopevswitch.la */
18
19 #include <config.h>
20 #include <inttypes.h>
21 #include <limits.h>
22 #include <stdlib.h>
23 #include "command-line.h"
24 #include "ovstest.h"
25 #include "dynamic-string.h"
26 #include "util.h"
27
28 static struct command *commands = NULL;
29 static size_t n_commands = 0;
30 static size_t allocated_commands = 0;
31
32 static void
33 add_command(struct command *cmd)
34 {
35     const struct command nil = {NULL, NULL, 0, 0, NULL};
36
37     while (n_commands + 1 >= allocated_commands) {
38         commands = x2nrealloc(commands, &allocated_commands,
39                               sizeof *cmd);
40     }
41
42     commands[n_commands] = *cmd;
43     commands[n_commands + 1] = nil;
44     n_commands++;
45 }
46
47 #define OVSTEST_USAGE \
48 "TEST [TESTARGS] where 'TEST' is a string, 'TESTARGS' are optional \n"\
49 "arguments of the TEST"
50
51 static void
52 flush_help_string(struct ds *ds)
53 {
54     if (ds->length > 2 ) {
55         ds->length -= 2;
56         printf ("%s\n", ds_cstr(ds));
57         ds_clear(ds);
58     }
59 }
60
61 static void
62 help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
63 {
64     const struct command *p;
65     struct ds test_names = DS_EMPTY_INITIALIZER;
66     const int linesize = 70;
67
68     printf("%s: the big test executable\n"
69            "usage: %s TEST [TESTARGS]\n"
70            "where TEST is one of the following. \n\n",
71            program_name, program_name);
72
73     for(p = commands; p->name != NULL; p++) {
74         if (*p->name != '-') { /* Skip internal commands */
75             ds_put_format(&test_names, "%s, ", p->name);
76             if ((test_names.length) >= linesize) {
77                 flush_help_string(&test_names);
78             }
79         }
80     }
81     flush_help_string(&test_names);
82     ds_destroy(&test_names);
83 }
84
85 static void
86 add_top_level_commands(void)
87 {
88     struct command help_cmd = {"--help", NULL, 0, 0, help};
89
90     add_command(&help_cmd);
91 }
92
93 void
94 ovstest_register(const char *test_name, ovstest_func f)
95 {
96     struct command test_cmd;
97
98     test_cmd.name = test_name;
99     test_cmd.usage = NULL;
100     test_cmd.min_args = 0;
101     test_cmd.max_args = INT_MAX;
102     test_cmd.handler = f;
103
104     add_command(&test_cmd);
105 }
106
107 static void
108 cleanup(void)
109 {
110     if (allocated_commands) {
111         free(commands);
112     }
113 }
114
115 int
116 main(int argc, char *argv[])
117 {
118     set_program_name(argv[0]);
119
120     if (argc < 2) {
121         ovs_fatal(0, "expect test program to be specified; "
122                   "use --help for usage");
123     }
124
125     add_top_level_commands();
126     if (argc > 1) {
127         run_command(argc - 1, argv + 1, commands);
128     }
129     cleanup();
130
131     return 0;
132 }