Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module ArgParse
- type ParseDef<'T> = {
- initState : 'T
- switches : Map<string, ('T -> 'T)>
- values : Map<string, (string -> 'T -> 'T)>
- other : (string -> 'T -> 'T)
- error : (string -> 'T -> 'T)
- } with
- member this.Parse(argv:string[]) =
- let isSwitch f = this.switches.ContainsKey f
- let isValue f = this.values.ContainsKey f
- let isEither f = isSwitch f || isValue f
- let rec parse args state =
- match args with
- | flag :: [] when flag |> isEither -> state |> this.error flag |> parse []
- | flag :: tail when flag |> isSwitch -> state |> this.switches.[flag] |> parse tail
- | flag :: arg :: tail when flag |> isValue -> state |> this.values.[flag] arg |> parse tail
- | arg :: tail -> state |> this.other arg |> parse tail
- | [] -> state
- this.initState |> parse (List.ofArray argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement