args_test refactorization after generalizing ValidArgs#841
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
d2d4015 to
2e3862d
Compare
3149d7c to
9f4a04e
Compare
|
ping |
|
A PR was created with the first three commits from this one: #1426. Then, this PR was cleaned up. Hope that helps move forward... |
|
This PR is being marked as stale due to a long period of inactivity |
|
Not stale, but ready to be merged. |
|
Could/should this change take into account ValidArgsFunction? |
Please see #1298 |
|
@tmc, since ValidArgs and ValidArgsFunction are mutually exclusive, I don't see how to change this to take the latter into account. If you use ValidArgsFunction, it should be handled by the completion logic, as far as I understand. Maybe you can provide a reproducer of what you are trying to achieve? |
|
Please, remove the stale label from this PR. Refs: |
|
Can we change the milestone of this PR to 1.5.0? |
|
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. |
|
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. |
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 ofValidArgsfromargs.gotocommand.go. As a result:ValidArgs.ValidArgsis checked first, and then the defined validator.OnlyValidArgsandExactValidArgsare deprecated.args_test.gois updated accordingly: