@@ -141,18 +141,30 @@ module Input =
141141 input
142142 |> editOption ( fun o ->
143143 o.Validators.Add( fun res ->
144- let value = res.GetValue< 'T>( o.Name)
145- match validate value with
146- | Ok () -> ()
147- | Error err -> res.AddError( err)
144+ try
145+ let value = res.GetValue< 'T>( o.Name)
146+ match validate value with
147+ | Ok () -> ()
148+ | Error err -> res.AddError( err)
149+ with
150+ | :? InvalidOperationException ->
151+ // res.GetValue<'T> will fire this ex when a customParser adds an error.
152+ // In this case, the validation error will already be displayed.
153+ ()
148154 )
149155 )
150156 |> editArgument ( fun a ->
151157 a.Validators.Add( fun res ->
152- let value = res.GetValue< 'T>( a.Name)
153- match validate value with
154- | Ok () -> ()
155- | Error err -> res.AddError( err)
158+ try
159+ let value = res.GetValue< 'T>( a.Name)
160+ match validate value with
161+ | Ok () -> ()
162+ | Error err -> res.AddError( err)
163+ with
164+ | :? InvalidOperationException ->
165+ // res.GetValue<'T> will fire this ex when a customParser adds an error.
166+ // In this case, the validation error will already be displayed.
167+ ()
156168 )
157169 )
158170
@@ -178,6 +190,24 @@ module Input =
178190 |> editOption ( fun o -> o.Validators.Add( validator))
179191 |> editArgument ( fun a -> a.Validators.Add( validator))
180192
193+ /// Parses the input using a custom parser function.
194+ let customParser ( parser : Parsing.ArgumentResult -> 'T ) ( input : ActionInput < 'T >) =
195+ input
196+ |> editOption ( fun o -> o.CustomParser <- parser)
197+ |> editArgument ( fun a -> a.CustomParser <- parser)
198+
199+ /// Creates a custom parser based on the result of the provided parser function.
200+ let tryParse ( parser : Parsing.ArgumentResult -> Result < 'T , string >) ( input : ActionInput < 'T >) =
201+ input
202+ |> customParser ( fun argResult ->
203+ match parser argResult with
204+ | Ok value ->
205+ value
206+ | Error err ->
207+ argResult.AddError( err)
208+ Unchecked.defaultof< 'T>
209+ )
210+
181211 /// Converts an `Option<'T>` to an `ActionInput<'T>` for usage with the command builders.
182212 let ofOption ( o : Option < 'T >) =
183213 ActionInput.OfOption< 'T> o
0 commit comments