-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommon.h
More file actions
195 lines (168 loc) · 6.84 KB
/
Common.h
File metadata and controls
195 lines (168 loc) · 6.84 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#pragma once
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#ifdef MLS_EXPORT_LIBRARY
#ifdef _WIN32
#ifdef MLS_LIB_COMPILE
#define MLS_PUBLIC_API __declspec(dllexport)
#else
#define MLS_PUBLIC_API __declspec(dllimport)
#endif
#else
#define MLS_PUBLIC_API
#endif
#else
#define MLS_PUBLIC_API
#endif
#ifdef __cplusplus
extern "C"
{
#endif
#ifdef __cplusplus
#define CLITERAL(x, ...) \
[]() { \
static x b[] = { __VA_ARGS__ }; \
return b; \
}()
#else
#define CLITERAL(x, ...) (x){ __VA_ARGS__ }
#endif
// Initialise a pointer with an array inline
#define UCLI_MAKE_FLAG_ARRAY(...) CLITERAL(UCLI_Flag, __VA_ARGS__)
// Initialise a pointer with an array inline
#define UCLI_MAKE_COMMAND_ARRAY(...) CLITERAL(UCLI_Command, __VA_ARGS__)
// Initialise a pointer with an array inline
#define UCLI_MAKE_STRING_ARRAY(...) CLITERAL(const char*, __VA_ARGS__)
typedef enum UCLI_CommandType
{
UCLI_COMMAND_TYPE_VOID = 0,
UCLI_COMMAND_TYPE_BOOL = 0,
UCLI_COMMAND_TYPE_STRING = 1,
UCLI_COMMAND_TYPE_ARRAY = 2
} UCLI_CommandType;
typedef enum UCLI_CallbackResult
{
UCLI_CALLBACK_RESULT_OK = 0,
UCLI_CALLBACK_RESULT_PREMATURE_EXIT = 1,
} UCLI_CallbackResult;
typedef struct UCLI_Command UCLI_Command;
typedef struct UCLI_Flag UCLI_Flag;
typedef UCLI_CallbackResult(*UCLI_CommandEvent)(const UCLI_Command*);
typedef UCLI_CallbackResult(*UCLI_FlagEvent)(const UCLI_Flag*);
/**
* @var longName - the long name of the flag. Example: "my-flag" -> `--my-flag`
* @var shortName - the short name of the flag. Example: "f" -> `-f`
* @var description - the help description of the flag. Only used if using the auto-generated help command
*
* @var defaultValues - A list of default string values for the flag
* @var defaultValuesCount - The size of the list of default values for the flag
*
* @var type - The data type of the flag(void, bool, string or array)
*
* @var boolValue - Used if the type of the flag is bool
* @var stringValues - An array of strings. Used if the type is string or array.
* @var stringValuesCount - The size of the strings array
*
* @var callback - A callback that will be called if the flag is encountered
* @var context - A user-defined context pointer
*
* @var useLiteralFlags - If the flag is of type UCLI_COMMAND_TYPE_ARRAY, determines whether we interpret flags in
* an array element sequence(--flag a b c d) literally as array members, or as a terminator. By default, you should
* be terminating. This is useful for applications that pass arguments to other applications verbatim
*
* @var priority - If the flag's priority is below (SIZE_MAX / 2), then the flag is pushed before any other flags
* with a lower priority for the flag's command depth. If the priority is higher, the flag is moved to the front of
* the command queue. SIZE_MAX is the highest priority. These commands are guaranteed to be called first, with the
* default help command being set to this exact priority if used.
*/
typedef struct MLS_PUBLIC_API UCLI_Flag
{
const char* longName;
char shortName;
const char* description;
const char** defaultValues;
size_t defaultValuesCount;
union
{
struct
{
const char** stringValues;
size_t stringValuesCount;
struct
{
bool _bFreeStringValues;
bool _bFreeInnerStringValues;
} _internal_;
} stringValues;
bool* boolValue;
};
UCLI_CommandType type;
UCLI_FlagEvent callback;
void* context;
bool useLiteralFlags;
// CAUTION: Values from SIZE_MAX / 2 to SIZE_MAX are placed at the front of the callback queue and are sorted
// at a command depth of -1, while values from 0 to SIZE_MAX / 2 are sorted at the command depth of the flag
size_t priority;
char* _internal_ctx_;
} UCLI_Flag;
/**
* @var longName - the long name of the command. Example: "my-command" -> `my-command`
* @var shortName - the short name of the command. Example: "f" -> `f`
* @var description - the help description of the command. Only used if using the auto-generated help command
*
* @var defaultValues - A list of default string values for the command
* @var defaultValuesCount - The size of the list of default values for the command
*
* @var type - The data type of the command(void, bool, string or array)
*
* @var boolValue - Used if the type of the command is bool
* @var stringValues - An array of strings. Used if the type is string or array.
* @var stringValuesCount - The size of the strings array
*
* @var callback - A callback that will be called if the command is encountered
* @var context - A user-defined context pointer
*
* @var useLiteralFlags - If the flag is of type UCLI_COMMAND_TYPE_ARRAY, determines whether we interpret flags in
* an array element sequence(--flag a b c d) literally as array members, or as a terminator. By default, you should
* be terminating. This is useful for applications that pass arguments to other applications verbatim
*/
typedef struct MLS_PUBLIC_API UCLI_Command
{
const char* longName;
char shortName;
const char* description;
const char** defaultValues;
size_t defaultValuesCount;
union
{
struct
{
const char** stringValues;
size_t stringValuesCount;
struct
{
bool _bFreeStringValues;
bool _bFreeInnerStringValues;
} _internal_;
} stringValues;
bool* boolValue;
};
UCLI_CommandType type;
UCLI_Command* subcommands;
size_t subcommandsCount;
UCLI_Flag* flags;
size_t flagsCount;
UCLI_CommandEvent callback;
void* context;
bool useLiteralFlags;
UCLI_Command* _internal_parent;
char* _internal_ctx_;
} UCLI_Command;
MLS_PUBLIC_API UCLI_CallbackResult UCLI_EMPTY_FLAG_CALLBACK(const UCLI_Flag* flag);
MLS_PUBLIC_API UCLI_CallbackResult UCLI_EMPTY_COMMAND_CALLBACK(const UCLI_Command* flag);
MLS_PUBLIC_API UCLI_CallbackResult UCLI_Parser_helpCommandC(const UCLI_Command* command);
MLS_PUBLIC_API UCLI_CallbackResult UCLI_Parser_helpCommandF(const UCLI_Flag* flag);
#ifdef __cplusplus
}
#endif