-
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
Support subcommands checking for suggestions #1500
Conversation
Till now, only the root command is looked for suggestions. Now `findSuggestions` works for nested commands as well. For example ``` Error: unknown command "rewin" for "root times" Did you mean this? rewind Run 'root times --help' for usage. ``` Signed-off-by: Yuval Goldberg <yuvigoldi@gmail.com>
/cc @jharshman |
Looks good to me. Would be a nice addition to the #1496 |
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
Till now, only the root command is looked for suggestions. Now `findSuggestions` works for nested commands as well. For example ``` Error: unknown command "rewin" for "root times" Did you mean this? rewind Run 'root times --help' for usage. ``` Signed-off-by: Yuval Goldberg <yuvigoldi@gmail.com>
Now listed in #1496 and merged in #1525. @spf13, maybe you can merge some of the PRs listed in #1496? Or, alternatively, provide merging permissions to some active contributor such as @marckhouzam. |
Till now, only the root command is looked for suggestions. Now `findSuggestions` works for nested commands as well. For example ``` Error: unknown command "rewin" for "root times" Did you mean this? rewind Run 'root times --help' for usage. ``` Signed-off-by: Yuval Goldberg <yuvigoldi@gmail.com>
Till now, only the root command is looked for suggestions. Now `findSuggestions` works for nested commands as well. For example ``` Error: unknown command "rewin" for "root times" Did you mean this? rewind Run 'root times --help' for usage. ``` Signed-off-by: Yuval Goldberg <yuvigoldi@gmail.com>
Till now, only the root command is looked for suggestions. Now `findSuggestions` works for nested commands as well. For example ``` Error: unknown command "rewin" for "root times" Did you mean this? rewind Run 'root times --help' for usage. ``` Signed-off-by: Yuval Goldberg <yuvigoldi@gmail.com>
I am concerned that this change is not backwards-compatible and will break a bunch of programs. Before this PR, commands (except the root command) that don't specify the
Before this PR:
but with this PR:
I believe this is a valid scenario that we will find in the wild. I know that the helm project used to take avantage of this possibility (it no longer does to improve its shell completion usage). For example, using an old version of helm:
Notice how the Test program used in the above example:package main
import (
"fmt"
"github.com/spf13/cobra"
)
var root = &cobra.Command{
Use: "prog",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("prod called with args %v\n", args)
},
}
var sub = &cobra.Command{
Use: "sub",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("sub called with args %v\n", args)
},
}
var subSub = &cobra.Command{
Use: "subSub",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("subSub called with args %v\n", args)
},
}
func main() {
root.AddCommand(sub)
sub.AddCommand(subSub)
root.Execute()
} |
@marckhouzam, I agree that this PR introduces a change that might break a bunch of programs. However, my understanding is that those programs are relaying on "undefined behaviour"; hence, it is not a breaking change from a semver perspective. See https://github.com/spf13/cobra/blob/master/user_guide.md#positional-and-custom-arguments. There is no documented default behaviour if Lines 998 to 1003 in 9e1d6f1
Lines 55 to 57 in 9e1d6f1
In order to assume that defaulting to if c.Args == nil {
return ArbitraryArgs(c, args)
} and update the documentation accordingly. I believe that might be done on top of this PR. On the other hand, as far as I understand, @YuviGold is changing the legacy behaviour. That was apparently inconsistent with regard to the root command and subcommands, and he is making it consistent. It was surprising for me, because I would expect the "legacy" to be kept untouched, then deprecated, last removed; never updated. Moreover, I wonder if this update wouldn't be equivalent to actually removing the usage of I will move this PR to the end of #1525, so we can keep discussing about it while other PRs are merged. |
Till now, only the root command is looked for suggestions. Now `findSuggestions` works for nested commands as well. For example ``` Error: unknown command "rewin" for "root times" Did you mean this? rewind Run 'root times --help' for usage. ``` Signed-off-by: Yuval Goldberg <yuvigoldi@gmail.com>
Till now, only the root command is looked for suggestions. Now `findSuggestions` works for nested commands as well. For example ``` Error: unknown command "rewin" for "root times" Did you mean this? rewind Run 'root times --help' for usage. ``` Signed-off-by: Yuval Goldberg <yuvigoldi@gmail.com>
Thank you @marckhouzam .I honestly didn't think about this use case. I believe that this multi-level suggestion is useful and necessary for many tools.
|
I of course agree that @spf13 and the maintainers have the final word on this. But I think it would be quite understandable if @spf13 did not realize that this was a breaking change. Considering the stated importance of very strong stability for Cobra, my guess is that this change should be avoided.
Thanks!
I agree. Maybe a backwards-compatible approach can be defined? At a minimum an opt-in solution would be backwards-compatible, but there may be something even better; I haven't looked into it myself. |
@marckhouzam, I suggest you or anyone else to propose a PR which adds a test that acknowledges the current de facto behaviour that other tools might be relying on. It seems that BTW, see the updated tests in #841. Note also: "If @YuviGold, I think you, cli/cli and other projects are exploiting an unwanted feature in the codebase. If you want to "support subcommands checking for suggestions" (which is the title of this PR), my understanding is you might achieve it by setting Overall, projects relying on both the "previous" behaviour and on the "after" behaviour are doing it wrong by relying on |
I hadn't considered the breaking change this would introduce. I disagree that changing undocumented behavior is ok. The code implicitly is the documentation. It's hard to say someone has done something wrong when it worked for them. I agree with @marckhouzam
I think we should consider how large the impact this is. If Helm is expecting the current behavior still and others are as well we can take 3 approaches.
|
These tests make sure we don't break backwards-compatibility with respect to the current behaviour of legacyArgs(). See spf13#1500 for the back-story. Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
I agree with Steve - this would be an unexpected breaking change for users. I'm ok putting a "notice" in and upcoming release of breaking changes upcoming in a 2.0.0 cobra release. I'm not a huge fan of letting lingering tech debt exist next to new implementation (a.k.a, the opt in / opt out approach). We've already done this with the shell completions opt in / opt out and it's a bit painful to maintain. |
This PR is being marked as stale due to a long period of inactivity |
These tests make sure we don't break backwards-compatibility with respect to the current behaviour of legacyArgs(). See spf13#1500 for the back-story. Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
These tests make sure we don't break backwards-compatibility with respect to the current behaviour of legacyArgs(). See spf13#1500 for the back-story. Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
These tests make sure we don't break backwards-compatibility with respect to the current behaviour of legacyArgs(). See spf13#1500 for the back-story. Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
These tests make sure we don't break backwards-compatibility with respect to the current behaviour of legacyArgs(). See #1500 for the back-story. Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
Hi @YuviGold - I think this is still valid (and worth discussing). Can you recreate this against the |
Resolves #636
Resolves #834
Resolves alexellis/arkade#525
Till now, only the root command looked for suggestions.
Now
findSuggestions
works for nested commands as well.For example