-
Notifications
You must be signed in to change notification settings - Fork 3k
completion: improve detection for flags that accept multiple values #2210
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -671,6 +671,29 @@ func TestFlagNameCompletionInGoWithDesc(t *testing.T) { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| // customMultiString is a custom Value type that accepts multiple values, | ||||||
| // but does not include "Slice" or "Array" in its "Type" string. | ||||||
| type customMultiString []string | ||||||
|
|
||||||
| var _ SliceValue = (*customMultiString)(nil) | ||||||
|
|
||||||
| func (s *customMultiString) String() string { | ||||||
| return fmt.Sprintf("%v", *s) | ||||||
|
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. What about this?
Suggested change
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. Samr here |
||||||
| } | ||||||
|
|
||||||
| func (s *customMultiString) Set(v string) error { | ||||||
| *s = append(*s, v) | ||||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| func (s *customMultiString) Type() string { | ||||||
| return "multi string" | ||||||
| } | ||||||
|
|
||||||
| func (s *customMultiString) GetSlice() []string { | ||||||
| return *s | ||||||
| } | ||||||
|
|
||||||
| func TestFlagNameCompletionRepeat(t *testing.T) { | ||||||
| rootCmd := &Command{ | ||||||
| Use: "root", | ||||||
|
|
@@ -693,6 +716,8 @@ func TestFlagNameCompletionRepeat(t *testing.T) { | |||||
| sliceFlag := rootCmd.Flags().Lookup("slice") | ||||||
| rootCmd.Flags().BoolSliceP("bslice", "b", nil, "bool slice flag") | ||||||
| bsliceFlag := rootCmd.Flags().Lookup("bslice") | ||||||
| rootCmd.Flags().VarP(&customMultiString{}, "multi", "m", "multi string flag") | ||||||
| multiFlag := rootCmd.Flags().Lookup("multi") | ||||||
|
|
||||||
| // Test that flag names are not repeated unless they are an array or slice | ||||||
| output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--first", "1", "--") | ||||||
|
|
@@ -706,6 +731,7 @@ func TestFlagNameCompletionRepeat(t *testing.T) { | |||||
| "--array", | ||||||
| "--bslice", | ||||||
| "--help", | ||||||
| "--multi", | ||||||
| "--second", | ||||||
| "--slice", | ||||||
| ":4", | ||||||
|
|
@@ -728,6 +754,7 @@ func TestFlagNameCompletionRepeat(t *testing.T) { | |||||
| "--array", | ||||||
| "--bslice", | ||||||
| "--help", | ||||||
| "--multi", | ||||||
| "--slice", | ||||||
| ":4", | ||||||
| "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n") | ||||||
|
|
@@ -737,20 +764,22 @@ func TestFlagNameCompletionRepeat(t *testing.T) { | |||||
| } | ||||||
|
|
||||||
| // Test that flag names are not repeated unless they are an array or slice | ||||||
| output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--slice", "1", "--slice=2", "--array", "val", "--bslice", "true", "--") | ||||||
| output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--slice", "1", "--slice=2", "--array", "val", "--bslice", "true", "--multi", "val", "--") | ||||||
| if err != nil { | ||||||
| t.Errorf("Unexpected error: %v", err) | ||||||
| } | ||||||
| // Reset the flag for the next command | ||||||
| sliceFlag.Changed = false | ||||||
| arrayFlag.Changed = false | ||||||
| bsliceFlag.Changed = false | ||||||
| multiFlag.Changed = false | ||||||
|
|
||||||
| expected = strings.Join([]string{ | ||||||
| "--array", | ||||||
| "--bslice", | ||||||
| "--first", | ||||||
| "--help", | ||||||
| "--multi", | ||||||
| "--second", | ||||||
| "--slice", | ||||||
| ":4", | ||||||
|
|
@@ -768,6 +797,7 @@ func TestFlagNameCompletionRepeat(t *testing.T) { | |||||
| // Reset the flag for the next command | ||||||
| sliceFlag.Changed = false | ||||||
| arrayFlag.Changed = false | ||||||
| multiFlag.Changed = false | ||||||
|
|
||||||
| expected = strings.Join([]string{ | ||||||
| "--array", | ||||||
|
|
@@ -778,6 +808,8 @@ func TestFlagNameCompletionRepeat(t *testing.T) { | |||||
| "-f", | ||||||
| "--help", | ||||||
| "-h", | ||||||
| "--multi", | ||||||
| "-m", | ||||||
| "--second", | ||||||
| "-s", | ||||||
| "--slice", | ||||||
|
|
@@ -797,6 +829,7 @@ func TestFlagNameCompletionRepeat(t *testing.T) { | |||||
| // Reset the flag for the next command | ||||||
| sliceFlag.Changed = false | ||||||
| arrayFlag.Changed = false | ||||||
| multiFlag.Changed = false | ||||||
|
|
||||||
| expected = strings.Join([]string{ | ||||||
| "-a", | ||||||
|
|
||||||
Oops, something went wrong.
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.
Can you do something like this either here or in the tests?
(This is untested pseudocode written on a phone, please check and adapt)
The idea is to validate pflag.SliceValue implements the interface you define
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.
@thaJeztah I'm unsure if you saw my comment last week