@@ -5,7 +5,6 @@ open System
5
5
open System.Threading
6
6
open System.Threading .Tasks
7
7
open System.CommandLine
8
- open System.CommandLine .Parsing
9
8
10
9
let private def < 'T > = Unchecked.defaultof< 'T>
11
10
@@ -18,6 +17,9 @@ let private parseInput<'V> (handlerInput: ActionInput) (pr: ParseResult) (cancel
18
17
19
18
type CommandSpec < 'Inputs , 'Output > =
20
19
{
20
+ RootCommand: RootCommand
21
+ ParserConfiguration: ParserConfiguration
22
+ InvocationConfiguration: InvocationConfiguration
21
23
Description: string
22
24
Inputs: ActionInput list
23
25
Handler: 'Inputs -> 'Output
@@ -29,6 +31,9 @@ type CommandSpec<'Inputs, 'Output> =
29
31
}
30
32
static member Default =
31
33
{
34
+ RootCommand = RootCommand()
35
+ ParserConfiguration = ParserConfiguration()
36
+ InvocationConfiguration = InvocationConfiguration()
32
37
Description = " My Command"
33
38
Inputs = []
34
39
Aliases = []
@@ -43,6 +48,9 @@ type BaseCommandBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>() =
43
48
44
49
let newHandler handler spec =
45
50
{
51
+ RootCommand = spec.RootCommand
52
+ ParserConfiguration = spec.ParserConfiguration
53
+ InvocationConfiguration = spec.InvocationConfiguration
46
54
Description = spec.Description
47
55
Inputs = spec.Inputs
48
56
Aliases = spec.Aliases
@@ -113,8 +121,6 @@ type BaseCommandBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>() =
113
121
invalidOp " Only 8 inputs are supported."
114
122
115
123
116
- member val CommandLineConfiguration = new CommandLineConfiguration( new RootCommand()) with get, set
117
-
118
124
member this.Yield _ =
119
125
CommandSpec< unit, 'Output>. Default
120
126
@@ -306,126 +312,150 @@ type BaseCommandBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>() =
306
312
cmd
307
313
308
314
309
- /// Builds a `System.CommandLineConfiguration ` that can be passed to the `CommandLineParser.Parse` static method .
310
- type CommandLineConfigurationBuilder < 'A , 'B , 'C , 'D , 'E , 'F , 'G , 'H , 'Output >() =
315
+ /// Builds a `System.RootCommand ` that will be returned to the user for manual execution .
316
+ type ManualExecutingRootCommandBuilder < 'A , 'B , 'C , 'D , 'E , 'F , 'G , 'H , 'Output >() =
311
317
inherit BaseCommandBuilder< 'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>()
312
-
313
- [<CustomOperation( " usePipeline" ); Obsolete( " Please use `configure` instead." ) >]
314
- member this.UsePipeline ( spec : CommandSpec < 'Inputs , 'Output >, subCommand : CommandLineConfiguration -> unit ) =
315
- subCommand this.CommandLineConfiguration
316
- spec
317
-
318
- [<CustomOperation( " usePipeline" ); Obsolete( " Please use `configure` instead." ) >]
319
- member this.UsePipeline ( spec : CommandSpec < 'Inputs , 'Output >, subCommand : CommandLineConfiguration -> CommandLineConfiguration ) =
320
- this.CommandLineConfiguration <- subCommand this.CommandLineConfiguration
321
- spec
322
318
319
+ /// Allows modification of the ParserConfiguration.
320
+ [<Obsolete( " 'configure' has been deprecated in favor of 'configureParser' or 'configureInvocation'." ) >]
323
321
[<CustomOperation( " configure" ) >]
324
- member this.Configure ( spec : CommandSpec < 'Inputs , 'Output >, subCommand : CommandLineConfiguration -> unit ) =
325
- subCommand this.CommandLineConfiguration
322
+ member this.Configure ( spec : CommandSpec < 'Inputs , 'Output >, configure : ParserConfiguration -> unit ) =
323
+ configure spec.ParserConfiguration
326
324
spec
327
325
326
+ /// Allows modification of the ParserConfiguration.
327
+ [<Obsolete( " 'configure' has been deprecated in favor of 'configureParser' or 'configureInvocation'." ) >]
328
328
[<CustomOperation( " configure" ) >]
329
- member this.Configure ( spec : CommandSpec < 'Inputs , 'Output >, subCommand : CommandLineConfiguration -> CommandLineConfiguration ) =
330
- this.CommandLineConfiguration <- subCommand this.CommandLineConfiguration
329
+ member this.Configure ( spec : CommandSpec < 'Inputs , 'Output >, configure : ParserConfiguration -> ParserConfiguration ) =
330
+ { spec with ParserConfiguration = configure spec.ParserConfiguration }
331
+
332
+ /// Allows modification of the ParserConfiguration.
333
+ [<CustomOperation( " configureParser" ) >]
334
+ member this.ConfigureParser ( spec : CommandSpec < 'Inputs , 'Output >, configure : ParserConfiguration -> unit ) =
335
+ configure spec.ParserConfiguration
331
336
spec
332
337
338
+ /// Allows modification of the ParserConfiguration.
339
+ [<CustomOperation( " configureParser" ) >]
340
+ member this.ConfigureParser ( spec : CommandSpec < 'Inputs , 'Output >, configure : ParserConfiguration -> ParserConfiguration ) =
341
+ { spec with ParserConfiguration = configure spec.ParserConfiguration }
342
+
333
343
/// Executes a Command with a handler that returns unit.
334
344
member this.Run ( spec : CommandSpec < 'Inputs , unit >) =
335
- this.CommandLineConfiguration .RootCommand
345
+ spec .RootCommand
336
346
|> this.SetGeneralProperties spec
337
347
|> this.SetHandlerUnit spec
338
348
|> ignore
339
- this.CommandLineConfiguration
349
+ spec.RootCommand
340
350
341
351
/// Executes a Command with a handler that returns int.
342
352
member this.Run ( spec : CommandSpec < 'Inputs , int >) =
343
- this.CommandLineConfiguration .RootCommand
353
+ spec .RootCommand
344
354
|> this.SetGeneralProperties spec
345
355
|> this.SetHandlerInt spec
346
356
|> ignore
347
- this.CommandLineConfiguration
357
+ spec.RootCommand
348
358
349
359
/// Executes a Command with a handler that returns a Task<unit>.
350
360
member this.Run ( spec : CommandSpec < 'Inputs , Task < unit >>) =
351
- this.CommandLineConfiguration .RootCommand
361
+ spec .RootCommand
352
362
|> this.SetGeneralProperties spec
353
363
|> this.SetHandlerTask spec
354
364
|> ignore
355
- this.CommandLineConfiguration
365
+ spec.RootCommand
356
366
357
367
/// Executes a Command with a handler that returns a Task<int>.
358
368
member this.Run ( spec : CommandSpec < 'Inputs , Task < int >>) =
359
- this.CommandLineConfiguration .RootCommand
369
+ spec .RootCommand
360
370
|> this.SetGeneralProperties spec
361
371
|> this.SetHandlerTaskInt spec
362
372
|> ignore
363
- this.CommandLineConfiguration
373
+ spec.RootCommand
364
374
365
375
366
376
/// Builds and executes a `System.CommandLine.RootCommand`.
367
377
type RootCommandBuilder < 'A , 'B , 'C , 'D , 'E , 'F , 'G , 'H , 'Output >( args : string array ) =
368
378
inherit BaseCommandBuilder< 'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>()
369
-
370
- [<CustomOperation( " usePipeline" ); Obsolete( " Please use `configure` instead." ) >]
371
- member this.UsePipeline ( spec : CommandSpec < 'Inputs , 'Output >, subCommand : CommandLineConfiguration -> unit ) =
372
- subCommand this.CommandLineConfiguration
373
- spec
374
379
375
- [<CustomOperation( " usePipeline" ); Obsolete( " Please use `configure` instead." ) >]
376
- member this.UsePipeline ( spec : CommandSpec < 'Inputs , 'Output >, subCommand : CommandLineConfiguration -> CommandLineConfiguration ) =
377
- this.CommandLineConfiguration <- subCommand this.CommandLineConfiguration
378
- spec
379
-
380
- /// Allows modification of the CommandLineConfiguration.
380
+ /// Allows modification of the ParserConfiguration.
381
+ [<Obsolete( " 'configure' has been deprecated in favor of 'configureParser' or 'configureInvocation'." ) >]
381
382
[<CustomOperation( " configure" ) >]
382
- member this.Configure ( spec : CommandSpec < 'Inputs , 'Output >, configure : CommandLineConfiguration -> unit ) =
383
- configure this.CommandLineConfiguration
383
+ member this.Configure ( spec : CommandSpec < 'Inputs , 'Output >, configure : ParserConfiguration -> unit ) =
384
+ configure spec.ParserConfiguration
384
385
spec
385
386
386
- /// Allows modification of the CommandLineConfiguration.
387
+ /// Allows modification of the ParserConfiguration.
388
+ [<Obsolete( " 'configure' has been deprecated in favor of 'configureParser' or 'configureInvocation'." ) >]
387
389
[<CustomOperation( " configure" ) >]
388
- member this.Configure ( spec : CommandSpec < 'Inputs , 'Output >, configure : CommandLineConfiguration -> CommandLineConfiguration ) =
389
- this.CommandLineConfiguration <- configure this.CommandLineConfiguration
390
+ member this.Configure ( spec : CommandSpec < 'Inputs , 'Output >, configure : ParserConfiguration -> ParserConfiguration ) =
391
+ { spec with ParserConfiguration = configure spec.ParserConfiguration }
392
+
393
+ /// Allows modification of the ParserConfiguration.
394
+ [<CustomOperation( " configureParser" ) >]
395
+ member this.ConfigureParser ( spec : CommandSpec < 'Inputs , 'Output >, configure : ParserConfiguration -> unit ) =
396
+ configure spec.ParserConfiguration
390
397
spec
398
+
399
+ /// Allows modification of the ParserConfiguration.
400
+ [<CustomOperation( " configureParser" ) >]
401
+ member this.ConfigureParser ( spec : CommandSpec < 'Inputs , 'Output >, configure : ParserConfiguration -> ParserConfiguration ) =
402
+ { spec with ParserConfiguration = configure spec.ParserConfiguration }
403
+
404
+ /// Allows modification of the InvocationConfiguration.
405
+ [<CustomOperation( " configureInvocation" ) >]
406
+ member this.ConfigureInvocation ( spec : CommandSpec < 'Inputs , 'Output >, configure : InvocationConfiguration -> unit ) =
407
+ configure spec.InvocationConfiguration
408
+ spec
409
+
410
+ /// Allows modification of the InvocationConfiguration.
411
+ [<CustomOperation( " configureInvocation" ) >]
412
+ member this.ConfigureInvocation ( spec : CommandSpec < 'Inputs , 'Output >, configure : InvocationConfiguration -> InvocationConfiguration ) =
413
+ { spec with InvocationConfiguration = configure spec.InvocationConfiguration }
391
414
392
415
/// Executes a Command with a handler that returns unit.
393
416
member this.Run ( spec : CommandSpec < 'Inputs , unit >) =
394
417
let rootCommand =
395
- this.CommandLineConfiguration .RootCommand
418
+ spec .RootCommand
396
419
|> this.SetGeneralProperties spec
397
420
|> this.SetHandlerUnit spec
398
421
399
- rootCommand.Parse( args, this.CommandLineConfiguration) .Invoke()
422
+ rootCommand
423
+ .Parse( args, spec.ParserConfiguration)
424
+ .Invoke( spec.InvocationConfiguration)
400
425
401
426
/// Executes a Command with a handler that returns int.
402
427
member this.Run ( spec : CommandSpec < 'Inputs , int >) =
403
428
let rootCommand =
404
- this.CommandLineConfiguration .RootCommand
429
+ spec .RootCommand
405
430
|> this.SetGeneralProperties spec
406
431
|> this.SetHandlerInt spec
407
432
408
- rootCommand.Parse( args, this.CommandLineConfiguration) .Invoke()
433
+ rootCommand
434
+ .Parse( args, spec.ParserConfiguration)
435
+ .Invoke( spec.InvocationConfiguration)
409
436
410
437
/// Executes a Command with a handler that returns a Task<unit> or Task<int>.
411
438
member this.Run ( spec : CommandSpec < 'Inputs , Task < unit >>) =
412
439
let rootCommand =
413
- this.CommandLineConfiguration .RootCommand
440
+ spec .RootCommand
414
441
|> this.SetGeneralProperties spec
415
442
|> this.SetHandlerTask spec
416
443
417
- rootCommand.Parse( args, this.CommandLineConfiguration) .InvokeAsync()
444
+ rootCommand
445
+ .Parse( args, spec.ParserConfiguration)
446
+ .InvokeAsync( spec.InvocationConfiguration)
418
447
419
448
/// Executes a Command with a handler that returns a Task<unit> or Task<int>.
420
449
member this.Run ( spec : CommandSpec < 'Inputs , Task < int >>) =
421
450
let rootCommand =
422
- this.CommandLineConfiguration .RootCommand
451
+ spec .RootCommand
423
452
|> this.SetGeneralProperties spec
424
453
|> this.SetHandlerTaskInt spec
425
454
426
- rootCommand.Parse( args, this.CommandLineConfiguration) .InvokeAsync()
455
+ rootCommand
456
+ .Parse( args, spec.ParserConfiguration)
457
+ .InvokeAsync( spec.InvocationConfiguration)
427
458
428
-
429
459
/// Builds a `System.CommandLine.Command`.
430
460
type CommandBuilder < 'A , 'B , 'C , 'D , 'E , 'F , 'G , 'H , 'Output >( name : string ) =
431
461
inherit BaseCommandBuilder< 'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>()
@@ -460,14 +490,15 @@ type CommandBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>(name: string) =
460
490
|> this.SetHandlerTaskInt spec
461
491
462
492
463
- /// Builds a `System.CommandLineConfiguration` that can be passed to the `CommandLineParser.Parse` static method using computation expression syntax.
464
- let commandLineConfiguration < 'A , 'B , 'C , 'D , 'E , 'F , 'G , 'H , 'Output > =
465
- CommandLineConfigurationBuilder< 'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>()
466
-
467
- /// Builds and executes a `System.CommandLine.RootCommand` using computation expression syntax.
493
+ /// Builds a self-invoking `System.CommandLine.RootCommand`.
468
494
let rootCommand < 'A , 'B , 'C , 'D , 'E , 'F , 'G , 'H , 'Output >( args : string array )=
469
495
RootCommandBuilder< 'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>( args)
470
496
471
- /// Builds a `System.CommandLine.Command` using computation expression syntax .
497
+ /// Builds a `System.CommandLine.Command`.
472
498
let command < 'A , 'B , 'C , 'D , 'E , 'F , 'G , 'H , 'Output > ( name : string ) =
473
499
CommandBuilder< 'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>( name)
500
+
501
+ module ManualInvocation =
502
+ /// Builds a `System.CommandLine.RootCommand` that will be returned to the user for manual invocation.
503
+ let rootCommand < 'A , 'B , 'C , 'D , 'E , 'F , 'G , 'H , 'Output > =
504
+ ManualExecutingRootCommandBuilder< 'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>()
0 commit comments