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