-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patharg.inl
More file actions
279 lines (256 loc) · 8.16 KB
/
Copy patharg.inl
File metadata and controls
279 lines (256 loc) · 8.16 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <stdbool.h>
#include <tchar.h>
bool* g_argb = NULL;
int g_argc = 0;
const TCHAR* const* g_argv = NULL;
const TCHAR* g_argdescription = NULL;
bool g_argshowUsage = false;
typedef struct TagArgArgDescription
{
const TCHAR* arg;
const TCHAR* value;
const TCHAR* desc;
} ArgArgDescription;
ArgArgDescription g_argargdescription[20];
int g_argargdescriptioncount = 0;
typedef struct TagArgArgNumDescription
{
const TCHAR* arg;
const TCHAR* desc;
bool used;
} ArgArgNumDescription;
ArgArgNumDescription g_argargnumdescription[10];
int g_argargnumdescriptioncount = 0;
int g_argargnumoptional = 65536;
#ifdef __cplusplus
#define ARG_OPTIONAL(x) = x
#else
#define ARG_OPTIONAL(x)
#endif
#define ARG_COLOR(n, s) "\x1B[" #n "m" s "\x1B[0m"
#ifdef _WINDOWS
#define ARG_COMMAND(s) s
#define ARG_OPTION(s) s
#define ARG_VALUE(s) s
#define ARG_ERROR(s) s
#else
#define ARG_COMMAND(s) ARG_COLOR(37, s)
#define ARG_OPTION(s) ARG_COLOR(33, s)
#define ARG_VALUE(s) ARG_COLOR(36, s)
#define ARG_ERROR(s) ARG_COLOR(31, s)
#endif
bool argswitch(const TCHAR* argf, const TCHAR* desc);
void arginit(int argc, const TCHAR* const argv[], const TCHAR* argdescription ARG_OPTIONAL(NULL))
{
g_argc = argc;
g_argv = argv;
g_argb = (bool*) malloc(argc * sizeof(bool));
for (int argi = 1; argi < g_argc; ++argi)
{
g_argb[argi] = false;
}
g_argdescription = argdescription;
}
const TCHAR* argapp()
{
const TCHAR* app = _tcsrchr(g_argv[0], _T('\\'));
return app == NULL ? g_argv[0] : app + 1;
}
bool argcleanup()
{
#ifdef _WINDOWS
TCHAR msg[1024] = TEXT("");
int msgoffset = 0;
#define msg(...) msgoffset += _stprintf_s(msg + msgoffset, ARRAYSIZE(msg) - msgoffset, __VA_ARGS__)
#else
#define msg(...) _ftprintf(stderr, __VA_ARGS__)
#endif
g_argshowUsage = argswitch(_T("/?"), _T("Show usage"));
bool ret = true;
for (int argi = 1; argi < g_argc; ++argi)
{
if (!g_argb[argi])
{
const TCHAR* arg = g_argv[argi];
if (arg[0] == '/')
{
msg(_T(ARG_ERROR("Unknown option:") " \"%s\".\n"), arg);
ret = false;
}
else
{
msg(_T(ARG_ERROR("Unknown argument:") " \"%s\".\n"), arg);
ret = false;
}
}
}
if (!g_argshowUsage)
{
for (int i = 0; i < g_argargnumdescriptioncount && i < g_argargnumoptional; ++i)
{
const ArgArgNumDescription* argargnumdescription = &g_argargnumdescription[i];
if (!argargnumdescription->used)
{
msg(_T(ARG_ERROR("Missing argument:") " %s \"%s\".\n"), argargnumdescription->arg, argargnumdescription->desc);
ret = false;
}
}
}
free(g_argb);
g_argb = NULL;
#ifdef _WINDOWS
if (msg[0] != TEXT('\0'))
MessageBox(NULL, msg, argapp(), MB_OK | MB_ICONERROR);
#endif
#undef msg
return ret;
}
bool argusage(bool bforce ARG_OPTIONAL(false))
{
#ifdef _WINDOWS
TCHAR msg[1024] = TEXT("");
int msgoffset = 0;
#define msg(...) msgoffset += _stprintf_s(msg + msgoffset, ARRAYSIZE(msg) - msgoffset, __VA_ARGS__)
#else
#define msg(...) _ftprintf(stdout, __VA_ARGS__)
#endif
if (!bforce && !g_argshowUsage)
return false;
if (g_argdescription != NULL)
msg(_T(ARG_COMMAND("%s") " - %s\n\n"), argapp(), g_argdescription);
else if (g_argargdescriptioncount == 0)
msg(_T(ARG_COMMAND("%s") "\n\n"), argapp());
if (g_argargdescriptioncount > 0)
{
msg(_T(ARG_COMMAND("%s")), argapp());
int i;
for (i = 0; i < g_argargdescriptioncount; ++i)
{
const ArgArgDescription* argargdescription = &g_argargdescription[i];
if (argargdescription->value != NULL)
msg(_T(" [" ARG_OPTION("%s") "=" ARG_VALUE("%s") "]"), argargdescription->arg, argargdescription->value);
else
msg(_T(" [" ARG_OPTION("%s") "]"), argargdescription->arg);
}
for (i = 0; i < g_argargnumdescriptioncount; ++i)
{
const ArgArgNumDescription* argargnumdescription = &g_argargnumdescription[i];
if (i >= g_argargnumoptional)
msg(_T(" [" ARG_OPTION("%s") "]"), argargnumdescription->arg);
else
msg(_T(" <" ARG_OPTION("%s")) ">", argargnumdescription->arg);
}
msg(_T("\n"));
for (i = 0; i < g_argargdescriptioncount; ++i)
{
const ArgArgDescription* argargdescription = &g_argargdescription[i];
if (argargdescription->desc != NULL)
msg(_T(" " ARG_OPTION("%s") "\t%s\n"), argargdescription->arg, argargdescription->desc);
}
for (i = 0; i < g_argargnumdescriptioncount; ++i)
{
const ArgArgNumDescription* argargnumdescription = &g_argargnumdescription[i];
if (argargnumdescription->desc != NULL)
msg(_T(" " ARG_OPTION("%s") "\t%s\n"), argargnumdescription->arg, argargnumdescription->desc);
}
}
#ifdef _WINDOWS
MessageBox(NULL, msg, argapp(), MB_OK | MB_ICONINFORMATION);
#endif
#undef msg
return true;
}
bool argswitch(const TCHAR* argf, const TCHAR* desc)
{
ArgArgDescription* argargdescription = &g_argargdescription[g_argargdescriptioncount++];
argargdescription->arg = argf;
argargdescription->value = NULL;
argargdescription->desc = desc;
for (int argi = 1; argi < g_argc; ++argi)
{
const TCHAR* arg = g_argv[argi];
if (_tcsicmp(arg, argf) == 0)
{
g_argb[argi] = true;
return true;
}
}
return false;
}
const TCHAR* argvalue(const TCHAR* argf, const TCHAR* def, const TCHAR* descvalue, const TCHAR* desc)
{
ArgArgDescription* argargdescription = &g_argargdescription[g_argargdescriptioncount++];
argargdescription->arg = argf;
argargdescription->value = descvalue;
argargdescription->desc = desc;
const size_t len = _tcslen(argf);
for (int argi = 1; argi < g_argc; ++argi)
{
const TCHAR* arg = g_argv[argi];
if (_tcsnicmp(arg, argf, len) == 0 && arg[len] == _T('='))
{
g_argb[argi] = true;
return arg + len + 1;
}
}
return def;
}
int argvalueint(const TCHAR* argf, int def, const TCHAR* descvalue, const TCHAR* desc)
{
const TCHAR* ret = argvalue(argf, NULL, descvalue, desc);
return ret ? _ttoi(ret) : def;
}
const TCHAR* argnum(int i, const TCHAR* def, const TCHAR* descvalue, const TCHAR* desc)
{
ArgArgNumDescription* argargnumdescription = &g_argargnumdescription[g_argargnumdescriptioncount++];
argargnumdescription->arg = descvalue;
argargnumdescription->desc = desc;
argargnumdescription->used = false;
for (int argi = 1; argi < g_argc && i >= 0; ++argi)
{
const TCHAR* arg = g_argv[argi];
if (arg[0] != '/')
{
--i;
if (i == 0)
{
g_argb[argi] = true;
argargnumdescription->used = true;
return arg;
}
}
}
// if g_argargnumoptional > g_argargnumdescriptioncount then def is useless
if (i < 0)
#ifdef _WINDOWS
MessageBox(NULL, _T("Error argument number\n"), argapp(), MB_OK | MB_ICONERROR);
#else
_ftprintf(stderr, _T(ARG_ERROR("Error argument number") "\n"));
#endif
return def;
}
void argoptional()
{
g_argargnumoptional = g_argargnumdescriptioncount;
}
const TCHAR* argnext(const TCHAR* def, const TCHAR* descvalue, const TCHAR* desc)
{
if (def != NULL && g_argargnumoptional == 65536)
argoptional();
ArgArgNumDescription* argargnumdescription = &g_argargnumdescription[g_argargnumdescriptioncount++];
argargnumdescription->arg = descvalue;
argargnumdescription->desc = desc;
argargnumdescription->used = false;
for (int argi = 1; argi < g_argc; ++argi)
{
const TCHAR* arg = g_argv[argi];
if (arg[0] != '/' && !g_argb[argi])
{
g_argb[argi] = true;
argargnumdescription->used = true;
return arg;
}
}
// if g_argargnumoptional > g_argargnumdescriptioncount then def is useless
return def;
}