-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
args_test refactorization after generalizing ValidArgs #841
base: main
Are you sure you want to change the base?
Conversation
f872d88
to
3a72951
Compare
IMO this PR overreaches a little over what was originally intended. Changing to Go modules is great but not required for this feature. I think there is a case for improvement here but perhaps it can be approached differently. Issue #838 mentions that there is not currently a method of combining these validation functions. A fix for this could to introduce a method of chaining them. |
3a72951
to
9e2b422
Compare
This PR was based on #840 because I expected that branch to be merged before this, so it could be fast-forwarded. #840 provides some interesting features, such as fixing CI, which would help evaluate this PR. Anyway, I have now made this PR independent from #840.
I think that it does not make sense to combine any of NoArgs, ArbitraryArgs, MinimumNArgs, MaximumNArgs, ExactArgs, RangeArgs, because all of them are mutually exclusive. Instead, my proposal is to apply ValidArgs (when defined) to the selected validator. That's why OnlyValidArgs and ExactValidArgs are deprecated. Moreover, if a user wants to combine any of the provided validators and extend it with custom logic, an example can be added. |
Thanks for making this independent of #840 . Maybe some clarification on the goal here was needed because it seems to have strayed away from the original issue you referenced. That issue proposed the addition of validation functions which were combinations of what is currently available. Hence why I suggested adding a method to allow the chaining of validation functions. However, I think I see where you're coming from. You want to remove However, this check inside the validation function contains the same logic as |
@jharshman, I had the same point of view as you when I started to implement this. As I went forward and I saw the codebase, I concluded that the proposed solution is the best. Some hints to better understand the changes:
The issue requested some method to combine e.g. MinimumNArgs(1) with OnlyValidArgs. That's possible with the current proposal: Args: cobra.MinimumNArgs(1),
ValidArgs: []string{"one", "two", "three"}, compared to the prototype suggested in #838: Args: cobra.MatchValid(cobra.MinimumNArgs(1)),
ValidArgs: []string{"one", "two", "three"}, Moreover, in #838 it was suggested that it would be better not to require the user to type
All of the original validators care about the number of arguments that a command accepts: NoArgs, ArbitraryArgs, MinimumNArgs(int), MaximumNArgs(int), ExactArgs(int), RangeArgs(min, max). We can consider it an enumeration of 6 items. When Nonetheless, the implementation was partial and a single one was introduced. Later, a second one was added. Still, there are 3 combinations missing, which is what was pointed out in #838 at first. Hence, the design decission is to provide one explicit validator for each combination of the multiple criterias to filter the arguments (original approach), or to enforce the orthogonality in the code (proposed approach).
|
9e2b422
to
d7c0e17
Compare
@umarcor thanks for taking the time to detail your approach and solution. Looks like one of the tests doesn't like the
|
This is why I originally based this PR on top of #840. Those CI issues are fixed there, as commented above. The commit that fixes it is precisely: bbec970. Nonetheless, I think that it would be better to use |
cf18056
to
178cb83
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
d2d4015
to
2e3862d
Compare
3149d7c
to
9f4a04e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fine to me. @eparis
ping |
52ab428
to
ed7f960
Compare
Can we change the milestone of this PR to 1.5.0? |
7f742d0
to
a34ecc9
Compare
This has an approval and green tests; is it waiting for anything else? I know this PR is connected to a lot of issues and if everyones happy I'm just curious what the hold is right now? [FWIW I haven't reviewed this, just cleaning up the backlog and kept finding links to this PR] |
@johnSchnake this is only waiting for some maintainer to decide merging it. It was approved by a maintainer several years ago, and then reviewed by a user who is a maintainer now. It was not merged since then because none of the currently active maintainers had time to sit down, read it, and understand what's it about. That's why I split #1426 in november, and I now split #1643. Please, review #1643, since that's the commit that modifies the functionality. This PR is now just style/refactorization based on #1643. BTW, you might want to merge #1612, which is a low-hanging fruit after #1547 was merged last week. |
a34ecc9
to
d464cba
Compare
d464cba
to
a9dbe74
Compare
This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size. |
5 of the commits in this PR are about refactoring args_test.go. The sixth one (feat: generalize ValidArgs; use it implicitly with any validator) moves the validation of ValidArgs from args.go to command.go. As a result: - Any validator can be used along with ValidArgs. ValidArgs is checked first, and then the defined validator. - OnlyValidArgs and ExactValidArgs are deprecated. args_test.go is updated accordingly: ``` === RUN TestNoArgs --- PASS: TestNoArgs (0.00s) === RUN TestNoArgsWithArgs --- PASS: TestNoArgsWithArgs (0.00s) === RUN TestNoArgsWithArgsWithValid --- PASS: TestNoArgsWithArgsWithValid (0.00s) === RUN TestArbitraryArgs --- PASS: TestArbitraryArgs (0.00s) === RUN TestArbitraryArgsWithValid --- PASS: TestArbitraryArgsWithValid (0.00s) === RUN TestArbitraryArgsWithValidWithInvalidArgs --- PASS: TestArbitraryArgsWithValidWithInvalidArgs (0.00s) === RUN TestMinimumNArgs --- PASS: TestMinimumNArgs (0.00s) === RUN TestMinimumNArgsWithValid --- PASS: TestMinimumNArgsWithValid (0.00s) === RUN TestMinimumNArgsWithValidWithInvalidArgs --- PASS: TestMinimumNArgsWithValidWithInvalidArgs (0.00s) === RUN TestMinimumNArgsWithLessArgs --- PASS: TestMinimumNArgsWithLessArgs (0.00s) === RUN TestMinimumNArgsWithLessArgsWithValid --- PASS: TestMinimumNArgsWithLessArgsWithValid (0.00s) === RUN TestMinimumNArgsWithLessArgsWithValidWithInvalidArgs --- PASS: TestMinimumNArgsWithLessArgsWithValidWithInvalidArgs (0.00s) === RUN TestMaximumNArgs --- PASS: TestMaximumNArgs (0.00s) === RUN TestMaximumNArgsWithValid --- PASS: TestMaximumNArgsWithValid (0.00s) === RUN TestMaximumNArgsWithValidWithInvalidArgs --- PASS: TestMaximumNArgsWithValidWithInvalidArgs (0.00s) === RUN TestMaximumNArgsWithMoreArgs --- PASS: TestMaximumNArgsWithMoreArgs (0.00s) === RUN TestMaximumNArgsWithMoreArgsWithValid --- PASS: TestMaximumNArgsWithMoreArgsWithValid (0.00s) === RUN TestMaximumNArgsWithMoreArgsWithValidWithInvalidArgs --- PASS: TestMaximumNArgsWithMoreArgsWithValidWithInvalidArgs (0.00s) === RUN TestExactArgs --- PASS: TestExactArgs (0.00s) === RUN TestExactArgsWithValid --- PASS: TestExactArgsWithValid (0.00s) === RUN TestExactArgsWithValidWithInvalidArgs --- PASS: TestExactArgsWithValidWithInvalidArgs (0.00s) === RUN TestExactArgsWithInvalidCount --- PASS: TestExactArgsWithInvalidCount (0.00s) === RUN TestExactArgsWithInvalidCountWithValid --- PASS: TestExactArgsWithInvalidCountWithValid (0.00s) === RUN TestExactArgsWithInvalidCountWithValidWithInvalidArgs --- PASS: TestExactArgsWithInvalidCountWithValidWithInvalidArgs (0.00s) === RUN TestRangeArgs --- PASS: TestRangeArgs (0.00s) === RUN TestRangeArgsWithValid --- PASS: TestRangeArgsWithValid (0.00s) === RUN TestRangeArgsWithValidWithInvalidArgs --- PASS: TestRangeArgsWithValidWithInvalidArgs (0.00s) === RUN TestRangeArgsWithInvalidCount --- PASS: TestRangeArgsWithInvalidCount (0.00s) === RUN TestRangeArgsWithInvalidCountWithValid --- PASS: TestRangeArgsWithInvalidCountWithValid (0.00s) === RUN TestRangeArgsWithInvalidCountWithValidWithInvalidArgs --- PASS: TestRangeArgsWithInvalidCountWithValidWithInvalidArgs (0.00s) === RUN TestRootTakesNoArgs --- PASS: TestRootTakesNoArgs (0.00s) === RUN TestRootTakesArgs --- PASS: TestRootTakesArgs (0.00s) === RUN TestChildTakesNoArgs --- PASS: TestChildTakesNoArgs (0.00s) === RUN TestChildTakesArgs --- PASS: TestChildTakesArgs (0.00s) ``` Merge spf13/cobra#841 Fix #838 and Fix #745
Fix #838 and Fix #745.
5 of the commits in this PR are about refactoring
args_test.go
. The sixth one (feat: generalize ValidArgs; use it implicitly with any validator
) moves the validation ofValidArgs
fromargs.go
tocommand.go
. As a result:ValidArgs
.ValidArgs
is checked first, and then the defined validator.OnlyValidArgs
andExactValidArgs
are deprecated.args_test.go
is updated accordingly: