-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arguments.fs
98 lines (79 loc) · 3.04 KB
/
Arguments.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
namespace KestrelTest
module Arguments =
type ProgramMode = ModeServer | ModeClient
type CommandLineOptions = {
mode: ProgramMode;
port: int;
help: bool;
}
let rec parseCommandLineRec args optionsSoFar =
match args with
| [] ->
optionsSoFar
| "-m"::xs | "--mode"::xs ->
match xs with
| "server"::xss ->
let newOptionsSoFar = { optionsSoFar with mode=ModeServer}
parseCommandLineRec xss newOptionsSoFar
| "client"::xss ->
let newOptionsSoFar = { optionsSoFar with mode=ModeClient}
parseCommandLineRec xss newOptionsSoFar
| _ ->
eprintfn "Mode needs a second argument"
parseCommandLineRec xs optionsSoFar
| "-p"::xs | "--port"::xs ->
match xs with
| p::xss ->
match System.Int32.TryParse(p) with
| (true, port) ->
let newOptionsSoFar = { optionsSoFar with port=port }
parseCommandLineRec xss newOptionsSoFar
| _ ->
eprintfn "Port needs to be an int"
parseCommandLineRec xss optionsSoFar
| _ ->
eprintfn "Port needs a second argument"
parseCommandLineRec xs optionsSoFar
| "-h"::xs | "--help"::xs ->
let newOptionsSoFar = { optionsSoFar with help=true}
parseCommandLineRec xs newOptionsSoFar
| x::xs ->
eprintfn "Option '%s' is unrecognized" x
parseCommandLineRec xs optionsSoFar
let parseCommandLine argv =
let defaultOptions = {
mode = ModeServer;
port = 8090;
help = false;
}
let args = argv |> List.ofSeq
parseCommandLineRec args defaultOptions
//
// When available on .net core, Argu or CommandLineParser should be easier options
//
// commandline style
//type options = {
//[ <Option('m', "mode", Required = true, HelpText = "set program mode: client | server | both")>] Mode : string;
//[ <Option('p', "port", HelpText = "set server port")>] Port : string;
//}
//let result = CommandLine.Parser.Default.ParseArguments<options>(argv)
//match result with
//| :? Parsed<options> as parsed ->
//match parsed with
//|
//| :? NotParsed<options> as notParsed -> fail notParsed.Errors
// Argu style
//open CommandLine
//type ProgramMode =
//| Client = "client"
//| Server = "server"
//| Both = "both"
//type ProgramOptions =
//| [<AltCommandLine("-p")>] Port of tcp_port: int
//| [<Mandatory><AltCommandLine("-m")>] Mode of value:string
//with
//interface IArgParserTemplate with
//member arg.Usage =
//match arg with
//| Port _ -> "set server port"
//| Mode _ -> "set program mode: client | server | both"