github.com/zaquestion/lab@v0.25.1/cmd/completion.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/MakeNowJust/heredoc/v2"
     8  	"github.com/rsteube/carapace"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  // completionCmd represents the completion command
    13  var completionCmd = &cobra.Command{
    14  	Use:   "completion [shell]",
    15  	Short: "Generates autocompletion for different shell implementations",
    16  	Long: heredoc.Doc(`
    17  		Generates shell autocompletion scripts for different implementations.
    18  
    19  		These scripts can be directly sourced, though using pre-generated
    20  		versions is recommended to avoid shell startup delay.`),
    21  	Example: heredoc.Doc(`
    22  		bash       : source <(lab completion)
    23  		elvish     : eval(lab completion|slurp)
    24  		fish       : lab completion | source
    25  		oil        : source <(lab completion)
    26  		powershell : lab completion | Out-String | Invoke-Expression
    27  		xonsh      : exec($(lab completion xonsh))
    28  		zsh        : source <(lab completion zsh)`),
    29  	ValidArgs: []string{"bash", "elvish", "fish", "oil", "powershell", "xonsh", "zsh"},
    30  	Run: func(cmd *cobra.Command, args []string) {
    31  		shell := ""
    32  		if len(args) > 0 {
    33  			shell = args[0]
    34  		}
    35  		if script, err := carapace.Gen(cmd).Snippet(shell); err != nil {
    36  			fmt.Fprintln(os.Stderr, err.Error())
    37  		} else {
    38  			fmt.Println(script)
    39  		}
    40  	},
    41  }
    42  
    43  func init() {
    44  	RootCmd.AddCommand(completionCmd)
    45  }