Skip to content

Commit 7f9aef6

Browse files
authored
Merge pull request cli#761 from cli/completion-fixes
Improve `completion` command
2 parents a6495a8 + 2915abc commit 7f9aef6

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

cmd/gh/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ func printError(out io.Writer, err error, cmd *cobra.Command, debug bool) {
7070
}
7171

7272
func shouldCheckForUpdate() bool {
73-
return updaterEnabled != "" && utils.IsTerminal(os.Stderr)
73+
return updaterEnabled != "" && !isCompletionCommand() && utils.IsTerminal(os.Stderr)
74+
}
75+
76+
func isCompletionCommand() bool {
77+
return len(os.Args) > 1 && os.Args[1] == "completion"
7478
}
7579

7680
func checkForUpdate(currentVersion string) (*update.ReleaseInfo, error) {

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)