Skip to content

Commit

Permalink
Include a basic example spf13#465 (spf13#631)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason-Hendry authored and eparis committed May 31, 2018
1 parent ef82de7 commit 1e58aa3
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions bash_completions.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
# Generating Bash Completions For Your Own cobra.Command

If you are using the generator you can create a completion command by running

```bash
cobra add completion
```

Update the help text show how to install the bash_completion Linux show here [Kubectl docs show mac options](https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion)

Writing the shell script to stdout allows the most flexible use.

```go
// completionCmd represents the completion command
var completionCmd = &cobra.Command{
Use: "completion",
Short: "Generates bash completion scripts",
Long: `To load completion run
. <(bitbucket completion)
To configure your bash shell to load completions for each session add to your bashrc
# ~/.bashrc or ~/.profile
. <(bitbucket completion)
`,
Run: func(cmd *cobra.Command, args []string) {
rootCmd.GenBashCompletion(os.Stdout);
},
}
```

**Note:** The cobra generator may include messages printed to stdout for example if the config file is loaded, this will break the auto complete script


## Example from kubectl

Generating bash completions from a cobra command is incredibly easy. An actual program which does so for the kubernetes kubectl binary is as follows:

```go
Expand Down

0 comments on commit 1e58aa3

Please sign in to comment.