Skip to content

Commit

Permalink
Merge pull request #127 from chestercodes/master
Browse files Browse the repository at this point in the history
Added post processing checking of sub command mandatory flag
  • Loading branch information
eiriktsarpalis authored Dec 7, 2020
2 parents 222d975 + 94e4d25 commit 9a5c64f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/Argu/Parsers/Common.fs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,24 @@ let postProcessResults (argInfo : UnionArgInfo) (ignoreMissingMandatory : bool)
| Choice1Of2 ts, ts' when caseInfo.GatherAllSources.Value -> Array.append ts ts'
| _, ts' -> ts'

let rec searchCaseInfoForError caseInfo =
match caseInfo.ParameterInfo.Value with
| SubCommand (_, unionArg, __) ->
match unionArg.Cases.Value with
| [| case |] ->
if case.IsMandatory.Value && not ignoreMissingMandatory then
Some (error unionArg ErrorCode.PostProcess "missing parameter '%s'." case.Name.Value)
else
searchCaseInfoForError case
| _ -> None
| _ -> None

match combined with
| [| sub |] ->
match searchCaseInfoForError sub.CaseInfo with
| Some error -> error
| _ -> combined

| [||] when caseInfo.IsMandatory.Value && not ignoreMissingMandatory ->
error argInfo ErrorCode.PostProcess "missing parameter '%s'." caseInfo.Name.Value
| _ -> combined
Expand Down
42 changes: 41 additions & 1 deletion tests/Argu.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ module ``Argu Tests Main List`` =
| Force -> "force changes in remote repo"
| Remote _ -> "push changes to remote repository and branch"

type NewArgs =
| [<Mandatory>] Name of string
with
interface IArgParserTemplate with
member this.Usage =
match this with
| Name _ -> "New name"

type TagArgs =
| New of ParseResults<NewArgs>
with
interface IArgParserTemplate with
member this.Usage =
match this with
| New _ -> "New tag"

type CheckoutArgs =
| [<Mandatory>] Branch of string
with
interface IArgParserTemplate with
member this.Usage =
match this with
| Branch _ -> "push changes to remote repository and branch"

[<CliPrefix(CliPrefix.Dash)>]
type CleanArgs =
| D
Expand Down Expand Up @@ -94,6 +118,8 @@ module ``Argu Tests Main List`` =
| [<CliPrefix(CliPrefix.Dash)>] B
| [<CliPrefix(CliPrefix.Dash)>] C
| [<CliPrefix(CliPrefix.None)>] Push of ParseResults<PushArgs>
| [<CliPrefix(CliPrefix.None)>] Checkout of ParseResults<CheckoutArgs>
| [<CliPrefix(CliPrefix.None)>] Tag of ParseResults<TagArgs>
| [<CliPrefix(CliPrefix.None)>] Clean of ParseResults<CleanArgs>
| [<CliPrefix(CliPrefix.None)>] Required of ParseResults<RequiredSubcommand>
| [<CliPrefix(CliPrefix.None)>] Unrecognized of ParseResults<GatherUnrecognizedSubcommand>
Expand Down Expand Up @@ -127,6 +153,8 @@ module ``Argu Tests Main List`` =
| First_Parameter _ -> "parameter that has to appear at beginning of command line args."
| Last_Parameter _ -> "parameter that has to appear at end of command line args."
| Push _ -> "push changes"
| Checkout _ -> "checkout ref"
| Tag _ -> "tag"
| Clean _ -> "clean state"
| Required _ -> "required subcommand"
| Unrecognized _ -> "unrecognized subcommand"
Expand Down Expand Up @@ -449,6 +477,18 @@ module ``Argu Tests Main List`` =
raisesWith<ArguParseException> <@ parser.ParseCommandLine args @>
(fun e -> <@ e.FirstLine.Contains "must be followed by <branch name>" @>)

[<Fact>]
let ``Main command parsing should fail on missing mandatory sub command parameter`` () =
let args = [|"--mandatory-arg" ; "true" ; "checkout" |]
raisesWith<ArguParseException> <@ parser.ParseCommandLine args @>
(fun e -> <@ e.FirstLine.Contains "--branch" @>)

[<Fact>]
let ``Main command parsing should fail on missing mandatory sub command's sub command parameter`` () =
let args = [|"--mandatory-arg"; "true"; "tag"; "--new"; |]
raisesWith<ArguParseException> <@ parser.ParseCommandLine args @>
(fun e -> <@ e.FirstLine.Contains "--name" @>)

[<Fact>]
let ``Main command parsing should allow trailing arguments`` () =
let args = [|"push" ; "origin" ; "master" ; "-f" |]
Expand Down Expand Up @@ -616,7 +656,7 @@ module ``Argu Tests Main List`` =
[<Fact>]
let ``Get all subcommand parsers`` () =
let subcommands = parser.GetSubCommandParsers()
test <@ subcommands.Length = 4 @>
test <@ subcommands.Length = 6 @>
test <@ subcommands |> List.forall (fun sc -> sc.IsSubCommandParser) @>

[<Fact>]
Expand Down

0 comments on commit 9a5c64f

Please sign in to comment.