-
Notifications
You must be signed in to change notification settings - Fork 5
/
flag.h
116 lines (87 loc) · 1.96 KB
/
flag.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
111
112
113
114
115
116
#ifndef _FLAG_H_
#define _FLAG_H_
#include <stdbool.h>
#include <stdio.h>
/*
* Max flags that may be defined.
*/
#define FLAGS_MAX 128
/*
* Max arguments supported for set->argv.
*/
#define FLAGS_MAX_ARGS 128
/*
* Flag errors.
*/
typedef enum {
FLAG_OK,
FLAG_ERROR_PARSING,
FLAG_ERROR_ARG_MISSING,
FLAG_ERROR_UNDEFINED_FLAG
} flag_error;
/*
* Flag types supported.
*/
typedef enum {
FLAG_TYPE_INT,
FLAG_TYPE_BOOL,
FLAG_TYPE_STRING
} flag_type;
/*
* Flag represents as single user-defined
* flag with a name, help description,
* type, and pointer to a value which
* is replaced upon precense of the flag.
*/
typedef struct {
const char *name;
const char *help;
flag_type type;
void *value;
} flag_t;
/*
* Flagset contains a number of flags,
* and is populated wth argc / argv with the
* remaining arguments.
*
* In the event of an error the error union
* is populated with either the flag or the
* associated argument.
*/
typedef struct {
const char *usage;
int nflags;
flag_t flags[FLAGS_MAX];
int argc;
const char *argv[FLAGS_MAX_ARGS];
union {
flag_t *flag;
const char *arg;
} error;
} flagset_t;
// Flagset.
flagset_t *
flagset_new();
void
flagset_free(flagset_t *self);
void
flagset_int(flagset_t *self, int *value, const char *name, const char *help);
void
flagset_bool(flagset_t *self, bool *value, const char *name, const char *help);
void
flagset_string(flagset_t *self, const char **value, const char *name, const char *help);
void
flagset_write_usage(flagset_t *self, FILE *fp, const char *name);
flag_error
flagset_parse(flagset_t *self, int argc, const char **args);
// Singleton.
void
flag_int(int *value, const char *name, const char *help);
void
flag_bool(bool *value, const char *name, const char *help);
void
flag_string(const char **value, const char *name, const char *help);
void
flag_parse(int argc, const char **args, const char *version);
#define flag_str flag_string
#endif /* _FLAG_H_ */