Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 0.86 KB | None | 0 0
  1. module VersionOne.CommandLine
  2.  
  3. type OptionBag = {
  4.   storedCredsFile : string
  5.   secretsFile : string
  6.   } with
  7.   static member Default = {
  8.     storedCredsFile = "stored_credentials.json"
  9.     secretsFile = "client_secrets.json"
  10.     }
  11.  
  12. let flagFuncs =
  13.   Map [
  14.     "-h", fun state -> failwith "Would display help here"
  15.     ]
  16.  
  17. let valueFuncs =
  18.   Map [
  19.     "--creds",   fun state v -> {state with storedCredsFile = v}
  20.     "--secrets", fun state v -> {state with secretsFile = v}
  21.     ]
  22.  
  23. let rec parseArgs' state = function
  24.  | flag :: tail when flagFuncs.ContainsKey flag  -> parseArgs' (flagFuncs.[flag] state) tail
  25.   | flag :: v :: tail when valueFuncs.ContainsKey flag -> parseArgs'  (valueFuncs.[flag] state v) tail
  26.  | item :: tail -> failwith "Unexpected argument \"%s\" in command line"
  27.  | [] -> state
  28.  
  29. let parseArgs = parseArgs' OptionBag.Default
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement