Skip to content

Commit

Permalink
implement ParseResult.TryGetSubCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriktsarpalis committed Jun 29, 2016
1 parent f1a37dd commit 87a3162
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/Argu/ParseResult.fs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ type ParseResult<'Template when 'Template :> IArgParserTemplate>
member r.IterResult (expr : Expr<'Field -> 'Template>, iterator : 'Field -> unit, ?source) : unit =
expr |> tryGetResult source |> Option.iter (parseResult iterator)

/// <summary>
/// Attempts to recover the subcommand parameter from the results,
/// if once has been specified.
/// </summary>
member r.TryGetSubCommand() : 'Template option =
results.Cases
|> Seq.concat
|> Seq.tryPick(fun c ->
if c.ArgInfo.Type = ArgumentType.SubCommand then
Some(c.Value :?> 'Template)
else None)

override r.ToString() = sprintf "%A" (r.GetAllResults())

// used by StructuredFormatDisplay attribute
Expand Down
2 changes: 1 addition & 1 deletion tests/Argu.Tests/Argu.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
<ItemGroup>
<Reference Include="FSharp.Core">
<HintPath>..\..\packages\FSharp.Core\lib\net40\FSharp.Core.dll</HintPath>
<Private>True</Private>
<Private>False</Private>
<Paket>True</Paket>
</Reference>
</ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions tests/Argu.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,15 @@ module ``Argu Tests`` =
let args = [|"push"; "--remote" ; "origin" ; "--branch" ; "master"|]
let results = parser.ParseCommandLine(args, ignoreMissing = true)
let nested = results.GetResult <@ Push @>
test <@ match results.TryGetSubCommand() with Some (Push _) -> true | _ -> false @>
test <@ nested.GetAllResults() = [Remote "origin" ; Branch "master"] @>

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

[<Fact>]
Expand Down

0 comments on commit 87a3162

Please sign in to comment.