Skip to content

Commit 87a3162

Browse files
implement ParseResult.TryGetSubCommand
1 parent f1a37dd commit 87a3162

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/Argu/ParseResult.fs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,18 @@ type ParseResult<'Template when 'Template :> IArgParserTemplate>
194194
member r.IterResult (expr : Expr<'Field -> 'Template>, iterator : 'Field -> unit, ?source) : unit =
195195
expr |> tryGetResult source |> Option.iter (parseResult iterator)
196196

197+
/// <summary>
198+
/// Attempts to recover the subcommand parameter from the results,
199+
/// if once has been specified.
200+
/// </summary>
201+
member r.TryGetSubCommand() : 'Template option =
202+
results.Cases
203+
|> Seq.concat
204+
|> Seq.tryPick(fun c ->
205+
if c.ArgInfo.Type = ArgumentType.SubCommand then
206+
Some(c.Value :?> 'Template)
207+
else None)
208+
197209
override r.ToString() = sprintf "%A" (r.GetAllResults())
198210

199211
// used by StructuredFormatDisplay attribute

tests/Argu.Tests/Argu.Tests.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
<ItemGroup>
187187
<Reference Include="FSharp.Core">
188188
<HintPath>..\..\packages\FSharp.Core\lib\net40\FSharp.Core.dll</HintPath>
189-
<Private>True</Private>
189+
<Private>False</Private>
190190
<Paket>True</Paket>
191191
</Reference>
192192
</ItemGroup>

tests/Argu.Tests/Tests.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,15 @@ module ``Argu Tests`` =
229229
let args = [|"push"; "--remote" ; "origin" ; "--branch" ; "master"|]
230230
let results = parser.ParseCommandLine(args, ignoreMissing = true)
231231
let nested = results.GetResult <@ Push @>
232+
test <@ match results.TryGetSubCommand() with Some (Push _) -> true | _ -> false @>
232233
test <@ nested.GetAllResults() = [Remote "origin" ; Branch "master"] @>
233234

234235
[<Fact>]
235236
let ``Simple subcommand parsing 2`` () =
236237
let args = [|"clean"; "-fdx"|]
237238
let results = parser.ParseCommandLine(args, ignoreMissing = true)
238239
let nested = results.GetResult <@ Clean @>
240+
test <@ match results.TryGetSubCommand() with Some (Clean _) -> true | _ -> false @>
239241
test <@ nested.GetAllResults() = [F; D; X] @>
240242

241243
[<Fact>]

0 commit comments

Comments
 (0)