shell_completions.go 3.5 KB

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