commands/run.go: hide descriptions in completions

In commit 15723008062b941b53743295784554b2f3ba875e of PR #5311 the
"git lfs completion" command was introduced, utilizing the support
provided by the spf13/cobra package to generate tab-completion scripts
for a number of shells, including the Bash, fish, and Zsh shells.
These scripts make use of "hidden"  __complete and __completeNoDesc
commands, also implemented by the spf13/cobra package, which the
shell completion functions may query to retrieve dynamic lists of
command names and flags from the Git LFS client.

At present, the __complete command is used, which also returns any
short help text defined each command.  This additional descriptive
text is then presented to the user if they are running a shell like
Zsh whose completion system supports the display of such hints.

However, as we only define short help text for a single Git LFS
command, namely the "git lfs help" command, the display of this
one text string causes the columnar display of available command
names to be prefaced with a single "help" line when the user
types "git lfs [Tab]":

  help             -- Help about any command
  checkout         fsck             post-checkout    status
  clean            install          post-commit      track
  ...              ...              ...              ...

This irregularity makes the display output less helpful and more
difficult to parse than if we simply suppress the inclusion of the
per-command descriptions entirely, so we do so by setting the
appropriate flags or using a different script generation method
of the spf13/cobra package.

Note that we then also need to update the name of the __complete
command to __completeNoDesc in the search-and-replace operation we
perform on the script generated for the Zsh shell.

We can always revisit this choice in the future should we choose
to add short help text to all our command definitions.  This would
require refactoring our NewCommand() and RegisterCommand() functions
to accept the per-command text strings as extra parameters.
This commit is contained in:
Chris Darroch 2023-07-17 13:38:29 -07:00
parent f32a828ae8
commit 1bb3a04a0c

@ -72,7 +72,7 @@ func Run() int {
switch args[0] {
case "bash":
completion := new(bytes.Buffer)
cmd.Root().GenBashCompletionV2(completion, true)
cmd.Root().GenBashCompletionV2(completion, false)
// this is needed for git bash completion to pick up the completion for the subcommand
completionSource := []byte(` local out directive
@ -91,14 +91,14 @@ func Run() int {
newCompletion.WriteTo(os.Stdout)
case "fish":
cmd.Root().GenFishCompletion(os.Stdout, true)
cmd.Root().GenFishCompletion(os.Stdout, false)
case "zsh":
completion := new(bytes.Buffer)
cmd.Root().GenZshCompletion(completion)
cmd.Root().GenZshCompletionNoDesc(completion)
// this is needed for git zsh completion to use the right command for completion
completionSource := []byte(` requestComp="${words[1]} __complete ${words[2,-1]}"`)
completionReplace := []byte(` requestComp="git-${words[1]#*git-} __complete ${words[2,-1]}"`)
completionSource := []byte(` requestComp="${words[1]} __completeNoDesc ${words[2,-1]}"`)
completionReplace := []byte(` requestComp="git-${words[1]#*git-} __completeNoDesc ${words[2,-1]}"`)
newCompletion := bytes.NewBuffer(bytes.Replace(completion.Bytes(), completionSource, completionReplace, 1))
newCompletion.WriteTo(os.Stdout)