shell_completions.go 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2013-2023 The Cobra Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cobra
  15. import (
  16. "github.com/spf13/pflag"
  17. )
  18. // MarkFlagRequired instructs the various shell completion implementations to
  19. // prioritize the named flag when performing completion,
  20. // and causes your command to report an error if invoked without the flag.
  21. func (c *Command) MarkFlagRequired(name string) error {
  22. return MarkFlagRequired(c.Flags(), name)
  23. }
  24. // MarkPersistentFlagRequired instructs the various shell completion implementations to
  25. // prioritize the named persistent flag when performing completion,
  26. // and causes your command to report an error if invoked without the flag.
  27. func (c *Command) MarkPersistentFlagRequired(name string) error {
  28. return MarkFlagRequired(c.PersistentFlags(), name)
  29. }
  30. // MarkFlagRequired instructs the various shell completion implementations to
  31. // prioritize the named flag when performing completion,
  32. // and causes your command to report an error if invoked without the flag.
  33. func MarkFlagRequired(flags *pflag.FlagSet, name string) error {
  34. return flags.SetAnnotation(name, BashCompOneRequiredFlag, []string{"true"})
  35. }
  36. // MarkFlagFilename instructs the various shell completion implementations to
  37. // limit completions for the named flag to the specified file extensions.
  38. func (c *Command) MarkFlagFilename(name string, extensions ...string) error {
  39. return MarkFlagFilename(c.Flags(), name, extensions...)
  40. }
  41. // MarkFlagCustom adds the BashCompCustom annotation to the named flag, if it exists.
  42. // The bash completion script will call the bash function f for the flag.
  43. //
  44. // This will only work for bash completion.
  45. // It is recommended to instead use c.RegisterFlagCompletionFunc(...) which allows
  46. // to register a Go function which will work across all shells.
  47. func (c *Command) MarkFlagCustom(name string, f string) error {
  48. return MarkFlagCustom(c.Flags(), name, f)
  49. }
  50. // MarkPersistentFlagFilename instructs the various shell completion
  51. // implementations to limit completions for the named persistent flag to the
  52. // specified file extensions.
  53. func (c *Command) MarkPersistentFlagFilename(name string, extensions ...string) error {
  54. return MarkFlagFilename(c.PersistentFlags(), name, extensions...)
  55. }
  56. // MarkFlagFilename instructs the various shell completion implementations to
  57. // limit completions for the named flag to the specified file extensions.
  58. func MarkFlagFilename(flags *pflag.FlagSet, name string, extensions ...string) error {
  59. return flags.SetAnnotation(name, BashCompFilenameExt, extensions)
  60. }
  61. // MarkFlagCustom adds the BashCompCustom annotation to the named flag, if it exists.
  62. // The bash completion script will call the bash function f for the flag.
  63. //
  64. // This will only work for bash completion.
  65. // It is recommended to instead use c.RegisterFlagCompletionFunc(...) which allows
  66. // to register a Go function which will work across all shells.
  67. func MarkFlagCustom(flags *pflag.FlagSet, name string, f string) error {
  68. return flags.SetAnnotation(name, BashCompCustom, []string{f})
  69. }
  70. // MarkFlagDirname instructs the various shell completion implementations to
  71. // limit completions for the named flag to directory names.
  72. func (c *Command) MarkFlagDirname(name string) error {
  73. return MarkFlagDirname(c.Flags(), name)
  74. }
  75. // MarkPersistentFlagDirname instructs the various shell completion
  76. // implementations to limit completions for the named persistent flag to
  77. // directory names.
  78. func (c *Command) MarkPersistentFlagDirname(name string) error {
  79. return MarkFlagDirname(c.PersistentFlags(), name)
  80. }
  81. // MarkFlagDirname instructs the various shell completion implementations to
  82. // limit completions for the named flag to directory names.
  83. func MarkFlagDirname(flags *pflag.FlagSet, name string) error {
  84. return flags.SetAnnotation(name, BashCompSubdirsInDir, []string{})
  85. }