@@ -153,7 +153,7 @@ let main argv =
153153 Recursively deleting c:\_github\FSharp.SystemCommandLine\src\FSharp.SystemCommandLine
154154```
155155
156- ### Simple App with an Injected Dependency
156+ ### Async App with an Injected CancellationToken
157157
158158` 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:
159159
@@ -170,24 +170,36 @@ You can declare injected dependencies via the `Input.InjectedDependency` method.
170170module Program
171171
172172open FSharp.SystemCommandLine
173- open System.CommandLine.Invocation
173+ open System.Threading
174+ open System.Threading.Tasks
174175
175- let app (ctx: InvocationContext, words: string array, separator: string option) =
176- let separator = separator |> Option.defaultValue ", "
177- System.String.Join(separator, words) |> printfn "Result: %s"
178- ctx.ExitCode <- 1
176+ let app (cancel: CancellationToken, words: string array, separator: string) =
177+ task {
178+ for i in [1..20] do
179+ if cancel.IsCancellationRequested then
180+ printfn "Cancellation Requested"
181+ raise (new System.OperationCanceledException())
182+ else
183+ printfn $"{i}"
184+ do! Task.Delay(1000)
185+
186+ System.String.Join(separator, words)
187+ |> printfn "Result: %s"
188+ }
179189
180190[<EntryPoint>]
181191let main argv =
182- let ctx = Input.InjectedDependency()
183- let words = Input.Option(["--word"; "-w"], Array.empty , "A list of words to be appended")
184- let separator = Input.OptionMaybe (["--separator"; "-s"], "A character that will separate the joined words.")
192+ let cancel = Input.InjectedDependency()
193+ let words = Input.Option(["--word"; "-w"], [||] , "A list of words to be appended")
194+ let separator = Input.Option (["--separator"; "-s"], ", " , "A character that will separate the joined words.")
185195
186196 rootCommand argv {
187197 description "Appends words together"
188- inputs (ctx , words, separator)
198+ inputs (cancel , words, separator)
189199 setHandler app
190200 }
201+ |> Async.AwaitTask
202+ |> Async.RunSynchronously
191203```
192204
193205
0 commit comments