Skip to content

Commit 2915abc

Browse files
committed
Improve user-friendliness of completion command
When ran directly in the terminal, the command now errors out with: $ gh completion error: the value for `--shell` is required see `gh completion --help` for more information This is to avoid the previously default bash code output confusing the user if they ran the command out of curiousity. A backwards compatibility layer is present here: if stdout is not a terminal, then output bash code like before. This is to support users who have already added a line like this to their bash profile: eval "$(gh completion)"
1 parent 79f749a commit 2915abc

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

command/completion.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
package command
22

33
import (
4+
"errors"
45
"fmt"
6+
"os"
57

68
"github.com/cli/cli/internal/cobrafish"
9+
"github.com/cli/cli/utils"
710
"github.com/spf13/cobra"
811
)
912

1013
func init() {
1114
RootCmd.AddCommand(completionCmd)
12-
completionCmd.Flags().StringP("shell", "s", "bash", "Shell type: {bash|zsh|fish|powershell}")
15+
completionCmd.Flags().StringP("shell", "s", "", "Shell type: {bash|zsh|fish|powershell}")
1316
}
1417

1518
var completionCmd = &cobra.Command{
1619
Use: "completion",
1720
Short: "Generate shell completion scripts",
1821
Long: `Generate shell completion scripts for GitHub CLI commands.
1922
23+
The output of this command will be computer code and is meant to be saved to a
24+
file or immediately evaluated by an interactive shell.
25+
2026
For example, for bash you could add this to your '~/.bash_profile':
2127
22-
eval "$(gh completion)"
28+
eval "$(gh completion -s bash)"
2329
2430
When installing GitHub CLI through a package manager, however, it's possible that
2531
no additional shell configuration is necessary to gain completion support. For
@@ -31,6 +37,19 @@ Homebrew, see <https://docs.brew.sh/Shell-Completion>
3137
return err
3238
}
3339

40+
if shellType == "" {
41+
out := cmd.OutOrStdout()
42+
isTTY := false
43+
if outFile, isFile := out.(*os.File); isFile {
44+
isTTY = utils.IsTerminal(outFile)
45+
}
46+
47+
if isTTY {
48+
return errors.New("error: the value for `--shell` is required\nsee `gh help completion` for more information")
49+
}
50+
shellType = "bash"
51+
}
52+
3453
switch shellType {
3554
case "bash":
3655
return RootCmd.GenBashCompletion(cmd.OutOrStdout())

0 commit comments

Comments
 (0)