-
Notifications
You must be signed in to change notification settings - Fork 481
/
cobrautil.go
53 lines (47 loc) · 1.29 KB
/
cobrautil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package cobrautil
import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// HideInheritedFlags hides inherited flags
func HideInheritedFlags(cmd *cobra.Command, hidden ...string) {
for _, h := range hidden {
// we could use cmd.SetHelpFunc to override the helper
// but, it's not enough because we also want the generated
// docs to be updated, so we override the flag instead
cmd.Flags().String(h, "", "")
_ = cmd.Flags().MarkHidden(h)
}
}
const annotationExperimentalCLI = "experimentalCLI"
func MarkFlagExperimental(f *pflag.Flag) {
if _, ok := f.Annotations[annotationExperimentalCLI]; ok {
return
}
if f.Annotations == nil {
f.Annotations = make(map[string][]string)
}
f.Annotations[annotationExperimentalCLI] = nil
f.Usage += " (EXPERIMENTAL)"
}
func MarkFlagsExperimental(fs *pflag.FlagSet, names ...string) {
for _, name := range names {
f := fs.Lookup(name)
if f == nil {
logrus.Warningf("Unknown flag name %q", name)
continue
}
MarkFlagExperimental(f)
}
}
func MarkCommandExperimental(c *cobra.Command) {
if _, ok := c.Annotations[annotationExperimentalCLI]; ok {
return
}
if c.Annotations == nil {
c.Annotations = make(map[string]string)
}
c.Annotations[annotationExperimentalCLI] = ""
c.Short += " (EXPERIMENTAL)"
}