Repository navigation
Expand file tree
/
Copy pathcli.strata
More file actions
403 lines (379 loc) · 11.1 KB
/
Copy pathcli.strata
File metadata and controls
403 lines (379 loc) · 11.1 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// cli.strata — a small declarative command-line parser for Strata.
//
// Usage (Strata side):
// import cli; // or `import ../cli;` from a subdirectory like samples/
//
// int app_main(const ref string[] args)
// {
// ^CliFlag[] flags = {
// CliFlag { .longName = "verbose", .shortName = "v" },
// CliFlag { .longName = "help", .shortName = "h" }
// };
// ^CliOpt[] opts = {
// CliOpt { .longName = "output", .shortName = "o", .def = "", .hasDef = false },
// CliOpt { .longName = "count", .shortName = "n", .def = "1", .hasDef = true }
// };
// ^CliSpec spec = CliSpec { .flags = flags, .opts = opts };
// ^CliResult r = CliParse(spec, args);
// if (r.err != CliErr.None)
// {
// return 2; // r.errArg holds the argv index of the failure
// }
// if (CliFlagSet(r, 0)) { /* --verbose / -v seen */ }
// if (CliOptSet(r, 0)) { string o = copy(r.optVal[0]); /* ... */ }
// CliInt n = CliParseInt(r.optVal[1]);
// // r.pos holds positional args in order (owned copies).
// return 0;
// }
//
// Supported forms (v1, no subcommands):
// --flag long flag --verbose
// --opt value long option --output out.txt
// --opt=value long option, inline --output=out.txt
// -f short flag -v
// -xyz combined short flags -vh
// -o value short option -o out.txt
// -ovalue / -o=val short option, inline -oout.txt / -o=out.txt
// -- stop parsing flags; the rest (and a lone `-`) are positional.
// A repeated flag stays set; a repeated option keeps the LAST value.
// An unknown flag/option sets err to CliErr.UnknownArg; an option with no
// value sets err to CliErr.MissingValue. A flag with `=value` attached
// (--verbose=x) is an error (flags take no values).
// Negative numbers look like short flags: put `--` first if you take them.
//
// Host contract (see cli_shim.h / cli_shim.c):
// - The host builds the `string[]` the parser borrows: one fat
// {ptr, len, cap} per argv entry plus one outer fat for the array.
// Pass it as `const ref string[]` so the parser never frees host memory.
// By convention the host strips argv[0] (program name) first.
// - The host implements `cli_slice` (declared below) and the Strata
// runtime hooks (strata_alloc / strata_free / strata_panic).
// - Later, when Strata gains a non-owning `cstring` type, the borrowed
// `string[]` handoff (and `copy()` calls below) can move to it.
// Slice helper the host provides: `cli_slice(s, start, len)` returns the
// substring s[start .. start+len] as a NEW owned string. Needed because pure
// Strata cannot carve a substring out of an argv entry (for `--opt=value`).
// ABI: `void cli_slice(const char* s, int start, int len, HostArr* out)`
// where HostArr is {data, u32 len, u32 cap}; the host mallocs len+1 bytes.
extern void cli_slice(string s, int start, int len, return string out);
enum CliErr : int
{
None,
UnknownArg,
MissingValue
};
struct CliFlag
{
string longName;
string shortName;
};
struct CliOpt
{
string longName;
string shortName;
string def;
bool hasDef;
};
struct CliSpec
{
^CliFlag[] flags;
^CliOpt[] opts;
};
struct CliResult
{
bool[] flagSet;
bool[] optSet;
string[] optVal;
string[] pos;
CliErr err;
int errArg;
};
struct CliInt
{
int v;
bool ok;
};
bool CliIsLong(const ref string s)
{
if (s.length < 2) { return false; }
if ((int)s[0] != 45) { return false; }
if ((int)s[1] != 45) { return false; }
return true;
}
bool CliIsShort(const ref string s)
{
if (s.length < 2) { return false; }
if ((int)s[0] != 45) { return false; }
return true;
}
int CliFindChar(const ref string s, int ch)
{
for (uint i = 0; i < s.length; i = i + 1)
{
if ((int)s[i] == ch) { return (int)i; }
}
return -1;
}
// True when `arg` is "--" + `name` exactly (`nameLen` counts the chars
// after "--", stopping before any '='). Empty names never match.
bool CliMatchLongName(const ref string arg, const ref string name, int nameLen)
{
if (name.length == 0) { return false; }
if ((int)name.length != nameLen) { return false; }
for (uint i = 0; i < name.length; i = i + 1)
{
if (arg[i + 2] != name[i]) { return false; }
}
return true;
}
int CliFindFlagShort(const ref ^CliSpec spec, int c)
{
for (uint f = 0; f < spec.flags.length; f = f + 1)
{
if (spec.flags[f].shortName.length == 1 && (int)spec.flags[f].shortName[0] == c)
{
return (int)f;
}
}
return -1;
}
int CliFindOptShort(const ref ^CliSpec spec, int c)
{
for (uint o = 0; o < spec.opts.length; o = o + 1)
{
if (spec.opts[o].shortName.length == 1 && (int)spec.opts[o].shortName[0] == c)
{
return (int)o;
}
}
return -1;
}
// Parses one `--name` / `--name=value` token. Returns extra args consumed
// beyond the token itself (0, or 1 when the value is the next arg),
// or -1 on error (r.err / r.errArg are set).
int CliParseLong(const ref ^CliSpec spec, ref ^CliResult r, const ref string[] args, int idx)
{
int epos = CliFindChar(args[(uint)idx], 61);
int nameLen;
bool hasVal;
if (epos < 0)
{
nameLen = (int)args[(uint)idx].length - 2;
hasVal = false;
}
else
{
nameLen = epos - 2;
hasVal = true;
}
for (uint o = 0; o < spec.opts.length; o = o + 1)
{
if (CliMatchLongName(args[(uint)idx], spec.opts[o].longName, nameLen))
{
if (hasVal)
{
int st = epos + 1;
int ln = (int)args[(uint)idx].length - st;
string v = cli_slice(args[(uint)idx], st, ln);
r.optVal[o] = v;
r.optSet[o] = true;
return 0;
}
if (idx + 1 >= (int)args.length)
{
r.err = CliErr.MissingValue;
r.errArg = idx;
return -1;
}
r.optVal[o] = copy(args[(uint)(idx + 1)]);
r.optSet[o] = true;
return 1;
}
}
for (uint f = 0; f < spec.flags.length; f = f + 1)
{
if (CliMatchLongName(args[(uint)idx], spec.flags[f].longName, nameLen))
{
if (hasVal)
{
r.err = CliErr.UnknownArg;
r.errArg = idx;
return -1;
}
r.flagSet[f] = true;
return 0;
}
}
r.err = CliErr.UnknownArg;
r.errArg = idx;
return -1;
}
// Parses one `-x` / `-xyz` / `-o value` / `-ovalue` / `-o=value` token.
// Same return contract as CliParseLong.
int CliParseShort(const ref ^CliSpec spec, ref ^CliResult r, const ref string[] args, int idx)
{
uint n = args[(uint)idx].length;
uint p = 1;
while (p < n)
{
int c = (int)args[(uint)idx][p];
int fi = CliFindFlagShort(spec, c);
int oi = CliFindOptShort(spec, c);
if (fi >= 0)
{
r.flagSet[(uint)fi] = true;
p = p + 1;
}
else if (oi >= 0)
{
uint o = (uint)oi;
if (p + 1 < n)
{
int st = (int)(p + 1);
if ((int)args[(uint)idx][p + 1] == 61)
{
st = st + 1;
}
int ln = (int)n - st;
string v = cli_slice(args[(uint)idx], st, ln);
r.optVal[o] = v;
r.optSet[o] = true;
}
else
{
if (idx + 1 >= (int)args.length)
{
r.err = CliErr.MissingValue;
r.errArg = idx;
return -1;
}
r.optVal[o] = copy(args[(uint)(idx + 1)]);
r.optSet[o] = true;
return 1;
}
return 0;
}
else
{
r.err = CliErr.UnknownArg;
r.errArg = idx;
return -1;
}
}
return 0;
}
// Parses `args` (program name already stripped by the host) per `spec`.
// The spec and args are borrowed; the result owns its strings.
^CliResult CliParse(const ref ^CliSpec spec, const ref string[] args)
{
^CliResult r = CliResult { .err = CliErr.None, .errArg = -1 };
for (uint i = 0; i < spec.flags.length; i = i + 1)
{
array_push(r.flagSet, false);
}
for (uint i = 0; i < spec.opts.length; i = i + 1)
{
array_push(r.optSet, false);
if (spec.opts[i].hasDef)
{
array_push(r.optVal, copy(spec.opts[i].def));
}
else
{
array_push(r.optVal, "");
}
}
uint i = 0;
bool onlyPos = false;
while (i < args.length)
{
if (onlyPos)
{
array_push(r.pos, copy(args[i]));
}
else if (args[i] == "--")
{
onlyPos = true;
}
else if (CliIsLong(args[i]))
{
int skip = CliParseLong(spec, r, args, (int)i);
if (skip < 0)
{
return r;
}
i = i + (uint)skip;
}
else if (CliIsShort(args[i]))
{
int skip = CliParseShort(spec, r, args, (int)i);
if (skip < 0)
{
return r;
}
i = i + (uint)skip;
}
else
{
array_push(r.pos, copy(args[i]));
}
i = i + 1;
}
return r;
}
// Parses an optional sign followed by decimal digits (no whitespace).
// Overflow fails closed: out-of-int32-range values report ok = false.
CliInt CliParseInt(const ref string s)
{
CliInt r = { .v = 0, .ok = false };
if (s.length == 0) { return r; }
uint i = 0;
bool neg = false;
if ((int)s[0] == 45) { neg = true; i = 1; }
else if ((int)s[0] == 43) { i = 1; }
if (i >= s.length) { return r; }
int v = 0;
while (i < s.length)
{
int c = (int)s[i];
if (c < 48 || c > 57)
{
r.v = 0;
r.ok = false;
return r;
}
int d = c - 48;
if (!neg)
{
if (v > 214748364) { return r; }
if (v == 214748364 && d > 7) { return r; }
v = v * 10 + d;
}
else
{
if (v < -214748364) { return r; }
if (v == -214748364 && d > 8) { return r; }
v = v * 10 - d;
}
i = i + 1;
}
r.v = v;
r.ok = true;
return r;
}
// Bounds-checked result readers (OOB reads report false / "").
bool CliFlagSet(const ref ^CliResult r, int i)
{
if (i < 0 || i >= (int)r.flagSet.length) { return false; }
return r.flagSet[(uint)i];
}
bool CliOptSet(const ref ^CliResult r, int i)
{
if (i < 0 || i >= (int)r.optSet.length) { return false; }
return r.optSet[(uint)i];
}
string CliOptCopy(const ref ^CliResult r, int i)
{
if (i < 0 || i >= (int)r.optVal.length) { return ""; }
return copy(r.optVal[(uint)i]);
}