Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
writing a very basic plugin system
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Martin DeMello  
View profile  
 More options Jan 16, 7:05 pm
Newsgroups: fa.caml
From: Martin DeMello <martindeme...@gmail.com>
Date: Tue, 17 Jan 2012 00:05:23 UTC
Local: Mon, Jan 16 2012 7:05 pm
Subject: [Caml-list] writing a very basic plugin system
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pat Johnson  
View profile  
 More options Jan 16, 9:28 pm
Newsgroups: fa.caml
From: Pat Johnson <john...@crans.org>
Date: Tue, 17 Jan 2012 02:28:44 UTC
Local: Mon, Jan 16 2012 9:28 pm
Subject: Re: [Caml-list] writing a very basic plugin system
Le 17/01/2012 01:04, Martin DeMello a écrit :

> 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.

This can easily be dealt with 3.12 capabilities :

module type Plugin =
  sig
  val usage : unit -> unit
  val handle : string list -> unit
end;;

(* let's assume A and B are declared accordingly *)

let handle m args =
let module Plug = val
  begin
   match m with
    "a" -> A
    "b" -> B
| _-> raise Not_found
  end : Plugin
in match args with
[] -> Plug.usage ()
| xs -> Plug.handle xs;;

Hope this helps,

--
PJ

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin DeMello  
View profile  
 More options Jan 16, 11:10 pm
Newsgroups: fa.caml
From: Martin DeMello <martindeme...@gmail.com>
Date: Tue, 17 Jan 2012 04:10:44 UTC
Local: Mon, Jan 16 2012 11:10 pm
Subject: Re: [Caml-list] writing a very basic plugin system
Perfect, thanks! Here's the complete working example (with help from
http://caml.inria.fr/pub/docs/manual-ocaml/manual021.html#toc81):

module type Plugin =
  sig
    val usage : unit -> unit
    val handle : string list -> unit
  end;;

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 = []) () =
  let module Plug = (val
  begin
    match m with
      "a" -> (module A: Plugin)
    | "b" -> (module B: Plugin)
    | _-> raise Not_found
  end : Plugin)
  in match args with
    [] -> Plug.usage ()
  | xs -> Plug.handle xs;;

let _ =
  handle "a" ();
  handle "a" ~args:["foo"] ();
  handle "b" ();
  handle "b" ~args:["foo"] ();
  handle "c" (); (* => error *)

martin

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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »