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