1package cobra 2 3import ( 4 "github.com/spf13/pflag" 5) 6 7// MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag if it exists, 8// and causes your command to report an error if invoked without the flag. 9func (c *Command) MarkFlagRequired(name string) error { 10 return MarkFlagRequired(c.Flags(), name) 11} 12 13// MarkPersistentFlagRequired adds the BashCompOneRequiredFlag annotation to the named persistent flag if it exists, 14// and causes your command to report an error if invoked without the flag. 15func (c *Command) MarkPersistentFlagRequired(name string) error { 16 return MarkFlagRequired(c.PersistentFlags(), name) 17} 18 19// MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag if it exists, 20// and causes your command to report an error if invoked without the flag. 21func MarkFlagRequired(flags *pflag.FlagSet, name string) error { 22 return flags.SetAnnotation(name, BashCompOneRequiredFlag, []string{"true"}) 23} 24 25// MarkFlagFilename adds the BashCompFilenameExt annotation to the named flag, if it exists. 26// Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided. 27func (c *Command) MarkFlagFilename(name string, extensions ...string) error { 28 return MarkFlagFilename(c.Flags(), name, extensions...) 29} 30 31// MarkFlagCustom adds the BashCompCustom annotation to the named flag, if it exists. 32// Generated bash autocompletion will call the bash function f for the flag. 33func (c *Command) MarkFlagCustom(name string, f string) error { 34 return MarkFlagCustom(c.Flags(), name, f) 35} 36 37// MarkPersistentFlagFilename instructs the various shell completion 38// implementations to limit completions for this persistent flag to the 39// specified extensions (patterns). 40// 41// Shell Completion compatibility matrix: bash, zsh 42func (c *Command) MarkPersistentFlagFilename(name string, extensions ...string) error { 43 return MarkFlagFilename(c.PersistentFlags(), name, extensions...) 44} 45 46// MarkFlagFilename instructs the various shell completion implementations to 47// limit completions for this flag to the specified extensions (patterns). 48// 49// Shell Completion compatibility matrix: bash, zsh 50func MarkFlagFilename(flags *pflag.FlagSet, name string, extensions ...string) error { 51 return flags.SetAnnotation(name, BashCompFilenameExt, extensions) 52} 53 54// MarkFlagCustom instructs the various shell completion implementations to 55// limit completions for this flag to the specified extensions (patterns). 56// 57// Shell Completion compatibility matrix: bash, zsh 58func MarkFlagCustom(flags *pflag.FlagSet, name string, f string) error { 59 return flags.SetAnnotation(name, BashCompCustom, []string{f}) 60} 61 62// MarkFlagDirname instructs the various shell completion implementations to 63// complete only directories with this named flag. 64// 65// Shell Completion compatibility matrix: zsh 66func (c *Command) MarkFlagDirname(name string) error { 67 return MarkFlagDirname(c.Flags(), name) 68} 69 70// MarkPersistentFlagDirname instructs the various shell completion 71// implementations to complete only directories with this persistent named flag. 72// 73// Shell Completion compatibility matrix: zsh 74func (c *Command) MarkPersistentFlagDirname(name string) error { 75 return MarkFlagDirname(c.PersistentFlags(), name) 76} 77 78// MarkFlagDirname instructs the various shell completion implementations to 79// complete only directories with this specified flag. 80// 81// Shell Completion compatibility matrix: zsh 82func MarkFlagDirname(flags *pflag.FlagSet, name string) error { 83 zshPattern := "-(/)" 84 return flags.SetAnnotation(name, zshCompDirname, []string{zshPattern}) 85} 86