@@ -46,50 +46,65 @@ type ActionInput<'T>(inputType: ActionInputSource) =
4646
4747module Input =
4848
49+ /// Injects an ` ActionContext ` into the action which contains the ` ParseResult ` and a cancellation token.
4950 let context =
5051 ActionInput< ActionContext>( Context)
5152
53+ /// Creates a named option. Example: ` option "--file-name" `
5254 let option < 'T > ( name : string ) =
5355 Option< 'T>( name) |> ActionInput.OfOption
5456
57+ /// Edits the underlying System.CommandLine.Option<'T>.
5558 let editOption ( edit : Option < 'T > -> unit ) ( input : ActionInput < 'T >) =
5659 match input.Source with
5760 | ParsedOption o -> o :?> Option< 'T> |> edit
5861 | _ -> ()
5962 input
6063
64+ /// Edits the underlying System.CommandLine.Argument<'T>.
6165 let editArgument ( edit : Argument < 'T > -> unit ) ( input : ActionInput < 'T >) =
6266 match input.Source with
6367 | ParsedArgument a -> a :?> Argument< 'T> |> edit
6468 | _ -> ()
6569 input
6670
71+ /// Adds one or more aliases to an option.
6772 let aliases ( aliases : string seq ) ( input : ActionInput < 'T >) =
6873 input |> editOption ( fun o -> aliases |> Seq.iter o.Aliases.Add)
6974
75+ /// Adds an alias to an option.
7076 let alias ( alias : string ) ( input : ActionInput < 'T >) =
7177 input |> editOption ( fun o -> o.Aliases.Add alias)
7278
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 >) =
7481 input
7582 |> editOption ( fun o -> o.Description <- description)
7683 |> editArgument ( fun a -> a.Description <- description)
7784
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.
7889 let defaultValue ( defaultValue : 'T ) ( input : ActionInput < 'T >) =
7990 input
8091 |> editOption ( fun o -> o.DefaultValueFactory <- ( fun _ -> defaultValue))
8192 |> editArgument ( fun a -> a.DefaultValueFactory <- ( fun _ -> defaultValue))
8293
94+ /// An alias for ` defaultValue ` to set the default value of an option or argument.
8395 let def = defaultValue
8496
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 >) =
8699 input
87100 |> editOption ( fun o -> o.DefaultValueFactory <- defaultValueFactory)
88101 |> editArgument ( fun a -> a.DefaultValueFactory <- defaultValueFactory)
89102
103+ /// Marks an option as required.
90104 let required ( input : ActionInput < 'T >) =
91105 input |> editOption ( fun o -> o.Required <- true )
92106
107+ /// Creates a named option of type ` Option<'T option> ` that defaults to ` None ` .
93108 let optionMaybe < 'T > ( name : string ) =
94109 let o = Option< 'T option>( name, aliases = [||])
95110 let isBool = typeof< 'T> = typeof< bool>
@@ -104,10 +119,12 @@ module Input =
104119 o.DefaultValueFactory <- ( fun _ -> None)
105120 ActionInput.OfOption< 'T option> o
106121
122+ /// Creates a named argument. Example: ` argument "file-name" `
107123 let argument < 'T > ( name : string ) =
108124 let a = Argument< 'T>( name)
109125 ActionInput.OfArgument< 'T> a
110126
127+ /// Creates a named argument of type ` Argument<'T option> ` that defaults to ` None ` .
111128 let argumentMaybe < 'T > ( name : string ) =
112129 let a = Argument< 'T option>( name)
113130 a.DefaultValueFactory <- ( fun _ -> None)
@@ -119,6 +136,7 @@ module Input =
119136 )
120137 ActionInput.OfArgument< 'T option> a
121138
139+ /// Adds a validator that validates the parsed value of an option or argument.
122140 let validate ( validate : 'T -> Result < unit , string >) ( input : ActionInput < 'T >) =
123141 input
124142 |> editOption ( fun o ->
@@ -138,15 +156,33 @@ module Input =
138156 )
139157 )
140158
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 ` .
141176 let addValidator ( validator : Parsing.SymbolResult -> unit ) ( input : ActionInput < 'T >) =
142177 input
143178 |> editOption ( fun o -> o.Validators.Add( validator))
144179 |> editArgument ( fun a -> a.Validators.Add( validator))
145180
146-
181+ /// Converts an ` Option<'T> ` to an ` ActionInput<'T> ` for usage with the command builders.
147182 let ofOption ( o : Option < 'T >) =
148183 ActionInput.OfOption< 'T> o
149184
185+ /// Converts an ` Argument<'T> ` to an ` ActionInput<'T> ` for usage with the command builders.
150186 let ofArgument ( a : Argument < 'T >) =
151187 ActionInput.OfArgument< 'T> a
152188
0 commit comments