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
Copy file name to clipboardExpand all lines: README.md
+54Lines changed: 54 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -337,3 +337,57 @@ let main argv =
337
337
addCommand helloCmd
338
338
}
339
339
```
340
+
341
+
## Defining Inputs Manually
342
+
343
+
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.
344
+
345
+
```F#
346
+
module Program
347
+
348
+
open FSharp.SystemCommandLine
349
+
350
+
let app (name: string) =
351
+
printfn $"Hello, {name}"
352
+
353
+
[<EntryPoint>]
354
+
let main argv =
355
+
356
+
let name =
357
+
let opt = System.CommandLine.Option<string>(
358
+
"--name",
359
+
getDefaultValue = (fun () -> ""),
360
+
description = "User name")
361
+
362
+
opt.AddValidator(fun result ->
363
+
let nameValue = result.GetValueForOption(opt)
364
+
if System.String.IsNullOrWhiteSpace(nameValue)
365
+
then result.ErrorMessage <- "Name cannot be an empty string."
366
+
elif nameValue.Length > 10
367
+
then result.ErrorMessage <- "Name cannot exceed more than 10 characters."
368
+
)
369
+
370
+
// Transforms Option<string> to be used with the `rootCommand`
371
+
HandlerInput.OfOption opt
372
+
373
+
374
+
rootCommand argv {
375
+
description "Provides a friendly greeting."
376
+
inputs name
377
+
setHandler app
378
+
}
379
+
```
380
+
381
+
Note that you can also import the `FSharp.SystemCommandLine.Aliases` namespace to use the `Arg<'T>` and `Opt<'T>` aliases:
382
+
383
+
```F#
384
+
open FSharp.SystemCommandLine.Aliases
385
+
386
+
let csOpt =
387
+
Opt<string>(
388
+
getDefaultValue = (fun () -> "conn string"),
389
+
aliases = [| "-cs";"--connectionString" |],
390
+
description = "An optional connection string to the server to import into"
0 commit comments