-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
flags.go
100 lines (83 loc) · 2.13 KB
/
flags.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package arguments
import (
"flag"
"fmt"
)
// flagStringSlice is a flag.Value implementation which allows collecting
// multiple instances of a single flag into a slice. This is used for flags
// such as -target=aws_instance.foo and -var x=y.
type flagStringSlice []string
var _ flag.Value = (*flagStringSlice)(nil)
func (v *flagStringSlice) String() string {
return ""
}
func (v *flagStringSlice) Set(raw string) error {
*v = append(*v, raw)
return nil
}
// flagNameValueSlice is a flag.Value implementation that appends raw flag
// names and values to a slice. This is used to collect a sequence of flags
// with possibly different names, preserving the overall order.
//
// FIXME: this is a copy of rawFlags from command/meta_config.go, with the
// eventual aim of replacing it altogether by gathering variables in the
// arguments package.
type flagNameValueSlice struct {
flagName string
items *[]FlagNameValue
}
var _ flag.Value = flagNameValueSlice{}
func newFlagNameValueSlice(flagName string) flagNameValueSlice {
var items []FlagNameValue
return flagNameValueSlice{
flagName: flagName,
items: &items,
}
}
func (f flagNameValueSlice) Empty() bool {
if f.items == nil {
return true
}
return len(*f.items) == 0
}
func (f flagNameValueSlice) AllItems() []FlagNameValue {
if f.items == nil {
return nil
}
return *f.items
}
func (f flagNameValueSlice) Alias(flagName string) flagNameValueSlice {
return flagNameValueSlice{
flagName: flagName,
items: f.items,
}
}
func (f flagNameValueSlice) String() string {
return ""
}
func (f flagNameValueSlice) Set(str string) error {
*f.items = append(*f.items, FlagNameValue{
Name: f.flagName,
Value: str,
})
return nil
}
type FlagNameValue struct {
Name string
Value string
}
func (f FlagNameValue) String() string {
return fmt.Sprintf("%s=%q", f.Name, f.Value)
}
// FlagIsSet returns whether a flag is explicitly set in a set of flags
func FlagIsSet(flags *flag.FlagSet, name string) bool {
isSet := false
flags.Visit(func(f *flag.Flag) {
if f.Name == name {
isSet = true
}
})
return isSet
}