11package command
22
33import (
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
1013func 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
1518var 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+
2026For example, for bash you could add this to your '~/.bash_profile':
2127
22- eval "$(gh completion)"
28+ eval "$(gh completion -s bash )"
2329
2430When installing GitHub CLI through a package manager, however, it's possible that
2531no 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\n see `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