Can be attached as a UDA to override defaults
If your function returns CliResult, you can return a value and some output in one object.
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
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.
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.
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);
Added May 23, 2025
Module for helping to make command line interface programs.