shell_completions.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package cobra
  2. import (
  3. "github.com/spf13/pflag"
  4. )
  5. // MarkFlagRequired instructs the various shell completion implementations to
  6. // prioritize the named flag when performing completion,
  7. // and causes your command to report an error if invoked without the flag.
  8. func (c *Command) MarkFlagRequired(name string) error {
  9. return MarkFlagRequired(c.Flags(), name)
  10. }
  11. // MarkPersistentFlagRequired instructs the various shell completion implementations to
  12. // prioritize the named persistent flag when performing completion,
  13. // and causes your command to report an error if invoked without the flag.
  14. func (c *Command) MarkPersistentFlagRequired(name string) error {
  15. return MarkFlagRequired(c.PersistentFlags(), name)
  16. }
  17. // MarkFlagRequired instructs the various shell completion implementations to
  18. // prioritize the named flag when performing completion,
  19. // and causes your command to report an error if invoked without the flag.
  20. func MarkFlagRequired(flags *pflag.FlagSet, name string) error {
  21. return flags.SetAnnotation(name, BashCompOneRequiredFlag, []string{"true"})
  22. }
  23. // MarkFlagFilename instructs the various shell completion implementations to
  24. // limit completions for the named flag to the specified file extensions.
  25. func (c *Command) MarkFlagFilename(name string, extensions ...string) error {
  26. return MarkFlagFilename(c.Flags(), name, extensions...)
  27. }
  28. // MarkFlagCustom adds the BashCompCustom annotation to the named flag, if it exists.
  29. // The bash completion script will call the bash function f for the flag.
  30. //
  31. // This will only work for bash completion.
  32. // It is recommended to instead use c.RegisterFlagCompletionFunc(...) which allows
  33. // to register a Go function which will work across all shells.
  34. func (c *Command) MarkFlagCustom(name string, f string) error {
  35. return MarkFlagCustom(c.Flags(), name, f)
  36. }
  37. // MarkPersistentFlagFilename instructs the various shell completion
  38. // implementations to limit completions for the named persistent flag to the
  39. // specified file extensions.
  40. func (c *Command) MarkPersistentFlagFilename(name string, extensions ...string) error {
  41. return MarkFlagFilename(c.PersistentFlags(), name, extensions...)
  42. }
  43. // MarkFlagFilename instructs the various shell completion implementations to
  44. // limit completions for the named flag to the specified file extensions.
  45. func MarkFlagFilename(flags *pflag.FlagSet, name string, extensions ...string) error {
  46. return flags.SetAnnotation(name, BashCompFilenameExt, extensions)
  47. }
  48. // MarkFlagCustom adds the BashCompCustom annotation to the named flag, if it exists.
  49. // The bash completion script will call the bash function f for the flag.
  50. //
  51. // This will only work for bash completion.
  52. // It is recommended to instead use c.RegisterFlagCompletionFunc(...) which allows
  53. // to register a Go function which will work across all shells.
  54. func MarkFlagCustom(flags *pflag.FlagSet, name string, f string) error {
  55. return flags.SetAnnotation(name, BashCompCustom, []string{f})
  56. }
  57. // MarkFlagDirname instructs the various shell completion implementations to
  58. // limit completions for the named flag to directory names.
  59. func (c *Command) MarkFlagDirname(name string) error {
  60. return MarkFlagDirname(c.Flags(), name)
  61. }
  62. // MarkPersistentFlagDirname instructs the various shell completion
  63. // implementations to limit completions for the named persistent flag to
  64. // directory names.
  65. func (c *Command) MarkPersistentFlagDirname(name string) error {
  66. return MarkFlagDirname(c.PersistentFlags(), name)
  67. }
  68. // MarkFlagDirname instructs the various shell completion implementations to
  69. // limit completions for the named flag to directory names.
  70. func MarkFlagDirname(flags *pflag.FlagSet, name string) error {
  71. return flags.SetAnnotation(name, BashCompSubdirsInDir, []string{})
  72. }