File tree Expand file tree Collapse file tree 2 files changed +30
-6
lines changed
Expand file tree Collapse file tree 2 files changed +30
-6
lines changed Original file line number Diff line number Diff line change @@ -24,15 +24,27 @@ type HandlerInput(source: HandlerInputSource) =
2424
2525type HandlerInput < 'T >( inputType : HandlerInputSource ) =
2626 inherit HandlerInput( inputType)
27+
2728 /// Converts a System.CommandLine.Option<'T> for usage with the CommandBuilder.
2829 static member OfOption < 'T >( o : Option < 'T >) = o :> Option |> ParsedOption |> HandlerInput< 'T>
30+
2931 /// Converts a System.CommandLine.Argument<'T> for usage with the CommandBuilder.
3032 static member OfArgument < 'T >( a : Argument < 'T >) = a :> Argument |> ParsedArgument |> HandlerInput< 'T>
33+
34+ /// Gets the value of an Option or Argument from the InvocationContext.
3135 member this.GetValue ( ctx : System.CommandLine.Invocation.InvocationContext ) =
3236 match this.Source with
3337 | ParsedOption o -> o :?> Option< 'T> |> ctx.ParseResult.GetValueForOption
3438 | ParsedArgument a -> a :?> Argument< 'T> |> ctx.ParseResult.GetValueForArgument
3539 | Context -> ctx |> unbox< 'T>
40+
41+ /// Gets the value of an Option or Argument from the Parser.
42+ member this.GetValue ( parseResult : Parsing.ParseResult ) =
43+ match this.Source with
44+ | ParsedOption o -> o :?> Option< 'T> |> parseResult.GetValueForOption
45+ | ParsedArgument a -> a :?> Argument< 'T> |> parseResult.GetValueForArgument
46+ | Context -> failwith " Cannot get a value for InvocationContext from a ParseResult."
47+
3648
3749let private applyConfiguration configure a =
3850 configure a; a
Original file line number Diff line number Diff line change 33open System.IO
44open FSharp.SystemCommandLine
55open System.CommandLine .Invocation
6+ open System.CommandLine .Parsing
67
78module Global =
89 let enableLogging = Input.Option< bool>( " --enable-logging" , false )
@@ -70,9 +71,20 @@ let ioCmd =
7071
7172//[<EntryPoint>]
7273let main argv =
73- rootCommand argv {
74- description " Sample app for System.CommandLine"
75- setHandler id
76- addGlobalOptions Global.options
77- addCommand ioCmd
78- }
74+ let ctx = Input.Context()
75+
76+ let parser =
77+ rootCommandParser {
78+ description " Sample app for System.CommandLine"
79+ setHandler id
80+ addGlobalOptions Global.options
81+ addCommand ioCmd
82+ }
83+
84+ let parseResult = parser.Parse( argv)
85+
86+ let loggingEnabled = Global.enableLogging.GetValue parseResult
87+ printfn $" ROOT: Logging enabled: {loggingEnabled}"
88+
89+ parseResult.Invoke()
90+
You can’t perform that action at this time.
0 commit comments