Skip to content

Commit 0150894

Browse files
authored
Edited "Setting Input Properties Manually"
1 parent 1c16f73 commit 0150894

1 file changed

Lines changed: 17 additions & 20 deletions

File tree

README.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,10 @@ As you can see, there are a lot of options that can be configured here (note tha
387387
![image](https://user-images.githubusercontent.com/1030435/199282781-1800b79c-7638-4242-8ca0-777d7237e20a.png)
388388

389389

390-
## Defining Inputs Manually
390+
## Setting Input Properties Manually
391391

392-
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>`.
393394

394395
```F#
395396
module Program
@@ -403,22 +404,17 @@ let app (name: string) =
403404
let main argv =
404405
405406
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+
)
417417
)
418-
419-
// Transforms Option<string> to be used with the `rootCommand`
420-
Input.OfOption opt
421-
422418
423419
rootCommand argv {
424420
description "Provides a friendly greeting."
@@ -427,13 +423,14 @@ let main argv =
427423
}
428424
```
429425

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+
430428
Note that you can also import the `FSharp.SystemCommandLine.Aliases` namespace to use the `Arg<'T>` and `Opt<'T>` aliases:
431429

432430
```F#
433-
open FSharp.SystemCommandLine.Aliases
434431
435-
let csOpt =
436-
Opt<string>(
432+
let connectionString =
433+
System.CommandLine.Option<string>(
437434
getDefaultValue = (fun () -> "conn string"),
438435
aliases = [| "-cs";"--connectionString" |],
439436
description = "An optional connection string to the server to import into"

0 commit comments

Comments
 (0)