Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 0.97 KB | None | 0 0
  1. module ArgParse
  2.  
  3. type ParseDef<'T> = {
  4.    initState : 'T
  5.     switches  : Map<string, ('T -> 'T)>
  6.     values    : Map<string, (string -> 'T -> 'T)>
  7.     other     : (string -> 'T -> 'T)
  8.     error     : (string -> 'T -> 'T)
  9.   } with
  10.  
  11.   member this.Parse(argv:string[]) =
  12.     let isSwitch f = this.switches.ContainsKey f
  13.     let isValue f = this.values.ContainsKey f
  14.     let isEither f = isSwitch f || isValue f
  15.     let rec parse args state =
  16.       match args with
  17.       | flag        :: []   when flag |> isEither -> state |> this.error flag        |> parse []
  18.       | flag        :: tail when flag |> isSwitch -> state |> this.switches.[flag]   |> parse tail
  19.       | flag :: arg :: tail when flag |> isValue  -> state |> this.values.[flag] arg |> parse tail
  20.       |         arg :: tail                       -> state |> this.other arg         |> parse tail  
  21.       |                []                         -> state
  22.  
  23.     this.initState |> parse (List.ofArray argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement