|
1 | 1 | module ProgramTask |
2 | 2 |
|
3 | 3 | open FSharp.SystemCommandLine |
4 | | -open System.CommandLine.Builder |
| 4 | +open System.Threading |
5 | 5 | open System.Threading.Tasks |
6 | 6 |
|
7 | | -type WordService() = |
8 | | - member _.Join(separator: string, words: string list) = |
9 | | - task { |
10 | | - do! Task.Delay(1000) |
11 | | - return System.String.Join(separator, words) |
12 | | - } |
13 | | - |
14 | | -let app (svc: WordService) (words: string list, separator: string) = |
| 7 | +let app (cancel: CancellationToken, words: string array, separator: string) = |
15 | 8 | task { |
16 | | - let! result = svc.Join(separator, words) |
17 | | - result |> printfn "Result: %s" |
| 9 | + for i in [1..20] do |
| 10 | + if cancel.IsCancellationRequested then |
| 11 | + printfn "Cancellation Requested!" |
| 12 | + raise (new System.OperationCanceledException()) |
| 13 | + else |
| 14 | + printfn $"{i}" |
| 15 | + do! Task.Delay(1000) |
| 16 | + |
| 17 | + System.String.Join(separator, words) |
| 18 | + |> printfn "Result: %s" |
18 | 19 | } |
19 | 20 |
|
20 | 21 | //[<EntryPoint>] |
21 | 22 | let main argv = |
22 | | - let words = Input.Option(["--word"; "-w"], [], "A list of words to be appended") |
| 23 | + let cancel = Input.InjectedDependency() |
| 24 | + let words = Input.Option(["--word"; "-w"], [||], "A list of words to be appended") |
23 | 25 | let separator = Input.Option(["--separator"; "-s"], ", ", "A character that will separate the joined words.") |
24 | 26 |
|
25 | | - // Initialize app dependencies |
26 | | - let svc = WordService() |
27 | | - |
28 | 27 | rootCommand argv { |
29 | 28 | description "Appends words together" |
30 | | - inputs (words, separator) |
31 | | - usePipeline (fun builder -> |
32 | | - CommandLineBuilder() // Override pipeline |
33 | | - .UseSuggestDirective() |
34 | | - .UseParseDirective() |
35 | | - ) |
36 | | - setHandler (app svc) // Partially apply app dependencies |
| 29 | + inputs (cancel, words, separator) |
| 30 | + setHandler app |
37 | 31 | } |
38 | 32 | |> Async.AwaitTask |
39 | 33 | |> Async.RunSynchronously |
0 commit comments