What is the best mechanism to write the following basic plugin system?
Plugins should provide two functions, `usage ()` and `handle :
[string] -> unit`, and the main interpreter will receive args of the
form `plugin-name [arg1; arg2; ...]. The problem with the main handle
function below is that it's twice as long as necessary - I should be
able to capture the pattern "if there are 0 args call usage else call
handle" more succinctly. Ideally, I just want to have a mapping of
plugin name to plugin and have the rest of the code handled
generically.
module A = struct
let usage () = print_endline "usage of A"
let handle args = print_endline "hello from A"
end
module B = struct
let usage () = print_endline "usage of A"
let handle args = print_endline "hello from B"
end
let handle m args = match (m, args) with
"a", [] -> A.usage ()
| "a", xs -> A.handle xs
| "b", [] -> B.usage ()
| "b", xs -> B.handle xs
| _, _ -> print_endline "no such module"
let _ =
handle "a" [];
handle "a" ["1"];
handle "b" [];
handle "b" ["args"]
--
Caml-list mailing list. Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs