-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_argopt.c
94 lines (70 loc) · 3.09 KB
/
test_argopt.c
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
#include <assert.h>
#include "vita/util/argopt.h"
int32_t main(void) {
vt_mallocator_t *alloctr = vt_mallocator_create();
// strings
vt_str_t *rpath = vt_str_create("my/temp/folder/default/initialized", alloctr);
vt_str_t *wpath = NULL;
// floats
float intensity = 0.1;
float polarity = 0.2;
// bools
bool verbose = false;
bool audio = false;
// ints
int32_t volume = 50;
int32_t level = 1;
// chars
char update = 'n';
char upgrade = 'n';
// arguments
const char *argv[] = {
/* progname: */"./test_argopt", // 1
/* rpath: */ "-r=../temp", // 2
/* wpath: */ "--wpath", "./docs/dw/data/", // 4
/* intensity: */ "--intensity=0.7", // 5
/* polarity: */ "-p", "0.35", // 7
/* verbose: */ "--verbose", // 8
/* audio: */ "-a", "true", // 10
/* volume: */ "-vl=83", // 11
/* level: */ "--level", "13", // 13
/* update: */ "--update=y", // 14
/* upgrade: */ "-ug", "y", // 16
/* unknown: */ "-uo", // 17
/* unknown: */ "--unkownOption", "unkownValue", // 19
// "-r", "../temp", "--wpath", "./docs/dw/data", "--intensity", "0.7"
};
const size_t argc = 19;
// const size_t argc = 7;
// options
vt_argopt_t optv[] = {
{ "--rpath", "-r", "read path", VT_ARGOPT(rpath), VT_TYPE_STR },
{ "--wpath", "-w", "save path", VT_ARGOPT(wpath), VT_TYPE_STR },
{ "--intensity", "-i", "level of intensity between [0; 1]", VT_ARGOPT(intensity), VT_TYPE_FLOAT },
{ "--polarity", "-p", "level of polarity between [0; 1]", VT_ARGOPT(polarity), VT_TYPE_FLOAT },
{ "--verbose", "-v", "verbose output", VT_ARGOPT(verbose), VT_TYPE_BOOL },
{ "--audio", "-a", "include audio", VT_ARGOPT(audio), VT_TYPE_BOOL },
{ "--volume", "-vl", "volume level", VT_ARGOPT(volume), VT_TYPE_INT32 },
{ "--level", "-l", "output level", VT_ARGOPT(level), VT_TYPE_INT32 },
{ "--update", "-ud", "update (y, n)", VT_ARGOPT(update), VT_TYPE_CHAR },
{ "--upgrade", "-ug", "upgrade (y, n)", VT_ARGOPT(upgrade), VT_TYPE_CHAR }
};
const size_t optc = sizeof(optv)/sizeof(vt_argopt_t);
// parse args and opts
const int8_t parse_status = vt_argopt_parse(argc, argv, optc, optv, alloctr);
// check
assert(parse_status == VT_ARGOPT_PARSE_ERROR);
assert(vt_str_equals_z(vt_str_z(rpath), "../temp"));
assert(vt_str_equals_z(vt_str_z(wpath), "./docs/dw/data/"));
assert(intensity == (float)0.7);
assert(polarity == (float)0.35);
assert(volume == 83);
assert(level == 13);
assert(update == 'y');
assert(upgrade == 'y');
// free resources
vt_str_destroy(rpath);
vt_str_destroy(wpath);
vt_mallocator_destroy(alloctr);
return 0;
}