Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
type Verbosity = Silent | Normal | Verbose
type Url = string
type Depth = int
let (|IsDepth|_|) str =
match System.Int32.TryParse(str) with
| (true, int) when int > 0 -> Some int
| _ -> None
let (|IsUrl|_|) url =
match System.Uri.IsWellFormedUriString(url, System.UriKind.Absolute) with
| true -> Some url
| _ -> None
type Configuration = { Verbosity: Verbosity; Depth: Depth; Url: Url }
let parseArguments args =
let rec parseArguments' args config =
match args with
| [] -> config
| "/v"::tail -> match tail with
| "Silent"::tail -> parseArguments' tail {config with Verbosity = Silent}
| "Normal"::tail -> parseArguments' tail {config with Verbosity = Normal}
| "Verbose"::tail -> parseArguments' tail {config with Verbosity = Verbose}
| _ -> failwith "Unknown verbosity level!"
| "/d"::tail -> match tail with
| IsDepth depth::tail -> parseArguments' tail {config with Depth = depth}
| _ -> failwith "Unable to parse depth level! Provide positive integer."
| "/u"::tail -> match tail with
| IsUrl url::tail -> parseArguments' tail { config with Url = url }
| _ -> failwith "Unable to parse URL!"
| _ -> failwith "Unknown parameter!"
let defaultConfig = { Verbosity = Normal;
Depth = 3;
Url = @"https://pl.wikipedia.org/wiki/Specjalna:Losowa_strona"}
parseArguments' (List.ofArray args) defaultConfig
[<EntryPoint>]
let main argv =
let config = parseArguments argv
printfn "%A" config
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment