-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Add remove group method for command #1997
Open
JunNishimura
wants to merge
9
commits into
spf13:main
Choose a base branch
from
JunNishimura:add_RemoveGroupMethod
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5943edc
add RemoveGroup method for Command
JunNishimura 5bbc677
add unit tests for command.RemoveGroup
JunNishimura 186ab8d
update user_guid
JunNishimura fbdc1d8
Merge branch 'main' into add_RemoveGroupMethod
JunNishimura a2edfa4
update user_guide
JunNishimura 8a27698
remove group id of the help command and completion command
JunNishimura 08be2cb
add unit tests
JunNishimura 02094ac
add error check to RemoveGroup command
JunNishimura 33524eb
fix unit tests
JunNishimura File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1862,6 +1862,61 @@ func TestAddGroup(t *testing.T) { | |
checkStringContains(t, output, "\nTest group\n cmd") | ||
} | ||
|
||
func TestRemoveSingleGroup(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for adding tests! Definitely makes it easier to validate!! |
||
var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun} | ||
|
||
rootCmd.AddGroup( | ||
&Group{ID: "group", Title: "Test group"}, | ||
&Group{ID: "help", Title: "help"}, | ||
&Group{ID: "comp", Title: "comp"}, | ||
) | ||
|
||
rootCmd.AddCommand(&Command{Use: "sub", GroupID: "group", Run: emptyRun}) | ||
|
||
rootCmd.SetHelpCommandGroupID("help") | ||
rootCmd.SetCompletionCommandGroupID("comp") | ||
|
||
rootCmd.RemoveGroup("group") | ||
|
||
output, err := executeCommand(rootCmd, "--help") | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
|
||
checkStringOmits(t, output, "\nTest group:\n sub") | ||
checkStringContains(t, output, "\nAdditional Commands:\n sub") | ||
} | ||
|
||
func TestRemoveMultipleGroups(t *testing.T) { | ||
var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun} | ||
|
||
rootCmd.AddGroup( | ||
&Group{ID: "group1", Title: "Test group1"}, | ||
&Group{ID: "group2", Title: "Test group2"}, | ||
&Group{ID: "help", Title: "help"}, | ||
&Group{ID: "comp", Title: "comp"}, | ||
) | ||
|
||
rootCmd.AddCommand( | ||
&Command{Use: "sub1", Short: "sub1", GroupID: "group1", Run: emptyRun}, | ||
&Command{Use: "sub2", Short: "sub2", GroupID: "group2", Run: emptyRun}, | ||
) | ||
|
||
rootCmd.SetHelpCommandGroupID("help") | ||
rootCmd.SetCompletionCommandGroupID("comp") | ||
|
||
rootCmd.RemoveGroup("group1", "group2") | ||
|
||
output, err := executeCommand(rootCmd, "--help") | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
|
||
checkStringOmits(t, output, "\nTest group1:\n sub1") | ||
checkStringOmits(t, output, "\nTest group2:\n sub2") | ||
checkStringContains(t, output, "\nAdditional Commands:\n sub1 sub1\n sub2 sub2") | ||
} | ||
|
||
func TestWrongGroupFirstLevel(t *testing.T) { | ||
var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
i’m not sure, but I don’t think this will allow removing a group for the help or completion commands. Do we want to support removing groups for those two commands?
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.
Interesting use case. I guess in theory someone might want to remove those from a group that have been added. But they are also sort of the "default" commands you get. What do you think Marc?
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.
In my opinion, I would like cobra to give users as much freedom of development as possible. It is up to the developers to decide whether they want to keep the
help
andcompletion
commands as existing groupies or remove them.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.
I agree and I'm 100% for freedom of use. It more just becomes a matter of not breaking long standing existing features. But this is new functionality anyways. So I'm good with it!
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.
I fixed as follows. If the group id passed as argument match, the group id of the help and completion command is reset in the RemoveGroup method. I would appreciate it if you check the code again.