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.

Also, we disable all further completions if the 'help' or 'version'
flags are present on the command-line.

Merge spf13/cobra#1813
  • Loading branch information
marckhouzam authored and hoshsadiq committed Jan 3, 2023
1 parent 34d3bfd commit bafccb0
Show file tree
Hide file tree
Showing 3 changed files with 258 additions and 7 deletions.
8 changes: 6 additions & 2 deletions command.go
Expand Up @@ -32,6 +32,8 @@ import (
"github.com/zulucmd/zulu/internal/util"
)

const FlagSetByCobraAnnotation = "cobra_annotation_flag_set_by_cobra"

//go:embed templates/*
var tmplFS embed.FS

Expand Down Expand Up @@ -1208,7 +1210,7 @@ func (c *Command) InitDefaultHelpFlag() {
} else {
usage += c.Name()
}
c.Flags().Bool("help", false, usage, zflag.OptShorthand('h'))
c.Flags().Bool("help", false, usage, zflag.OptShorthand('h'), zflag.OptAnnotation(FlagSetByCobraAnnotation, []string{"true"}))
}
}

Expand All @@ -1230,7 +1232,9 @@ func (c *Command) InitDefaultVersionFlag() {
usage += c.Name()
}

var opts []zflag.Opt
opts := []zflag.Opt{
zflag.OptAnnotation(FlagSetByCobraAnnotation, []string{"true"}),
}
if c.Flags().ShorthandLookup('v') == nil {
opts = append(opts, zflag.OptShorthand('v'))
}
Expand Down
24 changes: 24 additions & 0 deletions completions.go
Expand Up @@ -250,6 +250,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 Expand Up @@ -282,6 +288,12 @@ func (c *Command) getCompletions(args []string) (*Command, []string, ShellCompDi
}
}

// Look for the --help or --version flags. If they are present,
// there should be no further completions.
if helpOrVersionFlagPresent(finalCmd) {
return finalCmd, []string{}, ShellCompDirectiveNoFileComp, nil
}

// We only remove the flags from the arguments if DisableFlagParsing is not set.
// This is important for commands which have requested to do their own flag completion.
if !finalCmd.DisableFlagParsing {
Expand Down Expand Up @@ -451,6 +463,18 @@ func (c *Command) getCompletions(args []string) (*Command, []string, ShellCompDi
return finalCmd, completions, directive, nil
}

func helpOrVersionFlagPresent(cmd *Command) bool {
if versionFlag := cmd.Flags().Lookup("version"); versionFlag != nil &&
len(versionFlag.Annotations[FlagSetByCobraAnnotation]) > 0 && versionFlag.Changed {
return true
}
if helpFlag := cmd.Flags().Lookup("help"); helpFlag != nil &&
len(helpFlag.Annotations[FlagSetByCobraAnnotation]) > 0 && helpFlag.Changed {
return true
}
return false
}

func getFlagNameCompletions(flag *zflag.Flag, toComplete string) []string {
if nonCompletableFlag(flag) {
return []string{}
Expand Down

0 comments on commit bafccb0

Please sign in to comment.