/
Options.fs
90 lines (73 loc) · 2.8 KB
/
Options.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
namespace Fake.Core.CommandLineParsing
open System
open System.Collections.Generic
type OptionSection = { Title: string; Lines: string list }
type SafeOption =
{ Short: char option
Long: string option
ArgumentName: string option
AllowMultiple: bool
IsRequired: bool
DefaultValue: string option }
static member Empty =
{ Short = None
Long = None
ArgumentName = None
AllowMultiple = false
IsRequired = false
DefaultValue = None }
member x.FullShort =
match x.Short with
| None -> ""
| Some s -> String([| '-'; s |])
member x.FullLong =
match x.Long with
| None -> ""
| Some l -> String.Concat("--", l)
member xx.IsEmpty = xx = SafeOption.Empty
member xx.HasArgument = Option.isSome xx.ArgumentName
member xx.HasDefault = Option.isSome xx.DefaultValue
member xx.IsShort = Option.isSome xx.Short
member xx.IsLong = Option.isSome xx.Long
override xx.ToString() =
let inline printCOpt c =
match c with
| Some c -> sprintf "'%c'" c
| None -> "<none>"
let inline printSOpt s =
match s with
| Some s -> sprintf "\"%s\"" s
| None -> "<none>"
sprintf
"Option { Short=%s; Long=%s; ArgName=%s; Default=%s }"
(printCOpt xx.Short)
(printSOpt xx.Long)
(printSOpt xx.ArgumentName)
(printSOpt xx.DefaultValue)
type SafeOptions(list: SafeOption list) =
let findIn (l': string) (list: SafeOption list) =
list |> List.tryFind (fun o' -> o'.Long = Some l')
interface System.Collections.IEnumerable with
member x.GetEnumerator() =
(list :> System.Collections.IEnumerable).GetEnumerator()
interface IEnumerable<SafeOption> with
member x.GetEnumerator() =
(list :> IEnumerable<SafeOption>).GetEnumerator()
member _.Find(s': char) =
list |> List.tryFind (fun o' -> o'.Short = Some s')
member _.AddRange(opts: SafeOption list) = SafeOptions(opts @ list)
member _.FindAndRemove(s': char) =
match list |> List.tryFindIndex (fun o' -> o'.Short = Some s') with
| None -> None
| Some i ->
let ret = list[i]
Some(SafeOptions(list |> List.filter (fun t -> not <| obj.ReferenceEquals(t, ret))), ret)
member _.Find(l': string) = findIn l' list
//match base.Find(fun o' -> o'.Long = l') with
//| null -> base.Find(fun o' -> o'.Long.StartsWith(l'))
//| opt -> opt
member _.FindLast(l': string) = findIn l' (list |> List.rev)
//match base.FindLast(fun o' -> o'.Long = l') with
//| null -> base.FindLast(fun o' -> o'.Long.StartsWith(l'))
//| opt -> opt
member _.Last = list |> List.last