forked from adamdruppe/arsd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.d
More file actions
582 lines (485 loc) · 14.3 KB
/
cli.d
File metadata and controls
582 lines (485 loc) · 14.3 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/++
Module for helping to make command line interface programs.
You make an object with methods. Those methods take arguments and it reads them automatically for you. Or, you just make one function.
./yourprogram args...
or
./yourprogram class_method_name args....
Args go to:
bool: --name or --name=true|false
string/int/float/enum: --name=arg or --name arg
int[]: --name=arg,arg,arg or --name=arg --name=arg that you can repeat
string[] : remainder; the name is ignored, these are any args not already consumed by args
FilePath and FilePath[]: not yet supported
`--` always stops populating names and puts the remaining in the final string[] args param (if there is one)
`--help` always
Return values:
int is the return value to the cli
string is output, returns 0
other types are converted to string except for CliResult, which lets you specify output, error, and code in one struct.
Exceptions:
are printed with fairly minimal info to the stderr, cause program to return 1 unless it has a code attached
History:
Added May 23, 2025
+/
module arsd.cli;
// stdin:
/++
You can pass a function to [runCli] and it will parse command line arguments
into its arguments, then turn its return value (if present) into a cli return.
+/
unittest {
static // exclude from docs
void func(int a, string[] otherArgs) {
// because we run the test below with args "--a 5"
assert(a == 5);
assert(otherArgs.length == 0);
}
int main(string[] args) {
// make your main function forward to runCli!your_handler
return runCli!func(args);
}
assert(main(["unittest", "--a", "5"]) == 0);
}
/++
You can also pass a class to [runCli], and its public methods will be made
available as subcommands.
+/
unittest {
static // exclude from docs
class Thing {
void func(int a, string[] args) {
assert(a == 5);
assert(args.length == 0);
}
// int return values are forwarded to `runCli`'s return value
int other(bool flag) {
return flag ? 1 : 0;
}
}
int main(string[] args) {
// make your main function forward to runCli!your_handler
return runCli!Thing(args);
}
assert(main(["unittest", "func", "--a", "5"]) == 0);
assert(main(["unittest", "other"]) == 0);
assert(main(["unittest", "other", "--flag"]) == 1);
}
import arsd.core;
/++
+/
int runCli(alias handler)(string[] args) {
CliHandler thing;
static if(is(handler == class)) {
CliHandler[] allOptions;
scope auto instance = new handler();
foreach(memberName; __traits(derivedMembers, handler)) {
static if(memberName != "__ctor" && memberName != "__dtor") {
alias member = __traits(getMember, handler, memberName);
static if(__traits(getProtection, member) == "public") {
static if(is(typeof(member) == return)) {
auto ourthing = createCliHandler!member();
if(args.length > 1 && ourthing.uda.name == args[1]) {
thing = ourthing;
break;
}
allOptions ~= ourthing;
}
}
}
}
if(args.length && args[1] == "--help") {
foreach(option; allOptions)
writeln(option.printHelp());
return 0;
}
if(args.length)
args = args[1 .. $]; // cut off the original args(0) as irrelevant now, the command is the new args[0]
} else {
auto instance = null;
thing = createCliHandler!handler();
}
if(!thing.uda.unprocessed && args.length > 1 && args[1] == "--help") {
writeln(thing.printHelp());
return 0;
}
if(thing.handler is null) {
throw new CliArgumentException("subcommand", "no handler found");
}
auto ret = thing.handler(thing, instance, args);
if(ret.output.length)
writeln(ret.output);
if(ret.error.length)
writelnStderr(ret.error);
return ret.returnValue;
}
/++
+/
class CliArgumentException : object.Exception {
this(string argument, string message) {
super(argument ~ ": " ~ message);
}
}
/++
If your function returns `CliResult`, you can return a value and some output in one object.
Note that output and error are written to stdout and stderr, in addition to whatever the function
did inside. It does NOT represent captured stuff, it is just a function return value.
+/
struct CliResult {
int returnValue;
string output;
string error;
}
/++
Can be attached as a UDA to override defaults
+/
struct Cli {
string name;
string summary;
string help;
// only valid on function - passes the original args without processing them at all, not even --help
bool unprocessed; // FIXME mostly not implemented
// only valid on function - instead of erroring on unknown arg, just pass them unmodified to the catch-all array
bool passthroughUnrecognizedArguments; // FIXME not implemented
// only valid on arguments
dchar shortName; // bool things can be combined and if it is int it can take one like -O2. maybe.
int required = 2;
int arg0 = 2;
int consumesRemainder = 2;
int holdsAllArgs = 2; // FIXME: not implemented
string[] options; // FIXME if it is not one of the options and there are options, should it error?
}
version(sample)
void handler(bool sweetness, @Cli(arg0: true) string programName, float f, @Cli(required: true) int a, @Cli(name: "opend-to-build") string[] magic, int[] foo, string[] remainder) {
import arsd.core;
if(a == 4)
throw ArsdException!"lol"(4, 6);
mixin(dumpParams);
debug dump(__traits(parameters));
debug dump(i"$programName");
static struct Test {
int a;
string b;
float c;
}
debug dump(Test(a: 5, b: "omg", c: 7.5));
}
version(sample)
int main(string[] args) {
/+
import arsd.core;
auto e = extractCliArgs(args, false, ["a":true]);
foreach(a; e)
writeln(a.name, a.values);
return 0;
+/
return runCli!handler(args);
}
private enum SupportedCliTypes {
String,
Int,
Float,
Bool,
IntArray,
StringArray
}
private struct CliArg {
Cli uda;
string argumentName;
string ddoc;
SupportedCliTypes type;
//string default;
}
private struct CliHandler {
CliResult function(CliHandler info, Object _this, string[] args) handler;
Cli uda;
CliArg[] args;
string methodName;
string ddoc;
string printHelp() {
string help = uda.name;
if(help.length)
help ~= ": ";
help ~= uda.help;
foreach(arg; args) {
if(!arg.uda.required)
help ~= "[";
if(arg.uda.consumesRemainder)
help ~= "args...";
else if(arg.type == SupportedCliTypes.Bool)
help ~= "--" ~ arg.uda.name;
else
help ~= "--" ~ arg.uda.name ~ "=" ~ enumNameForValue(arg.type);
if(!arg.uda.required)
help ~= "]";
help ~= " ";
}
// FIXME: print the help details for the args
return help;
}
}
private template CliTypeForD(T) {
static if(is(T == enum))
enum CliTypeForD = SupportedCliTypes.String;
else static if(is(T == string))
enum CliTypeForD = SupportedCliTypes.String;
else static if(is(T == bool))
enum CliTypeForD = SupportedCliTypes.Bool;
else static if(is(T : long))
enum CliTypeForD = SupportedCliTypes.Int;
else static if(is(T : double))
enum CliTypeForD = SupportedCliTypes.Float;
else static if(is(T : int[]))
enum CliTypeForD = SupportedCliTypes.IntArray;
else static if(is(T : string[]))
enum CliTypeForD = SupportedCliTypes.StringArray;
else
static assert(0, "Unsupported type for CLI: " ~ T.stringof);
}
private CliHandler createCliHandler(alias handler)() {
CliHandler ret;
ret.methodName = __traits(identifier, handler);
version(D_OpenD)
ret.ddoc = __traits(docComment, handler);
foreach(uda; __traits(getAttributes, handler))
static if(is(typeof(uda) == Cli))
ret.uda = uda;
if(ret.uda.name is null)
ret.uda.name = ret.methodName;
if(ret.uda.help is null)
ret.uda.help = ret.ddoc;
if(ret.uda.summary is null)
ret.uda.summary = ret.uda.help; // FIXME: abbreviate
static if(is(typeof(handler) Params == __parameters))
foreach(idx, param; Params) {
CliArg arg;
arg.argumentName = __traits(identifier, Params[idx .. idx + 1]);
// version(D_OpenD) arg.ddoc = __traits(docComment, Params[idx .. idx + 1]);
arg.type = CliTypeForD!param;
foreach(uda; __traits(getAttributes, Params[idx .. idx + 1]))
static if(is(typeof(uda) == Cli)) {
arg.uda = uda;
// import std.stdio; writeln(cast(int) uda.arg0);
}
// if not specified by user, replace with actual defaults
if(arg.uda.consumesRemainder == 2) {
if(idx + 1 == Params.length && is(param == string[]))
arg.uda.consumesRemainder = true;
else
arg.uda.consumesRemainder = false;
} else {
assert(0, "do not set consumesRemainder explicitly at least not at this time");
}
if(arg.uda.arg0 == 2)
arg.uda.arg0 = false;
if(arg.uda.required == 2)
arg.uda.required = false;
if(arg.uda.holdsAllArgs == 2)
arg.uda.holdsAllArgs = false;
static if(is(param == enum))
if(arg.uda.options is null)
arg.uda.options = [__traits(allMembers, param)];
if(arg.uda.name is null)
arg.uda.name = arg.argumentName;
ret.args ~= arg;
}
ret.handler = &cliForwarder!handler;
return ret;
}
private struct ExtractedCliArgs {
string name;
string[] values;
}
private ExtractedCliArgs[] extractCliArgs(string[] args, bool needsCommandName, bool[string] namesThatTakeSeparateArguments) {
// FIXME: if needsCommandName, args[1] should be that
ExtractedCliArgs[] ret;
if(args.length == 0)
return [ExtractedCliArgs(), ExtractedCliArgs()];
ExtractedCliArgs remainder;
ret ~= ExtractedCliArgs(null, [args[0]]); // arg0 is a bit special, always the first one
args = args[1 .. $];
ref ExtractedCliArgs byName(string name) {
// FIXME: could actually do a map to index thing if i had to
foreach(ref r; ret)
if(r.name == name)
return r;
ret ~= ExtractedCliArgs(name);
return ret[$-1];
}
string nextArgName = null;
void appendPossibleEmptyArg() {
if(nextArgName is null)
return;
byName(nextArgName).values ~= null;
nextArgName = null;
}
foreach(idx, arg; args) {
if(arg == "--") {
remainder.values ~= args[idx + 1 .. $];
break;
}
if(arg[0] == '-') {
// short name or short nameINT_VALUE
// -longname or -longname=VALUE. if -longname, next arg is its value unless next arg starts with -.
if(arg.length == 1) {
// plain - often represents stdin or whatever, treat it as a normal filename arg
remainder.values ~= arg;
} else {
appendPossibleEmptyArg();
string value;
if(arg[1] == '-') {
// long name...
import arsd.string;
auto equal = arg.indexOf("=");
if(equal != -1) {
nextArgName = arg[2 .. equal];
value = arg[equal + 1 .. $];
} else {
nextArgName = arg[2 .. $];
}
} else {
// short name
nextArgName = arg[1 .. $]; // FIXME what if there's bundled? or an arg?
}
byName(nextArgName);
if(value !is null) {
byName(nextArgName).values ~= value;
nextArgName = null;
} else if(!namesThatTakeSeparateArguments.get(nextArgName, false)) {
byName(nextArgName).values ~= null; // just so you can see how many times it appeared
nextArgName = null;
}
}
} else {
if(nextArgName !is null) {
byName(nextArgName).values ~= arg;
nextArgName = null;
} else {
remainder.values ~= arg;
}
}
}
appendPossibleEmptyArg();
ret ~= remainder; // remainder also a bit special, always the last one
return ret;
}
// FIXME: extractPrefix for stuff like --opend-to-build and --DRT- stuff
private T extractCliArgsT(T)(CliArg info, ExtractedCliArgs[] args) {
try {
import arsd.conv;
if(info.uda.arg0) {
static if(is(T == string)) {
return args[0].values[0];
} else {
assert(0, "arg0 consumers must be type string");
}
}
if(info.uda.consumesRemainder)
static if(is(T == string[])) {
return args[$-1].values;
} else {
assert(0, "remainder consumers must be type string[]");
}
foreach(arg; args)
if(arg.name == info.uda.name) {
static if(is(T == string[]))
return arg.values;
else static if(is(T == int[])) {
int[] ret;
ret.length = arg.values.length;
foreach(i, a; arg.values)
ret[i] = to!int(a);
return ret;
} else static if(is(T == bool)) {
// if the argument is present, that means it is set unless the value false was explicitly given
if(arg.values.length)
return arg.values[$-1] != "false";
return true;
} else {
if(arg.values.length == 1)
return to!T(arg.values[$-1]);
else
throw ArsdException!"wrong number of args"(arg.values.length);
}
}
return T.init;
} catch(Exception e) {
throw new CliArgumentException(info.uda.name, e.toString);
}
}
private CliResult cliForwarder(alias handler)(CliHandler info, Object this_, string[] args) {
try {
static if(is(typeof(handler) Params == __parameters))
Params params;
assert(Params.length == info.args.length);
bool[string] map;
foreach(a; info.args)
if(a.type != SupportedCliTypes.Bool)
map[a.uda.name] = true;
auto eargs = extractCliArgs(args, false, map);
/+
import arsd.core;
foreach(a; eargs)
writeln(a.name, a.values);
+/
foreach(a; eargs[1 .. $-1]) {
bool found;
foreach(a2; info.args)
if(a.name == a2.uda.name) {
found = true;
break;
}
if(!found)
throw new CliArgumentException(a.name, "Invalid arg");
}
// FIXME: look for missing required argument
foreach(a; info.args) {
if(a.uda.required) {
bool found = false;
foreach(a2; eargs[1 .. $-1]) {
if(a2.name == a.uda.name) {
found = true;
break;
}
}
if(!found)
throw new CliArgumentException(a.uda.name, "Missing required arg");
}
}
foreach(idx, ref param; params) {
param = extractCliArgsT!(typeof(param))(info.args[idx], eargs);
}
auto callit() {
static if(is(__traits(parent, handler) Parent == class)) {
auto instance = cast(Parent) this_;
assert(instance !is null);
return __traits(child, instance, handler)(params);
} else {
return handler(params);
}
}
static if(is(typeof(handler) Return == return)) {
static if(is(Return == void)) {
callit();
return CliResult(0);
} else static if(is(Return == int)) {
return CliResult(callit());
} else static if(is(Return == string)) {
return CliResult(0, callit());
} else static assert(0, "Invalid return type on handler: " ~ Return.stringof);
} else static assert(0, "bad handler");
} catch(CliArgumentException e) {
auto str = e.msg;
auto idx = str.indexOf("------");
if(idx != -1)
str = str[0 .. idx];
str = str.stripInternal();
return CliResult(1, null, str);
} catch(Throwable t) {
auto str = t.toString;
auto idx = str.indexOf("------");
if(idx != -1)
str = str[0 .. idx];
str = str.stripInternal();
return CliResult(1, null, str);
}
}