Skip to content

Commit 33379a9

Browse files
committed
2 parents 577530f + 3351197 commit 33379a9

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,43 @@ let main argv =
122122
Recursively deleting c:\_github\FSharp.SystemCommandLine\src\FSharp.SystemCommandLine
123123
```
124124

125+
### Simple App with an Injected Dependency
126+
127+
`System.CommandLine` has a built-in dependency injection system and provides a handlful of built-in types that can be injected into your handler function by default:
128+
129+
* `CancellationToken`
130+
* `InvocationContext`
131+
* `ParseResult`
132+
* `IConsole`
133+
* `HelpBuilder`
134+
* `BindingContext`
135+
136+
You can declare injected dependencies via the `Input.InjectedDependency` method.
137+
138+
```F#
139+
module Program
140+
141+
open FSharp.SystemCommandLine
142+
open System.CommandLine.Invocation
143+
144+
let app (ctx: InvocationContext, words: string array, separator: string option) =
145+
let separator = separator |> Option.defaultValue ", "
146+
System.String.Join(separator, words) |> printfn "Result: %s"
147+
ctx.ExitCode <- 1
148+
149+
[<EntryPoint>]
150+
let main argv =
151+
let ctx = Input.InjectedDependency()
152+
let words = Input.Option(["--word"; "-w"], Array.empty, "A list of words to be appended")
153+
let separator = Input.OptionMaybe(["--separator"; "-s"], "A character that will separate the joined words.")
154+
155+
rootCommand argv {
156+
description "Appends words together"
157+
inputs (ctx, words, separator)
158+
setHandler app
159+
}
160+
```
161+
125162

126163
### Async App with a Partially Applied Dependency
127164

0 commit comments

Comments
 (0)