Created
September 14, 2013 16:36
-
-
Save rgrinberg/6563443 to your computer and use it in GitHub Desktop.
no need for objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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