Skip to content

Commit

Permalink
Include --help and --version flag in completion
Browse files Browse the repository at this point in the history
Fixes #1786

The --help, -h, --version and -v flags are normally added when the
`execute()` function is called on a command.  When doing completion
we don't call `execute()` so we need to add these flags explicitly to
the command being completed.

Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
  • Loading branch information
marckhouzam committed Sep 20, 2022
1 parent a281c8b commit bfa0766
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
6 changes: 6 additions & 0 deletions completions.go
Expand Up @@ -274,6 +274,12 @@ func (c *Command) getCompletions(args []string) (*Command, []string, ShellCompDi
}
finalCmd.ctx = c.ctx

// These flags are normally added when `execute()` is called on `finalCmd`,
// however, when doing completion, we don't call `finalCmd.execute()`.
// Let's add the --help and --version flag ourselves.
finalCmd.InitDefaultHelpFlag()
finalCmd.InitDefaultVersionFlag()

// Check if we are doing flag value completion before parsing the flags.
// This is important because if we are completing a flag value, we need to also
// remove the flag name argument from the list of finalArgs or else the parsing
Expand Down
42 changes: 37 additions & 5 deletions completions_test.go
Expand Up @@ -493,8 +493,9 @@ func TestFlagNameCompletionInGo(t *testing.T) {
Run: emptyRun,
}
childCmd := &Command{
Use: "childCmd",
Run: emptyRun,
Use: "childCmd",
Version: "1.2.3",
Run: emptyRun,
}
rootCmd.AddCommand(childCmd)

Expand Down Expand Up @@ -528,6 +529,8 @@ func TestFlagNameCompletionInGo(t *testing.T) {
expected = strings.Join([]string{
"--first",
"-f",
"--help",
"-h",
"--second",
"-s",
":4",
Expand Down Expand Up @@ -561,7 +564,11 @@ func TestFlagNameCompletionInGo(t *testing.T) {
expected = strings.Join([]string{
"--second",
"-s",
"--help",
"-h",
"--subFlag",
"--version",
"-v",
":4",
"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")

Expand All @@ -576,9 +583,10 @@ func TestFlagNameCompletionInGoWithDesc(t *testing.T) {
Run: emptyRun,
}
childCmd := &Command{
Use: "childCmd",
Short: "first command",
Run: emptyRun,
Use: "childCmd",
Short: "first command",
Version: "1.2.3",
Run: emptyRun,
}
rootCmd.AddCommand(childCmd)

Expand Down Expand Up @@ -612,6 +620,8 @@ func TestFlagNameCompletionInGoWithDesc(t *testing.T) {
expected = strings.Join([]string{
"--first\tfirst flag",
"-f\tfirst flag",
"--help\thelp for root",
"-h\thelp for root",
"--second\tsecond flag",
"-s\tsecond flag",
":4",
Expand Down Expand Up @@ -645,7 +655,11 @@ func TestFlagNameCompletionInGoWithDesc(t *testing.T) {
expected = strings.Join([]string{
"--second\tsecond flag",
"-s\tsecond flag",
"--help\thelp for childCmd",
"-h\thelp for childCmd",
"--subFlag\tsub flag",
"--version\tversion for childCmd",
"-v\tversion for childCmd",
":4",
"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")

Expand Down Expand Up @@ -688,6 +702,7 @@ func TestFlagNameCompletionRepeat(t *testing.T) {
expected := strings.Join([]string{
"--array",
"--bslice",
"--help",
"--second",
"--slice",
":4",
Expand All @@ -709,6 +724,7 @@ func TestFlagNameCompletionRepeat(t *testing.T) {
expected = strings.Join([]string{
"--array",
"--bslice",
"--help",
"--slice",
":4",
"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
Expand All @@ -731,6 +747,7 @@ func TestFlagNameCompletionRepeat(t *testing.T) {
"--array",
"--bslice",
"--first",
"--help",
"--second",
"--slice",
":4",
Expand All @@ -756,6 +773,8 @@ func TestFlagNameCompletionRepeat(t *testing.T) {
"-b",
"--first",
"-f",
"--help",
"-h",
"--second",
"-s",
"--slice",
Expand Down Expand Up @@ -1792,6 +1811,7 @@ func TestFlagCompletionWithNotInterspersedArgs(t *testing.T) {

expected := strings.Join([]string{
"--bool\ttest bool flag",
"--help\thelp for child",
"--string\ttest string flag",
":4",
"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
Expand Down Expand Up @@ -2602,6 +2622,8 @@ func TestCompleteWithDisableFlagParsing(t *testing.T) {
expected := strings.Join([]string{
"--persistent",
"-p",
"--help",
"-h",
"--nonPersistent",
"-n",
"--flag",
Expand All @@ -2624,6 +2646,8 @@ func TestCompleteWithDisableFlagParsing(t *testing.T) {
expected = strings.Join([]string{
"--persistent",
"-p",
"--help",
"-h",
"--nonPersistent",
"-n",
":4",
Expand Down Expand Up @@ -2753,6 +2777,8 @@ func TestCompletionForGroupedFlags(t *testing.T) {
expectedOutput: strings.Join([]string{
"--ingroup1",
"--ingroup2",
"--help",
"-h",
"--ingroup3",
"--nogroup",
":4",
Expand Down Expand Up @@ -2851,6 +2877,8 @@ func TestCompletionForMutuallyExclusiveFlags(t *testing.T) {
expectedOutput: strings.Join([]string{
"--ingroup1",
"--ingroup2",
"--help",
"-h",
"--ingroup3",
"--nogroup",
":4",
Expand All @@ -2861,6 +2889,8 @@ func TestCompletionForMutuallyExclusiveFlags(t *testing.T) {
args: []string{"child", "--ingroup1", "8", "-"},
expectedOutput: strings.Join([]string{
"--ingroup1", // Should be suggested again since it is a slice
"--help",
"-h",
"--nogroup",
":4",
"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n"),
Expand All @@ -2869,6 +2899,8 @@ func TestCompletionForMutuallyExclusiveFlags(t *testing.T) {
desc: "group ignored if some flags not applicable",
args: []string{"--ingroup1", "8", "-"},
expectedOutput: strings.Join([]string{
"--help",
"-h",
"--ingroup1",
"--ingroup2",
":4",
Expand Down

0 comments on commit bfa0766

Please sign in to comment.