tests: Fix bug in "ovsdb-tool compact" test.
[sliver-openvswitch.git] / tests / test-command-line.c
1 /*
2  * Copyright (c) 2010 Nicira Networks.
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 #include <config.h>
18 #include "test-command-line.h"
19 #include <getopt.h>
20 #include <limits.h>
21 #include <stdlib.h>
22 #include "command-line.h"
23 #include "timeval.h"
24 #include "util.h"
25 #include "vlog.h"
26
27 static void
28 test_usage(const struct command commands[])
29 {
30     const struct command *p;
31
32     printf("%s: an Open vSwitch test utility\n"
33            "usage: %s [OPTIONS] COMMAND [ARG...]\n\n"
34            "Valid commands:\n" ,
35            program_name, program_name);
36
37     for (p = commands; p->name; p++) {
38         int i;
39
40         printf("  %s", p->name);
41         for (i = 0; i < p->min_args; i++) {
42             printf(" ARG%d", i + 1);
43         }
44         if (p->max_args == INT_MAX) {
45             printf(" [ARG...]");
46         } else if (p->max_args > p->min_args) {
47             for (i = p->min_args; i < p->max_args; i++) {
48                 putchar(' ');
49                 if (i == p->min_args) {
50                     putchar('[');
51                 }
52                 printf("ARG%d", i + 1);
53             }
54             putchar(']');
55         }
56         putchar('\n');
57     }
58     vlog_usage();
59     printf("\nOther options:\n"
60            "  -t, --timeout=SECS          give up after SECS seconds\n"
61            "  -h, --help                  display this help message\n");
62     exit(EXIT_SUCCESS);
63 }
64
65 /* Parses options for test programs that don't have any special needs.
66  * Prints --help output based on 'commands'. */
67 void
68 parse_test_options(int argc, char *argv[],
69                    const struct command commands[])
70 {
71     static struct option long_options[] = {
72         {"timeout", required_argument, 0, 't'},
73         {"verbose", optional_argument, 0, 'v'},
74         {"help", no_argument, 0, 'h'},
75         {0, 0, 0, 0},
76     };
77     char *short_options = long_options_to_short_options(long_options);
78
79     for (;;) {
80         unsigned long int timeout;
81         int c;
82
83         c = getopt_long(argc, argv, short_options, long_options, NULL);
84         if (c == -1) {
85             break;
86         }
87
88         switch (c) {
89         case 't':
90             timeout = strtoul(optarg, NULL, 10);
91             if (timeout <= 0) {
92                 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
93                           optarg);
94             } else {
95                 time_alarm(timeout);
96             }
97             break;
98
99         case 'h':
100             test_usage(commands);
101
102         case 'v':
103             vlog_set_verbosity(optarg);
104             break;
105
106         case '?':
107             exit(EXIT_FAILURE);
108
109         default:
110             abort();
111         }
112     }
113     free(short_options);
114 }
115