Skip to content

Commit 9879bf1

Browse files
committed
Added an overload of "Input" "GetValue" to get a parsed value from a ParseResult.
1 parent 89e9cc2 commit 9879bf1

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/FSharp.SystemCommandLine/Inputs.fs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,27 @@ type HandlerInput(source: HandlerInputSource) =
2424

2525
type 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

3749
let private applyConfiguration configure a =
3850
configure a; a

src/TestConsole/ProgramNestedSubCommands.fs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
open System.IO
44
open FSharp.SystemCommandLine
55
open System.CommandLine.Invocation
6+
open System.CommandLine.Parsing
67

78
module Global =
89
let enableLogging = Input.Option<bool>("--enable-logging", false)
@@ -70,9 +71,20 @@ let ioCmd =
7071

7172
//[<EntryPoint>]
7273
let 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+

0 commit comments

Comments
 (0)