1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
#include <usual/getopt.h>
#include "test_common.h"
#include <usual/string.h>
#include <stdarg.h>
static const char *xgetopt(const char *opts, const struct option *lopts, ...)
{
static char resbuf[1024];
int i, c, argc = 1;
char *argv[100];
va_list ap;
char *p = resbuf;
resbuf[0] = 'X';
resbuf[1] = 0;
argv[0] = "prog";
va_start(ap, lopts);
while (1) {
argv[argc] = va_arg(ap, char *);
if (!argv[argc])
break;
argc++;
}
va_end(ap);
opterr = 0;
optind = 0;
while (1) {
if (lopts)
c = getopt_long(argc, argv, opts, lopts, NULL);
else
c = getopt(argc, argv, opts);
if (c == -1)
break;
switch (c) {
case '?':
return "ERR";
case ':':
return "EARG";
case 0:
break;
default:
if (p != resbuf)
*p++ = ',';
if (optarg)
p += sprintf(p, "%c=%s", c, optarg);
else
p += sprintf(p, "%c", c);
}
}
for (i = optind; i < argc; i++)
p += sprintf(p, "|%s", argv[i]);
return resbuf;
}
static void test_getopt(void *_)
{
str_check(xgetopt("ab:", NULL, "-abFOO", "zzz", NULL), "a,b=FOO|zzz");
str_check(xgetopt("ab:", NULL, "-a", "zzz", "-bFOO", NULL), "a,b=FOO|zzz");
str_check(xgetopt("ab:", NULL, "-b", "FOO", "-", "--", "-a", NULL), "b=FOO|-|-a");
str_check(xgetopt("ab:", NULL, "--foo", NULL), "ERR");
end:;
}
static void test_getopt_long(void *_)
{
static int longc;
static const char sopts[] = "ab:";
static const struct option lopts[] = {
{ "longa", no_argument, NULL, 'a'},
{ "longb", required_argument, NULL, 'b'},
{ "longc", no_argument, &longc, 'C'},
{ NULL },
};
str_check(xgetopt(sopts, lopts, "--longa", "--", "--longa", NULL), "a|--longa");
str_check(xgetopt(sopts, lopts, "--longb", "FOO", "ARG", "--longa", NULL), "b=FOO,a|ARG");
str_check(xgetopt(sopts, lopts, "--longb=BAZ", NULL), "b=BAZ");
str_check(xgetopt(sopts, lopts, "--longb", NULL), "ERR");
str_check(xgetopt(sopts, lopts, "--xx", NULL), "ERR");
str_check(xgetopt(sopts, lopts, "-", "--longc", "ARG", NULL), "|-|ARG");
tt_assert(longc == 'C');
end:;
}
struct testcase_t getopt_tests[] = {
{ "getopt", test_getopt },
{ "getopt_long", test_getopt_long },
END_OF_TESTCASES
};
|