-
Notifications
You must be signed in to change notification settings - Fork 0
/
opts.h
110 lines (97 loc) · 4.17 KB
/
opts.h
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
99
100
101
102
103
104
105
106
107
108
109
110
#ifndef OPTS_H__INCL
#define OPTS_H__INCL
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
#define OPT_PARSE_START(argc, argv, usage) \
do { \
int _opt_parse_index; \
int _opt_parse_print_help = 0; \
int _opt_parse_exit_code = 0; \
for (_opt_parse_index = 1; _opt_parse_index < (argc);) { \
const char* _opt_parse_option = (argv)[_opt_parse_index]; \
const char* _opt_parse_argument = _opt_parse_index + 1 < (argc)? argv[_opt_parse_index + 1] : NULL; \
int _opt_parse_has_argument = 0; \
int _opt_parse_parsed = 0; \
if (_opt_parse_print_help) { \
fprintf(stderr, "Usage: %s %s\n", (argv)[0], usage); \
fprintf(stderr, "-h, --help\n print this message and exit\n"); \
} else { \
if (!strcmp(_opt_parse_option, "-h") || !strcmp(_opt_parse_option, "--help")) { \
_opt_parse_print_help = 1; \
continue; \
} \
} \
do { \
} while (0)
#define OPT_PARSE_END() \
if (_opt_parse_print_help) { \
exit(_opt_parse_exit_code); \
} \
if (!_opt_parse_parsed) { \
fprintf(stderr, "Error: Unknown argument '%s'\n", _opt_parse_option); \
_opt_parse_print_help = 1; \
_opt_parse_exit_code = 1; \
_opt_parse_index = 0; \
continue; \
} \
if (_opt_parse_has_argument) { \
++_opt_parse_index; \
} \
++_opt_parse_index; \
} \
} while (0)
#define OPT_FLAG(var, sname, lname, help) \
do { \
if (_opt_parse_print_help) { \
if (sname) { fprintf(stderr, "%s%s", sname, (lname)? ", ": ""); } \
if (lname) { fprintf(stderr, "%s", lname); } \
fprintf(stderr, "\n"); \
fprintf(stderr, " %s\n", help) \
} else { \
if ((sname && !strcmp(_opt_parse_option, sname)) || (lname && !strcmp(_opt_parse_option, lname))) { \
_opt_parse_parsed = 1; \
var = 1; \
} \
} \
} while (0)
#define OPT_ARG(name, help) \
do { \
if (_opt_parse_print_help) { \
if (sname) { fprintf(stderr, "Free arguments %s\n", name); } \
fprintf(stderr, " %s\n", help) \
} else if (_opt_parse_option[0] != '-') { \
_opt_parse_parsed = 1; \
action(_opt_parse_option); \
} \
} while (0)
#define OPT_OPTION_WITH_ARGUMENT(parser, type, sname, lname, help) \
if (_opt_parse_print_help) { \
if (sname) { fprintf(stderr, "%s%s", sname, (lname)? ", ": ""); } \
if (lname) { fprintf(stderr, "%s", lname); } \
fprintf(stderr, " <%s>\n", type); \
fprintf(stderr, " %s\n", help); \
} else { \
if (_opt_parse_argument == NULL) { \
fprintf(stderr, "Error: options '%s' needs argument.\n", _opt_parse_option); \
_opt_parse_print_help = 1; \
_opt_parse_exit_code = 1; \
continue; \
} \
if ((sname && !strcmp(_opt_parse_option, sname)) || (lname && !strcmp(_opt_parse_option, lname))) { \
_opt_parse_parsed = 1; \
_opt_parse_has_argument = 1; \
{ \
parser; \
} \
} \
} do {} while(0)
#define OPT_STR(var, sname, lname, help) \
OPT_OPTION_WITH_ARGUMENT(var = _opt_parse_argument, "str", sname, lname, help)
#define OPT_INT(var, sname, lname, help) \
OPT_OPTION_WITH_ARGUMENT(char *endptr = NULL; var = strtol(_opt_parse_argument, &endptr, 0); if (endptr == NULL) { _opt_parse_print_help = 1; _opt_parse_exit_code = 1; continue; }, "str", sname, lname, help)
#ifdef __cplusplus
} // extern "C"
#endif
#endif // OPTS_H__INCL