// Package carapace is a command argument completion generator for spf13/cobra
package carapace import ( ) // Carapace wraps cobra.Command to define completions. type Carapace struct { cmd *cobra.Command } // Gen initialized Carapace for given command. func ( *cobra.Command) *Carapace { addCompletionCommand() storage.bridge() return &Carapace{ cmd: , } } // PreRun sets a function to be run before completion. func ( Carapace) ( func( *cobra.Command, []string)) { if := storage.get(.cmd); .prerun != nil { := .prerun .prerun = func( *cobra.Command, []string) { // TODO yuck - probably best to append to a slice in storage (, ) (, ) } } else { .prerun = } } // PreInvoke sets a function to alter actions before they are invoked. func ( Carapace) ( func( *cobra.Command, *pflag.Flag, Action) Action) { if := storage.get(.cmd); .preinvoke != nil { := .preinvoke .preinvoke = func( *cobra.Command, *pflag.Flag, Action) Action { return (, , (, , )) } } else { .preinvoke = } } // PositionalCompletion defines completion for positional arguments using a list of Actions. func ( Carapace) ( ...Action) { storage.get(.cmd).positional = } // PositionalAnyCompletion defines completion for any positional arguments not already defined. func ( Carapace) ( Action) { storage.get(.cmd).positionalAny = & } // DashCompletion defines completion for positional arguments after dash (`--`) using a list of Actions. func ( Carapace) ( ...Action) { storage.get(.cmd).dash = } // DashAnyCompletion defines completion for any positional arguments after dash (`--`) not already defined. func ( Carapace) ( Action) { storage.get(.cmd).dashAny = & } // FlagCompletion defines completion for flags using a map consisting of name and Action. func ( Carapace) ( ActionMap) { := storage.get(.cmd) .flagMutex.Lock() defer .flagMutex.Unlock() if .flag == nil { .flag = } else { for , := range { .flag[] = } } } const annotation_standalone = "carapace_standalone" // Standalone prevents cobra defaults interfering with standalone mode (e.g. implicit help command). func ( Carapace) () { .cmd.CompletionOptions = cobra.CompletionOptions{ DisableDefaultCmd: true, } if .cmd.Annotations == nil { .cmd.Annotations = make(map[string]string) } .cmd.Annotations[annotation_standalone] = "true" .PreRun(func( *cobra.Command, []string) { if := .Flag("help"); == nil { .Flags().Bool("help", false, "") .Flag("help").Hidden = true } else if .Annotations != nil { if , := .Annotations[cobra.FlagSetByCobraAnnotation]; { .Flag("help").Hidden = true } } }) .cmd.SetHelpCommand(&cobra.Command{Use: "_carapace_help", Hidden: true, Deprecated: "fake help command to prevent default"}) } // Snippet creates completion script for given shell. func ( Carapace) ( string) (string, error) { return shell.Snippet(.cmd, ) } // IsCallback returns true if current program invocation is a callback. func () bool { return len(os.Args) > 1 && os.Args[1] == "_carapace" } // Test verifies the configuration (e.g. flag name exists) // // func TestCarapace(t *testing.T) { // carapace.Test(t) // } func ( interface{ ( ...interface{}) }) { for , := range storage.check() { .() } }