What I believe you are looking for is generally called in the literature as the Command Pattern. In heavily OO languages this pattern typically involves creating bunch of classes that implement a common, simple Command interface which has a single execute()
method.
In D however, you have delegates and can probably avoid generating potentially hundred small classes for this purpose.
Here is one of the possible D alternatives, using lambda expressions ( http://dlang.org/expression.html#Lambda ):
module command2;
import std.stdio;
import std.conv;
import std.array;
// 2 = binary operation
alias int delegate(int arg1, int arg2) Command2;
// Global AA to hold all commands
Command2[string] commands;
// WARNING: assumes perfect string as input!!
void execute(string arg) {
auto pieces = split(arg);
int first = to!int(pieces[1]);
int second = to!int(pieces[2]);
Command2 cmd = commands[pieces[0]];
int result = cmd(first, second); // notice we do not need a big switch here
writeln(arg, " --> ", result);
} // execute() function
void main(string[] args) {
commands["add"] = (int a, int b) => a + b;
commands["sub"] = (int a, int b) => a - b;
commands["sqrt"] = (int a, int b) => a * a; // second parameter ignored
// ... add more commands (or better call them operations) here...
execute("add 2 2");
execute("sqrt 4 0"); // had to have 0 here because execute assumes perfect imput
} // main() function
Here is the source code to fork and play with: http://dpaste.dzfl.pl/41d72036
I will write the OO version when I find more time...
Regarding the execution of a script/application in some directory... It is merely a matter of writing a function which takes parameters, and calls the std.process.execute()
. A very quick example how to extend the code above:
// WARNING: no error checking, etc!
int factoriel(int arg, int ignored) {
auto p = std.process.execute(["./funcs/factoriel", to!string(arg)]);
return to!int(p.output);
} // factoriel() function
...
// in main()
commands["fact"] = toDelegate(&factoriel);
...
execute("fact 6 0"); // again, we add 0 because we do not know how to do unary operations, yet. :)