Skip to content

Instantly share code, notes, and snippets.

@rgrinberg
Created September 14, 2013 16:36
Show Gist options
  • Save rgrinberg/6563443 to your computer and use it in GitHub Desktop.
Save rgrinberg/6563443 to your computer and use it in GitHub Desktop.
no need for objects
type 'a option_parser = string list -> 'a list
type 'a handler = 'a list -> unit
type subcommand =
| Subcommand : 'a option_parser * 'a handler -> subcommand
let handle_run options =
let refresh = ref false in
let wrapper = ref None in
ListLabels.iter options ~f:(function
| `Refresh -> refresh := true
| `Wrapper w -> wrapper := Some w
); print_endline "handle_run..."
let handle_download options =
let refresh = ref false in
let show = ref false in
ListLabels.iter options ~f:(function
| `Refresh -> refresh := true
| `Show -> show := true
); print_endline "handle_download..."
let parse_run : [> `Refresh | `Wrapper of int] option_parser =
fun _ -> [`Refresh; `Wrapper 5]
let parse_download : [> `Refresh | `Show] option_parser =
fun _ -> [`Refresh; `Show]
let subcommands = [
("run", Subcommand (parse_run, handle_run));
("download", Subcommand (parse_download, handle_download));
]
let (|>) f x = x f (* in case you're not on 4.01 yet *)
let run_command args (Subcommand (parse, handler)) =
args |> parse |> handler
let () = subcommands
|> List.map snd
|> List.iter (run_command ["test";"me"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment