caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Martin DeMello <martindemello@gmail.com>
To: OCaml List <caml-list@yquem.inria.fr>
Subject: [Caml-list] writing a very basic plugin system
Date: Mon, 16 Jan 2012 16:04:26 -0800	[thread overview]
Message-ID: <CAFrFfuFkSLPSn2rjwDhtNQx0SCXPoRgjp6QPhRyVdtVixYMgUQ@mail.gmail.com> (raw)

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"]

             reply	other threads:[~2012-01-17  0:04 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-17  0:04 Martin DeMello [this message]
2012-01-17  2:24 ` Pat Johnson
2012-01-17  4:09   ` Martin DeMello

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAFrFfuFkSLPSn2rjwDhtNQx0SCXPoRgjp6QPhRyVdtVixYMgUQ@mail.gmail.com \
    --to=martindemello@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).