Skip to content

Commit 9a5c64f

Browse files
Merge pull request #127 from chestercodes/master
Added post processing checking of sub command mandatory flag
2 parents 222d975 + 94e4d25 commit 9a5c64f

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/Argu/Parsers/Common.fs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,24 @@ let postProcessResults (argInfo : UnionArgInfo) (ignoreMissingMandatory : bool)
6666
| Choice1Of2 ts, ts' when caseInfo.GatherAllSources.Value -> Array.append ts ts'
6767
| _, ts' -> ts'
6868

69+
let rec searchCaseInfoForError caseInfo =
70+
match caseInfo.ParameterInfo.Value with
71+
| SubCommand (_, unionArg, __) ->
72+
match unionArg.Cases.Value with
73+
| [| case |] ->
74+
if case.IsMandatory.Value && not ignoreMissingMandatory then
75+
Some (error unionArg ErrorCode.PostProcess "missing parameter '%s'." case.Name.Value)
76+
else
77+
searchCaseInfoForError case
78+
| _ -> None
79+
| _ -> None
80+
6981
match combined with
82+
| [| sub |] ->
83+
match searchCaseInfoForError sub.CaseInfo with
84+
| Some error -> error
85+
| _ -> combined
86+
7087
| [||] when caseInfo.IsMandatory.Value && not ignoreMissingMandatory ->
7188
error argInfo ErrorCode.PostProcess "missing parameter '%s'." caseInfo.Name.Value
7289
| _ -> combined

tests/Argu.Tests/Tests.fs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,30 @@ module ``Argu Tests Main List`` =
3737
| Force -> "force changes in remote repo"
3838
| Remote _ -> "push changes to remote repository and branch"
3939

40+
type NewArgs =
41+
| [<Mandatory>] Name of string
42+
with
43+
interface IArgParserTemplate with
44+
member this.Usage =
45+
match this with
46+
| Name _ -> "New name"
47+
48+
type TagArgs =
49+
| New of ParseResults<NewArgs>
50+
with
51+
interface IArgParserTemplate with
52+
member this.Usage =
53+
match this with
54+
| New _ -> "New tag"
55+
56+
type CheckoutArgs =
57+
| [<Mandatory>] Branch of string
58+
with
59+
interface IArgParserTemplate with
60+
member this.Usage =
61+
match this with
62+
| Branch _ -> "push changes to remote repository and branch"
63+
4064
[<CliPrefix(CliPrefix.Dash)>]
4165
type CleanArgs =
4266
| D
@@ -94,6 +118,8 @@ module ``Argu Tests Main List`` =
94118
| [<CliPrefix(CliPrefix.Dash)>] B
95119
| [<CliPrefix(CliPrefix.Dash)>] C
96120
| [<CliPrefix(CliPrefix.None)>] Push of ParseResults<PushArgs>
121+
| [<CliPrefix(CliPrefix.None)>] Checkout of ParseResults<CheckoutArgs>
122+
| [<CliPrefix(CliPrefix.None)>] Tag of ParseResults<TagArgs>
97123
| [<CliPrefix(CliPrefix.None)>] Clean of ParseResults<CleanArgs>
98124
| [<CliPrefix(CliPrefix.None)>] Required of ParseResults<RequiredSubcommand>
99125
| [<CliPrefix(CliPrefix.None)>] Unrecognized of ParseResults<GatherUnrecognizedSubcommand>
@@ -127,6 +153,8 @@ module ``Argu Tests Main List`` =
127153
| First_Parameter _ -> "parameter that has to appear at beginning of command line args."
128154
| Last_Parameter _ -> "parameter that has to appear at end of command line args."
129155
| Push _ -> "push changes"
156+
| Checkout _ -> "checkout ref"
157+
| Tag _ -> "tag"
130158
| Clean _ -> "clean state"
131159
| Required _ -> "required subcommand"
132160
| Unrecognized _ -> "unrecognized subcommand"
@@ -449,6 +477,18 @@ module ``Argu Tests Main List`` =
449477
raisesWith<ArguParseException> <@ parser.ParseCommandLine args @>
450478
(fun e -> <@ e.FirstLine.Contains "must be followed by <branch name>" @>)
451479

480+
[<Fact>]
481+
let ``Main command parsing should fail on missing mandatory sub command parameter`` () =
482+
let args = [|"--mandatory-arg" ; "true" ; "checkout" |]
483+
raisesWith<ArguParseException> <@ parser.ParseCommandLine args @>
484+
(fun e -> <@ e.FirstLine.Contains "--branch" @>)
485+
486+
[<Fact>]
487+
let ``Main command parsing should fail on missing mandatory sub command's sub command parameter`` () =
488+
let args = [|"--mandatory-arg"; "true"; "tag"; "--new"; |]
489+
raisesWith<ArguParseException> <@ parser.ParseCommandLine args @>
490+
(fun e -> <@ e.FirstLine.Contains "--name" @>)
491+
452492
[<Fact>]
453493
let ``Main command parsing should allow trailing arguments`` () =
454494
let args = [|"push" ; "origin" ; "master" ; "-f" |]
@@ -616,7 +656,7 @@ module ``Argu Tests Main List`` =
616656
[<Fact>]
617657
let ``Get all subcommand parsers`` () =
618658
let subcommands = parser.GetSubCommandParsers()
619-
test <@ subcommands.Length = 4 @>
659+
test <@ subcommands.Length = 6 @>
620660
test <@ subcommands |> List.forall (fun sc -> sc.IsSubCommandParser) @>
621661

622662
[<Fact>]

0 commit comments

Comments
 (0)