github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/cmd/tendermint/commands/completion.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/spf13/cobra"
     7  )
     8  
     9  // NewCompletionCmd returns a cobra.Command that generates bash and zsh
    10  // completion scripts for the given root command. If hidden is true, the
    11  // command will not show up in the root command's list of available commands.
    12  func NewCompletionCmd(rootCmd *cobra.Command, hidden bool) *cobra.Command {
    13  	flagZsh := "zsh"
    14  	cmd := &cobra.Command{
    15  		Use:   "completion",
    16  		Short: "Generate shell completion scripts",
    17  		Long: fmt.Sprintf(`Generate Bash and Zsh completion scripts and print them to STDOUT.
    18  
    19  Once saved to file, a completion script can be loaded in the shell's
    20  current session as shown:
    21  
    22     $ . <(%s completion)
    23  
    24  To configure your bash shell to load completions for each session add to
    25  your $HOME/.bashrc or $HOME/.profile the following instruction:
    26  
    27     . <(%s completion)
    28  `, rootCmd.Use, rootCmd.Use),
    29  		RunE: func(cmd *cobra.Command, _ []string) error {
    30  			zsh, err := cmd.Flags().GetBool(flagZsh)
    31  			if err != nil {
    32  				return err
    33  			}
    34  			if zsh {
    35  				return rootCmd.GenZshCompletion(cmd.OutOrStdout())
    36  			}
    37  			return rootCmd.GenBashCompletion(cmd.OutOrStdout())
    38  		},
    39  		Hidden: hidden,
    40  		Args:   cobra.NoArgs,
    41  	}
    42  
    43  	cmd.Flags().Bool(flagZsh, false, "Generate Zsh completion script")
    44  
    45  	return cmd
    46  }