Skip to content

Commit 9582bf3

Browse files
committed
Slightly improved getValue fn
1 parent 135a574 commit 9582bf3

1 file changed

Lines changed: 43 additions & 40 deletions

File tree

src/FSharp.SystemCommandLine/CommandBuilders.fs

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ let private def<'T> = Unchecked.defaultof<'T>
1616
let castValueDescriptor (ivdInputs: IValueDescriptor list) (idx: int) =
1717
ivdInputs[idx] :?> IValueDescriptor<'InputType>
1818

19+
let getValue<'V> (handlerInputs: HandlerInput list) (ctx: IC) (idx: int) =
20+
match handlerInputs[idx].Source with
21+
| ParsedOption o -> ctx.ParseResult.GetValueForOption<'V>(o :?> Option<'V>)
22+
| ParsedArgument a -> ctx.ParseResult.GetValueForArgument<'V>(a :?> Argument<'V>)
23+
| InjectedDependency -> ctx |> unbox<'V>
24+
1925
type CommandSpec<'Inputs, 'Output> =
2026
{
2127
Description: string
@@ -152,65 +158,62 @@ type BaseCommandBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>() =
152158
spec.Handler (args :?> 'Inputs)
153159

154160
let getValue (ctx: IC) (idx: int) =
155-
match spec.Inputs[idx].Source with
156-
| ParsedOption o -> ctx.ParseResult.GetValueForOption(o)
157-
| ParsedArgument a -> ctx.ParseResult.GetValueForArgument(a)
158-
| InjectedDependency -> ctx
161+
getValue spec.Inputs ctx idx
159162

160163
match spec.Inputs.Length with
161164
| 00 -> cmd.SetHandler(Action<IC>(fun ctx ->
162165
ctx.ExitCode <- handler ()))
163166
| 01 -> cmd.SetHandler(Action<IC>(fun ctx ->
164-
let a = getValue ctx 0 :?> 'A
167+
let a: 'A = getValue ctx 0
165168
ctx.ExitCode <- handler (a)))
166169
| 02 -> cmd.SetHandler(Action<IC>(fun ctx ->
167-
let a = getValue ctx 0 :?> 'A
168-
let b = getValue ctx 1 :?> 'B
170+
let a: 'A = getValue ctx 0
171+
let b: 'B = getValue ctx 1
169172
ctx.ExitCode <- handler (a, b)))
170173
| 03 -> cmd.SetHandler(Action<IC>(fun ctx ->
171-
let a = getValue ctx 0 :?> 'A
172-
let b = getValue ctx 1 :?> 'B
173-
let c = getValue ctx 2 :?> 'C
174+
let a: 'A = getValue ctx 0
175+
let b: 'B = getValue ctx 1
176+
let c: 'C = getValue ctx 2
174177
ctx.ExitCode <- handler (a, b, c)))
175178
| 04 -> cmd.SetHandler(Action<IC>(fun ctx ->
176-
let a = getValue ctx 0 :?> 'A
177-
let b = getValue ctx 1 :?> 'B
178-
let c = getValue ctx 2 :?> 'C
179-
let d = getValue ctx 3 :?> 'D
179+
let a: 'A = getValue ctx 0
180+
let b: 'B = getValue ctx 1
181+
let c: 'C = getValue ctx 2
182+
let d: 'D = getValue ctx 3
180183
ctx.ExitCode <- handler (a, b, c, d)))
181184
| 05 -> cmd.SetHandler(Action<IC>(fun ctx ->
182-
let a = getValue ctx 0 :?> 'A
183-
let b = getValue ctx 1 :?> 'B
184-
let c = getValue ctx 2 :?> 'C
185-
let d = getValue ctx 3 :?> 'D
186-
let e = getValue ctx 4 :?> 'E
185+
let a: 'A = getValue ctx 0
186+
let b: 'B = getValue ctx 1
187+
let c: 'C = getValue ctx 2
188+
let d: 'D = getValue ctx 3
189+
let e: 'E = getValue ctx 4
187190
ctx.ExitCode <- handler (a, b, c, d, e)))
188191
| 06 -> cmd.SetHandler(Action<IC>(fun ctx ->
189-
let a = getValue ctx 0 :?> 'A
190-
let b = getValue ctx 1 :?> 'B
191-
let c = getValue ctx 2 :?> 'C
192-
let d = getValue ctx 3 :?> 'D
193-
let e = getValue ctx 4 :?> 'E
194-
let f = getValue ctx 5 :?> 'F
192+
let a: 'A = getValue ctx 0
193+
let b: 'B = getValue ctx 1
194+
let c: 'C = getValue ctx 2
195+
let d: 'D = getValue ctx 3
196+
let e: 'E = getValue ctx 4
197+
let f: 'F = getValue ctx 5
195198
ctx.ExitCode <- handler (a, b, c, d, e, f)))
196199
| 07 -> cmd.SetHandler(Action<IC>(fun ctx ->
197-
let a = getValue ctx 0 :?> 'A
198-
let b = getValue ctx 1 :?> 'B
199-
let c = getValue ctx 2 :?> 'C
200-
let d = getValue ctx 3 :?> 'D
201-
let e = getValue ctx 4 :?> 'E
202-
let f = getValue ctx 5 :?> 'F
203-
let g = getValue ctx 6 :?> 'G
200+
let a: 'A = getValue ctx 0
201+
let b: 'B = getValue ctx 1
202+
let c: 'C = getValue ctx 2
203+
let d: 'D = getValue ctx 3
204+
let e: 'E = getValue ctx 4
205+
let f: 'F = getValue ctx 5
206+
let g: 'G = getValue ctx 6
204207
ctx.ExitCode <- handler (a, b, c, d, e, f, g)))
205208
| 08 -> cmd.SetHandler(Action<IC>(fun ctx ->
206-
let a = getValue ctx 0 :?> 'A
207-
let b = getValue ctx 1 :?> 'B
208-
let c = getValue ctx 2 :?> 'C
209-
let d = getValue ctx 3 :?> 'D
210-
let e = getValue ctx 4 :?> 'E
211-
let f = getValue ctx 5 :?> 'F
212-
let g = getValue ctx 6 :?> 'G
213-
let h = getValue ctx 7 :?> 'H
209+
let a: 'A = getValue ctx 0
210+
let b: 'B = getValue ctx 1
211+
let c: 'C = getValue ctx 2
212+
let d: 'D = getValue ctx 3
213+
let e: 'E = getValue ctx 4
214+
let f: 'F = getValue ctx 5
215+
let g: 'G = getValue ctx 6
216+
let h: 'H = getValue ctx 7
214217
ctx.ExitCode <- handler (a, b, c, d, e, f, g, h)))
215218
| _ -> raise (NotImplementedException("Only 8 inputs are supported."))
216219
cmd

0 commit comments

Comments
 (0)