@@ -17,10 +17,20 @@ let private parseInput<'V> (handlerInputs: HandlerInput list) (ctx: IC) (idx: in
1717 | ParsedArgument a -> ctx.ParseResult.GetValueForArgument< 'V>( a :?> Argument< 'V>)
1818 | Context -> ctx |> unbox< 'V>
1919
20+ let private addGlobalOptionsToParser ( globalInputs : HandlerInput list ) ( parser : Parser ) =
21+ globalInputs
22+ |> List.iter ( fun gi ->
23+ match gi.Source with
24+ | ParsedOption o -> parser.Configuration.RootCommand.AddGlobalOption( o)
25+ | _ -> ()
26+ )
27+ parser
28+
2029type CommandSpec < 'Inputs , 'Output > =
2130 {
2231 Description: string
2332 Inputs: HandlerInput list
33+ GlobalInputs: HandlerInput list
2434 Handler: 'Inputs -> 'Output
2535 Aliases: string list
2636 SubCommands: System .CommandLine .Command list
@@ -31,6 +41,7 @@ type CommandSpec<'Inputs, 'Output> =
3141 {
3242 Description = " My Command"
3343 Inputs = []
44+ GlobalInputs = []
3445 Aliases = []
3546 ExtraInputs = []
3647 Handler = def< unit -> 'Output> // Support unit -> 'Output handler by default
@@ -44,6 +55,7 @@ type BaseCommandBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>() =
4455 {
4556 Description = spec.Description
4657 Inputs = spec.Inputs
58+ GlobalInputs = spec.GlobalInputs
4759 Aliases = spec.Aliases
4860 ExtraInputs = spec.ExtraInputs
4961 Handler = handler
@@ -108,6 +120,14 @@ type BaseCommandBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>() =
108120 member this.SetHandler ( spec : CommandSpec < 'Inputs , 'Output >, handler : 'Inputs -> 'Output ) =
109121 newHandler handler spec
110122
123+ [<CustomOperation( " addGlobalOption" ) >]
124+ member this.AddGlobalOption ( spec : CommandSpec < 'Inputs , 'Output >, globalInput : HandlerInput ) =
125+ { spec with GlobalInputs = spec.GlobalInputs @ [ globalInput ] }
126+
127+ [<CustomOperation( " addGlobalOptions" ) >]
128+ member this.AddGlobalOptions ( spec : CommandSpec < 'Inputs , 'Output >, globalInputs : HandlerInput seq ) =
129+ { spec with GlobalInputs = spec.GlobalInputs @ ( globalInputs |> List.ofSeq) }
130+
111131 [<Obsolete( " 'setCommand' has been deprecated in favor of 'addCommand' or 'addCommands'." ) >]
112132 [<CustomOperation( " setCommand" ) >]
113133 member this.SetCommand ( spec : CommandSpec < 'Inputs , 'Output >, subCommand : System.CommandLine.Command ) =
@@ -401,6 +421,7 @@ type RootCommandParserBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>() =
401421 |> this.SetHandlerUnit spec
402422 |> ignore
403423 this.CommandLineBuilder.Build()
424+ |> addGlobalOptionsToParser spec.GlobalInputs
404425
405426 /// Executes a Command with a handler that returns int.
406427 member this.Run ( spec : CommandSpec < 'Inputs , int >) =
@@ -409,6 +430,7 @@ type RootCommandParserBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>() =
409430 |> this.SetHandlerInt spec
410431 |> ignore
411432 this.CommandLineBuilder.Build()
433+ |> addGlobalOptionsToParser spec.GlobalInputs
412434
413435 /// Executes a Command with a handler that returns a Task<unit> or Task<int>.
414436 member this.Run ( spec : CommandSpec < 'Inputs , Task < 'ReturnValue >>) =
@@ -417,6 +439,7 @@ type RootCommandParserBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>() =
417439 |> this.SetHandlerTask spec
418440 |> ignore
419441 this.CommandLineBuilder.Build()
442+ |> addGlobalOptionsToParser spec.GlobalInputs
420443
421444
422445/// Builds and executes a `System.CommandLine.RootCommand`.
@@ -439,46 +462,73 @@ type RootCommandBuilder<'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>(args: string ar
439462 |> this.SetGeneralProperties spec
440463 |> this.SetHandlerUnit spec
441464 |> ignore
442- this.CommandLineBuilder.Build() .Parse( args) .Invoke()
465+
466+ let parser =
467+ this.CommandLineBuilder.Build()
468+ |> addGlobalOptionsToParser spec.GlobalInputs
469+
470+ parser.Parse( args) .Invoke()
443471
444472 /// Executes a Command with a handler that returns int.
445473 member this.Run ( spec : CommandSpec < 'Inputs , int >) =
446474 this.CommandLineBuilder.Command
447475 |> this.SetGeneralProperties spec
448476 |> this.SetHandlerInt spec
449477 |> ignore
450- this.CommandLineBuilder.Build() .Parse( args) .Invoke()
478+
479+ let parser =
480+ this.CommandLineBuilder.Build()
481+ |> addGlobalOptionsToParser spec.GlobalInputs
482+
483+ parser.Parse( args) .Invoke()
451484
452485 /// Executes a Command with a handler that returns a Task<unit> or Task<int>.
453486 member this.Run ( spec : CommandSpec < 'Inputs , Task < 'ReturnValue >>) =
454487 this.CommandLineBuilder.Command
455488 |> this.SetGeneralProperties spec
456489 |> this.SetHandlerTask spec
457490 |> ignore
458- this.CommandLineBuilder.Build() .Parse( args) .InvokeAsync()
491+
492+ let parser =
493+ this.CommandLineBuilder.Build()
494+ |> addGlobalOptionsToParser spec.GlobalInputs
495+
496+ parser.Parse( args) .InvokeAsync()
459497
460498
461499/// Builds a `System.CommandLine.Command`.
462500type CommandBuilder < 'A , 'B , 'C , 'D , 'E , 'F , 'G , 'H , 'Output >( name : string ) =
463501 inherit BaseCommandBuilder< 'A, 'B, 'C, 'D, 'E, 'F, 'G, 'H, 'Output>()
464502
503+ let addGlobalOptionsToCommand ( globalOptions : HandlerInput list ) ( cmd : Command ) =
504+ globalOptions
505+ |> List.iter ( fun g ->
506+ match g.Source with
507+ | ParsedOption o -> cmd.AddGlobalOption( o)
508+ | _ -> ()
509+ )
510+ cmd
511+
465512 /// Returns a Command with a handler that returns unit.
466513 member this.Run ( spec : CommandSpec < 'Inputs , unit >) =
467514 Command( name)
468515 |> this.SetGeneralProperties spec
469516 |> this.SetHandlerUnit spec
517+ |> addGlobalOptionsToCommand spec.GlobalInputs
470518
471519 /// Executes a Command with a handler that returns int.
472520 member this.Run ( spec : CommandSpec < 'Inputs , int >) =
473521 Command( name)
474522 |> this.SetGeneralProperties spec
475523 |> this.SetHandlerInt spec
524+ |> addGlobalOptionsToCommand spec.GlobalInputs
476525
477526 /// Executes a Command with a handler that returns a Task<unit> or Task<int>.
478527 member this.Run ( spec : CommandSpec < 'Inputs , Task < 'ReturnValue >>) =
479528 Command( name)
480529 |> this.SetGeneralProperties spec
481530 |> this.SetHandlerTask spec
531+ |> addGlobalOptionsToCommand spec.GlobalInputs
482532
483533
484534/// Builds a `System.CommandLine.RootCommand` using computation expression syntax.
0 commit comments