Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module VersionOne.CommandLine
- module ArgParse =
- type FlagFuncs<'T> = Map<string, ('T -> 'T)>
- type ValueFuncs<'T> = Map<string, ('T -> string -> 'T)>
- type BareFunc<'T> = 'T -> string -> 'T
- type OptionsDefinition<'T> = {
- initialState : 'T
- flagFuncs : FlagFuncs<'T>
- valueFuncs : ValueFuncs<'T>
- bareFunc : BareFunc<'T>
- }
- let rec parseArgs' (def:OptionsDefinition<'T>) state = function
- | flag :: tail when def.flagFuncs.ContainsKey flag -> parseArgs' def (def.flagFuncs.[flag] state) tail
- | flag :: arg :: tail when def.valueFuncs.ContainsKey flag -> parseArgs' def (def.valueFuncs.[flag] state arg) tail
- | arg :: tail -> parseArgs' def (def.bareFunc state arg) tail
- | [] -> state
- let fromDefinition def argv = parseArgs' def def.initialState (List.ofArray argv)
- module Main =
- type Options = {
- creds : string
- secrets : string
- outputs : string list
- }
- let parseOpts = ArgParse.fromDefinition {
- initialState =
- { creds = "stored_credentials.json"
- secrets = "client_secrets.json"
- outputs = [] }
- bareFunc = fun opts v -> {opts with outputs = v :: opts.outputs }
- flagFuncs =
- Map [
- "-h", fun opts -> failwith "Would display help here"
- ]
- valueFuncs =
- Map [
- "--creds", fun opts v -> {opts with creds = v}
- "--secrets", fun opts v -> {opts with secrets = v}
- ]
- }
- [<EntryPoint>]
- let main argv =
- let opts = parseOpts argv
- printfn "%A" opts
- 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement