Skip to content

Commit a3fba01

Browse files
authored
Added help for manually creating S.CL inputs
1 parent 201e550 commit a3fba01

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,57 @@ let main argv =
337337
addCommand helloCmd
338338
}
339339
```
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"
391+
)
392+
|> HandlerInput.OfOption
393+
```

0 commit comments

Comments
 (0)