@@ -46,50 +46,65 @@ type ActionInput<'T>(inputType: ActionInputSource) =
46
46
47
47
module Input =
48
48
49
+ /// Injects an `ActionContext` into the action which contains the `ParseResult` and a cancellation token.
49
50
let context =
50
51
ActionInput< ActionContext>( Context)
51
52
53
+ /// Creates a named option. Example: `option "--file-name"`
52
54
let option < 'T > ( name : string ) =
53
55
Option< 'T>( name) |> ActionInput.OfOption
54
56
57
+ /// Edits the underlying System.CommandLine.Option<'T>.
55
58
let editOption ( edit : Option < 'T > -> unit ) ( input : ActionInput < 'T >) =
56
59
match input.Source with
57
60
| ParsedOption o -> o :?> Option< 'T> |> edit
58
61
| _ -> ()
59
62
input
60
63
64
+ /// Edits the underlying System.CommandLine.Argument<'T>.
61
65
let editArgument ( edit : Argument < 'T > -> unit ) ( input : ActionInput < 'T >) =
62
66
match input.Source with
63
67
| ParsedArgument a -> a :?> Argument< 'T> |> edit
64
68
| _ -> ()
65
69
input
66
70
71
+ /// Adds one or more aliases to an option.
67
72
let aliases ( aliases : string seq ) ( input : ActionInput < 'T >) =
68
73
input |> editOption ( fun o -> aliases |> Seq.iter o.Aliases.Add)
69
74
75
+ /// Adds an alias to an option.
70
76
let alias ( alias : string ) ( input : ActionInput < 'T >) =
71
77
input |> editOption ( fun o -> o.Aliases.Add alias)
72
78
73
- let desc ( description : string ) ( input : ActionInput < 'T >) =
79
+ /// Sets the description of an option or argument.
80
+ let description ( description : string ) ( input : ActionInput < 'T >) =
74
81
input
75
82
|> editOption ( fun o -> o.Description <- description)
76
83
|> editArgument ( fun a -> a.Description <- description)
77
84
85
+ /// An alias for `description` to set the description of the input.
86
+ let desc = description
87
+
88
+ /// Sets the default value of an option or argument.
78
89
let defaultValue ( defaultValue : 'T ) ( input : ActionInput < 'T >) =
79
90
input
80
91
|> editOption ( fun o -> o.DefaultValueFactory <- ( fun _ -> defaultValue))
81
92
|> editArgument ( fun a -> a.DefaultValueFactory <- ( fun _ -> defaultValue))
82
93
94
+ /// An alias for `defaultValue` to set the default value of an option or argument.
83
95
let def = defaultValue
84
96
85
- let defFactory ( defaultValueFactory : Parsing.ArgumentResult -> 'T ) ( input : ActionInput < 'T >) =
97
+ /// Sets the default value factory of an option or argument.
98
+ let defaultValueFactory ( defaultValueFactory : Parsing.ArgumentResult -> 'T ) ( input : ActionInput < 'T >) =
86
99
input
87
100
|> editOption ( fun o -> o.DefaultValueFactory <- defaultValueFactory)
88
101
|> editArgument ( fun a -> a.DefaultValueFactory <- defaultValueFactory)
89
102
103
+ /// Marks an option as required.
90
104
let required ( input : ActionInput < 'T >) =
91
105
input |> editOption ( fun o -> o.Required <- true )
92
106
107
+ /// Creates a named option of type `Option<'T option>` that defaults to `None`.
93
108
let optionMaybe < 'T > ( name : string ) =
94
109
let o = Option< 'T option>( name, aliases = [||])
95
110
let isBool = typeof< 'T> = typeof< bool>
@@ -104,10 +119,12 @@ module Input =
104
119
o.DefaultValueFactory <- ( fun _ -> None)
105
120
ActionInput.OfOption< 'T option> o
106
121
122
+ /// Creates a named argument. Example: `argument "file-name"`
107
123
let argument < 'T > ( name : string ) =
108
124
let a = Argument< 'T>( name)
109
125
ActionInput.OfArgument< 'T> a
110
126
127
+ /// Creates a named argument of type `Argument<'T option>` that defaults to `None`.
111
128
let argumentMaybe < 'T > ( name : string ) =
112
129
let a = Argument< 'T option>( name)
113
130
a.DefaultValueFactory <- ( fun _ -> None)
@@ -119,6 +136,7 @@ module Input =
119
136
)
120
137
ActionInput.OfArgument< 'T option> a
121
138
139
+ /// Adds a validator that validates the parsed value of an option or argument.
122
140
let validate ( validate : 'T -> Result < unit , string >) ( input : ActionInput < 'T >) =
123
141
input
124
142
|> editOption ( fun o ->
@@ -138,15 +156,33 @@ module Input =
138
156
)
139
157
)
140
158
159
+ /// Validates that the file exists.
160
+ let validateFileExists ( input : ActionInput < System.IO.FileInfo >) =
161
+ input
162
+ |> validate ( fun file ->
163
+ if file.Exists then Ok ()
164
+ else Error $" File '{file.FullName}' does not exist."
165
+ )
166
+
167
+ /// Validates that the directory exists.
168
+ let validateDirectoryExists ( input : ActionInput < System.IO.DirectoryInfo >) =
169
+ input
170
+ |> validate ( fun dir ->
171
+ if dir.Exists then Ok ()
172
+ else Error $" Directory '{dir.FullName}' does not exist."
173
+ )
174
+
175
+ /// Adds a validator for the given `Parsing.SymbolResult`.
141
176
let addValidator ( validator : Parsing.SymbolResult -> unit ) ( input : ActionInput < 'T >) =
142
177
input
143
178
|> editOption ( fun o -> o.Validators.Add( validator))
144
179
|> editArgument ( fun a -> a.Validators.Add( validator))
145
180
146
-
181
+ /// Converts an `Option<'T>` to an `ActionInput<'T>` for usage with the command builders.
147
182
let ofOption ( o : Option < 'T >) =
148
183
ActionInput.OfOption< 'T> o
149
184
185
+ /// Converts an `Argument<'T>` to an `ActionInput<'T>` for usage with the command builders.
150
186
let ofArgument ( a : Argument < 'T >) =
151
187
ActionInput.OfArgument< 'T> a
152
188
0 commit comments