Skip to content

Instantly share code, notes, and snippets.

@mat3u
Created February 24, 2015 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mat3u/b3f785998d244df0fff7 to your computer and use it in GitHub Desktop.
Save mat3u/b3f785998d244df0fff7 to your computer and use it in GitHub Desktop.
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