Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added post processing checking of sub command mandatory flag #127

Merged
merged 5 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -36,6 +36,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 @@ -90,6 +114,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 @@ -120,6 +146,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 @@ -426,6 +454,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 @@ -593,7 +633,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