@@ -8,13 +8,13 @@ open System.CommandLine.Binding
88open System.CommandLine .Builder
99open System.CommandLine .Parsing
1010
11- type private IVD < 'T > = IValueDescriptor < 'T>
11+ type private HI < 'T > = HandlerInput < 'T>
1212let private def < 'T > = Unchecked.defaultof< 'T>
1313
1414type CommandSpec < 'Inputs , 'Output > =
1515 {
1616 Description: string
17- Inputs: IValueDescriptor list
17+ Inputs: HandlerInput list
1818 Handler: 'Inputs -> 'Output
1919 SubCommands: System .CommandLine .Command list
2020 }
@@ -36,7 +36,7 @@ type BaseCommandBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'I, 'J, 'K, 'L, 'M, 'N,
3636 Handler = handler
3737 SubCommands = spec.SubCommands
3838 }
39-
39+
4040 member val CommandLineBuilder = CommandLineBuilder() .UseDefaults() with get, set
4141
4242 member this.Yield _ =
@@ -50,67 +50,67 @@ type BaseCommandBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'I, 'J, 'K, 'L, 'M, 'N,
5050 { spec with Description = description }
5151
5252 [<CustomOperation( " inputs" ) >]
53- member this.Inputs ( spec : CommandSpec < 'T , 'Output >, a : IVD < 'A >) =
53+ member this.Inputs ( spec : CommandSpec < 'T , 'Output >, a : HI < 'A >) =
5454 { newHandler def< 'A -> 'Output> spec with Inputs = [ a ] }
5555
5656 [<CustomOperation( " inputs" ) >]
57- member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : IVD < 'A >, b : IVD < 'B >)) =
57+ member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : HI < 'A >, b : HI < 'B >)) =
5858 { newHandler def< 'A * 'B -> 'Output> spec with Inputs = [ a; b ] }
5959
6060 [<CustomOperation( " inputs" ) >]
61- member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : IVD < 'A >, b : IVD < 'B >, c : IVD < 'C >)) =
61+ member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : HI < 'A >, b : HI < 'B >, c : HI < 'C >)) =
6262 { newHandler def< 'A * 'B * 'C -> 'Output> spec with Inputs = [ a; b; c ] }
6363
6464 [<CustomOperation( " inputs" ) >]
65- member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : IVD < 'A >, b : IVD < 'B >, c : IVD < 'C >, d : IVD < 'D >)) =
65+ member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : HI < 'A >, b : HI < 'B >, c : HI < 'C >, d : HI < 'D >)) =
6666 { newHandler def< 'A * 'B * 'C * 'D -> 'Output> spec with Inputs = [ a; b; c; d ] }
6767
6868 [<CustomOperation( " inputs" ) >]
69- member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : IVD < 'A >, b : IVD < 'B >, c : IVD < 'C >, d : IVD < 'D >, e : IVD < 'E >)) =
69+ member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : HI < 'A >, b : HI < 'B >, c : HI < 'C >, d : HI < 'D >, e : HI < 'E >)) =
7070 { newHandler def< 'A * 'B * 'C * 'D * 'E -> 'Output> spec with Inputs = [ a; b; c; d; e ] }
7171
7272 [<CustomOperation( " inputs" ) >]
73- member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : IVD < 'A >, b : IVD < 'B >, c : IVD < 'C >, d : IVD < 'D >, e : IVD < 'E >, f : IVD < 'F >)) =
73+ member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : HI < 'A >, b : HI < 'B >, c : HI < 'C >, d : HI < 'D >, e : HI < 'E >, f : HI < 'F >)) =
7474 { newHandler def< 'A * 'B * 'C * 'D * 'E * 'F -> 'Output> spec with Inputs = [ a; b; c; d; e; f ] }
7575
7676 [<CustomOperation( " inputs" ) >]
77- member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : IVD < 'A >, b : IVD < 'B >, c : IVD < 'C >, d : IVD < 'D >, e : IVD < 'E >, f : IVD < 'F >, g : IVD < 'G >)) =
77+ member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : HI < 'A >, b : HI < 'B >, c : HI < 'C >, d : HI < 'D >, e : HI < 'E >, f : HI < 'F >, g : HI < 'G >)) =
7878 { newHandler def< 'A * 'B * 'C * 'D * 'E * 'F * 'G -> 'Output> spec with Inputs = [ a; b; c; d; e; f; g ] }
7979
8080 [<CustomOperation( " inputs" ) >]
81- member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : IVD < 'A >, b : IVD < 'B >, c : IVD < 'C >, d : IVD < 'D >, e : IVD < 'E >, f : IVD < 'F >, g : IVD < 'G >, h : IVD < 'H >)) =
81+ member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : HI < 'A >, b : HI < 'B >, c : HI < 'C >, d : HI < 'D >, e : HI < 'E >, f : HI < 'F >, g : HI < 'G >, h : HI < 'H >)) =
8282 { newHandler def< 'A * 'B * 'C * 'D * 'E * 'F * 'G * 'H -> 'Output> spec with Inputs = [ a; b; c; d; e; f; g; h ] }
8383
8484 [<CustomOperation( " inputs" ) >]
85- member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : IVD < 'A >, b : IVD < 'B >, c : IVD < 'C >, d : IVD < 'D >, e : IVD < 'E >, f : IVD < 'F >, g : IVD < 'G >, h : IVD < 'H >, i : IVD < 'I >)) =
85+ member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : HI < 'A >, b : HI < 'B >, c : HI < 'C >, d : HI < 'D >, e : HI < 'E >, f : HI < 'F >, g : HI < 'G >, h : HI < 'H >, i : HI < 'I >)) =
8686 { newHandler def< 'A * 'B * 'C * 'D * 'E * 'F * 'G * 'H * 'I -> 'Output> spec with Inputs = [ a; b; c; d; e; f; g; h; i ] }
8787
8888 [<CustomOperation( " inputs" ) >]
89- member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : IVD < 'A >, b : IVD < 'B >, c : IVD < 'C >, d : IVD < 'D >, e : IVD < 'E >, f : IVD < 'F >, g : IVD < 'G >, h : IVD < 'H >, i : IVD < 'I >, j : IVD < 'J >)) =
89+ member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : HI < 'A >, b : HI < 'B >, c : HI < 'C >, d : HI < 'D >, e : HI < 'E >, f : HI < 'F >, g : HI < 'G >, h : HI < 'H >, i : HI < 'I >, j : HI < 'J >)) =
9090 { newHandler def< 'A * 'B * 'C * 'D * 'E * 'F * 'G * 'H * 'I * 'J -> 'Output> spec with Inputs = [ a; b; c; d; e; f; g; h; i; j ] }
9191
9292 [<CustomOperation( " inputs" ) >]
93- member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : IVD < 'A >, b : IVD < 'B >, c : IVD < 'C >, d : IVD < 'D >, e : IVD < 'E >, f : IVD < 'F >, g : IVD < 'G >, h : IVD < 'H >, i : IVD < 'I >, j : IVD < 'J >, k : IVD < 'K >)) =
93+ member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : HI < 'A >, b : HI < 'B >, c : HI < 'C >, d : HI < 'D >, e : HI < 'E >, f : HI < 'F >, g : HI < 'G >, h : HI < 'H >, i : HI < 'I >, j : HI < 'J >, k : HI < 'K >)) =
9494 { newHandler def< 'A * 'B * 'C * 'D * 'E * 'F * 'G * 'H * 'I * 'J * 'K -> 'Output> spec with Inputs = [ a; b; c; d; e; f; g; h; i; j; k ] }
9595
9696 [<CustomOperation( " inputs" ) >]
97- member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : IVD < 'A >, b : IVD < 'B >, c : IVD < 'C >, d : IVD < 'D >, e : IVD < 'E >, f : IVD < 'F >, g : IVD < 'G >, h : IVD < 'H >, i : IVD < 'I >, j : IVD < 'J >, k : IVD < 'K >, l : IVD < 'L >)) =
97+ member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : HI < 'A >, b : HI < 'B >, c : HI < 'C >, d : HI < 'D >, e : HI < 'E >, f : HI < 'F >, g : HI < 'G >, h : HI < 'H >, i : HI < 'I >, j : HI < 'J >, k : HI < 'K >, l : HI < 'L >)) =
9898 { newHandler def< 'A * 'B * 'C * 'D * 'E * 'F * 'G * 'H * 'I * 'J * 'K * 'L -> 'Output> spec with Inputs = [ a; b; c; d; e; f; g; h; i; j; k; l ] }
9999
100100 [<CustomOperation( " inputs" ) >]
101- member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : IVD < 'A >, b : IVD < 'B >, c : IVD < 'C >, d : IVD < 'D >, e : IVD < 'E >, f : IVD < 'F >, g : IVD < 'G >, h : IVD < 'H >, i : IVD < 'I >, j : IVD < 'J >, k : IVD < 'K >, l : IVD < 'L >, m : IVD < 'M >)) =
101+ member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : HI < 'A >, b : HI < 'B >, c : HI < 'C >, d : HI < 'D >, e : HI < 'E >, f : HI < 'F >, g : HI < 'G >, h : HI < 'H >, i : HI < 'I >, j : HI < 'J >, k : HI < 'K >, l : HI < 'L >, m : HI < 'M >)) =
102102 { newHandler def< 'A * 'B * 'C * 'D * 'E * 'F * 'G * 'H * 'I * 'J * 'K * 'L * 'M -> 'Output> spec with Inputs = [ a; b; c; d; e; f; g; h; i; j; k; l; m ] }
103103
104104 [<CustomOperation( " inputs" ) >]
105- member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : IVD < 'A >, b : IVD < 'B >, c : IVD < 'C >, d : IVD < 'D >, e : IVD < 'E >, f : IVD < 'F >, g : IVD < 'G >, h : IVD < 'H >, i : IVD < 'I >, j : IVD < 'J >, k : IVD < 'K >, l : IVD < 'L >, m : IVD < 'M >, n : IVD < 'N >)) =
105+ member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : HI < 'A >, b : HI < 'B >, c : HI < 'C >, d : HI < 'D >, e : HI < 'E >, f : HI < 'F >, g : HI < 'G >, h : HI < 'H >, i : HI < 'I >, j : HI < 'J >, k : HI < 'K >, l : HI < 'L >, m : HI < 'M >, n : HI < 'N >)) =
106106 { newHandler def< 'A * 'B * 'C * 'D * 'E * 'F * 'G * 'H * 'I * 'J * 'K * 'L * 'M * 'N -> 'Output> spec with Inputs = [ a; b; c; d; e; f; g; h; i; j; k; l; m; n ] }
107107
108108 [<CustomOperation( " inputs" ) >]
109- member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : IVD < 'A >, b : IVD < 'B >, c : IVD < 'C >, d : IVD < 'D >, e : IVD < 'E >, f : IVD < 'F >, g : IVD < 'G >, h : IVD < 'H >, i : IVD < 'I >, j : IVD < 'J >, k : IVD < 'K >, l : IVD < 'L >, m : IVD < 'M >, n : IVD < 'N >, o : IVD < 'O >)) =
109+ member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : HI < 'A >, b : HI < 'B >, c : HI < 'C >, d : HI < 'D >, e : HI < 'E >, f : HI < 'F >, g : HI < 'G >, h : HI < 'H >, i : HI < 'I >, j : HI < 'J >, k : HI < 'K >, l : HI < 'L >, m : HI < 'M >, n : HI < 'N >, o : HI < 'O >)) =
110110 { newHandler def< 'A * 'B * 'C * 'D * 'E * 'F * 'G * 'H * 'I * 'J * 'K * 'L * 'M * 'N * 'O -> 'Output> spec with Inputs = [ a; b; c; d; e; f; g; h; i; j; k; l; m; n; o ] }
111111
112112 [<CustomOperation( " inputs" ) >]
113- member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : IVD < 'A >, b : IVD < 'B >, c : IVD < 'C >, d : IVD < 'D >, e : IVD < 'E >, f : IVD < 'F >, g : IVD < 'G >, h : IVD < 'H >, i : IVD < 'I >, j : IVD < 'J >, k : IVD < 'K >, l : IVD < 'L >, m : IVD < 'M >, n : IVD < 'N >, o : IVD < 'O >, p : IVD < 'P >)) =
113+ member this.Inputs ( spec : CommandSpec < 'T , 'Output >, ( a : HI < 'A >, b : HI < 'B >, c : HI < 'C >, d : HI < 'D >, e : HI < 'E >, f : HI < 'F >, g : HI < 'G >, h : HI < 'H >, i : HI < 'I >, j : HI < 'J >, k : HI < 'K >, l : HI < 'L >, m : HI < 'M >, n : HI < 'N >, o : HI < 'O >, p : HI < 'P >)) =
114114 { newHandler def< 'A * 'B * 'C * 'D * 'E * 'F * 'G * 'H * 'I * 'J * 'K * 'L * 'M * 'N * 'O * 'P -> 'Output> spec with Inputs = [ a; b; c; d; e; f; g; h; i; j; k; l; m; n; o; p ] }
115115
116116 [<CustomOperation( " setHandler" ) >]
@@ -124,16 +124,31 @@ type BaseCommandBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'I, 'J, 'K, 'L, 'M, 'N,
124124 /// Sets general properties on the command.
125125 member this.SetGeneralProperties ( spec : CommandSpec < 'T , 'U >) ( cmd : Command ) =
126126 cmd.Description <- spec.Description
127- spec.Inputs |> List.choose ( function | :? Option as opt -> Some opt | _ -> None) |> List.iter cmd.AddOption
128- spec.Inputs |> List.choose ( function | :? Argument as arg -> Some arg | _ -> None) |> List.iter cmd.AddArgument
127+ spec.Inputs
128+ |> Seq.iter ( fun input ->
129+ match input.Source with
130+ | ParsedOption o -> cmd.AddOption o
131+ | ParsedArgument a -> cmd.AddArgument a
132+ | InjectedDependency -> () // DI system will inject this input
133+ )
134+
129135 spec.SubCommands |> List.iter cmd.AddCommand
130136 cmd
131137
132138 /// Sets a command handler that returns unit.
133139 member this.SetActionHandler ( spec : CommandSpec < 'Inputs , unit >) ( cmd : Command ) =
134- let inputs = spec.Inputs |> List.toArray
135140 let handler ( args : obj ) = spec.Handler ( args :?> 'Inputs)
136141
142+ let inputs =
143+ spec.Inputs
144+ |> List.choose ( fun input ->
145+ match input.Source with
146+ | ParsedOption o -> o :> IValueDescriptor |> Some
147+ | ParsedArgument a -> a :> IValueDescriptor |> Some
148+ | InjectedDependency -> None
149+ )
150+ |> List.toArray
151+
137152 match spec.Inputs.Length with
138153 | 00 -> cmd.SetHandler( Action( fun () -> handler ()))
139154 | 01 -> cmd.SetHandler( Action< 'A>( fun a -> handler ( a)), inputs)
@@ -157,12 +172,21 @@ type BaseCommandBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'I, 'J, 'K, 'L, 'M, 'N,
157172
158173 /// Sets a command handler that returns a Task.
159174 member this.SetFuncHandler ( spec : CommandSpec < 'Inputs , Task < 'ReturnValue >>) ( cmd : Command ) =
160- let inputs = spec.Inputs |> List.toArray
161175 let handler ( args : obj ) =
162176 task {
163177 return ! spec.Handler ( args :?> 'Inputs )
164178 }
165179
180+ let inputs =
181+ spec.Inputs
182+ |> List.choose ( fun input ->
183+ match input.Source with
184+ | ParsedOption o -> o :> IValueDescriptor |> Some
185+ | ParsedArgument a -> a :> IValueDescriptor |> Some
186+ | InjectedDependency -> None
187+ )
188+ |> List.toArray
189+
166190 match spec.Inputs.Length with
167191 | 00 -> cmd.SetHandler( Func< Task>( fun () -> handler ()))
168192 | 01 -> cmd.SetHandler( Func< 'A, Task>( fun a -> handler ( a)), inputs)
0 commit comments