You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While the `Input.Argument` and `Input.Option` helper methods are useful for most common scenarios, sometimes it may be necessary to manually define inputs using the underlying `System.CommandLine` base library. This will make it easier to investigate the various overloads and take advantage of other features like custom validation.
392
+
Sometimes it may be necessary to access the underlying `System.CommandLine` base input properties manually to take advantage of features like custom validation. You can do this by using overloads of `Input.Argument`, `Input.Option` and `Input.OptionMaybe` that provide a `configure` argument.
393
+
The `configure` argument is a function that allows you to modify the underlying `System.CommandLine``Option<'T>` or `Argument<'T>`.
393
394
394
395
```F#
395
396
module Program
@@ -403,22 +404,17 @@ let app (name: string) =
403
404
let main argv =
404
405
405
406
let name =
406
-
let opt = System.CommandLine.Option<string>(
407
-
"--name",
408
-
getDefaultValue = (fun () -> ""),
409
-
description = "User name")
410
-
411
-
opt.AddValidator(fun result ->
412
-
let nameValue = result.GetValueForOption(opt)
413
-
if System.String.IsNullOrWhiteSpace(nameValue)
414
-
then result.ErrorMessage <- "Name cannot be an empty string."
415
-
elif nameValue.Length > 10
416
-
then result.ErrorMessage <- "Name cannot exceed more than 10 characters."
407
+
Input.Option<string>("--name", fun o ->
408
+
o.Description <- "User name"
409
+
o.SetDefaultValue "-- NotSet --"
410
+
o.AddValidator(fun result ->
411
+
let nameValue = result.GetValueForOption(opt)
412
+
if System.String.IsNullOrWhiteSpace(nameValue)
413
+
then result.ErrorMessage <- "Name cannot be an empty string."
414
+
elif nameValue.Length > 10
415
+
then result.ErrorMessage <- "Name cannot exceed more than 10 characters."
416
+
)
417
417
)
418
-
419
-
// Transforms Option<string> to be used with the `rootCommand`
420
-
Input.OfOption opt
421
-
422
418
423
419
rootCommand argv {
424
420
description "Provides a friendly greeting."
@@ -427,13 +423,14 @@ let main argv =
427
423
}
428
424
```
429
425
426
+
Alternatively, you can manually create a `System.CommandLine``Option<'T>` or `Argument<'T>` and then convert it for use with a `CommandBuilder` via the `Input.OfOption` or `Input.OfArgument` helper methods.
427
+
430
428
Note that you can also import the `FSharp.SystemCommandLine.Aliases` namespace to use the `Arg<'T>` and `Opt<'T>` aliases:
431
429
432
430
```F#
433
-
open FSharp.SystemCommandLine.Aliases
434
431
435
-
let csOpt =
436
-
Opt<string>(
432
+
let connectionString =
433
+
System.CommandLine.Option<string>(
437
434
getDefaultValue = (fun () -> "conn string"),
438
435
aliases = [| "-cs";"--connectionString" |],
439
436
description = "An optional connection string to the server to import into"
0 commit comments