1package cli
2
3import "flag"
4
5type PathFlag struct {
6	Name        string
7	Aliases     []string
8	Usage       string
9	EnvVars     []string
10	FilePath    string
11	Required    bool
12	Hidden      bool
13	TakesFile   bool
14	Value       string
15	DefaultText string
16	Destination *string
17	HasBeenSet  bool
18}
19
20// IsSet returns whether or not the flag has been set through env or file
21func (f *PathFlag) IsSet() bool {
22	return f.HasBeenSet
23}
24
25// String returns a readable representation of this value
26// (for usage defaults)
27func (f *PathFlag) String() string {
28	return FlagStringer(f)
29}
30
31// Names returns the names of the flag
32func (f *PathFlag) Names() []string {
33	return flagNames(f.Name, f.Aliases)
34}
35
36// IsRequired returns whether or not the flag is required
37func (f *PathFlag) IsRequired() bool {
38	return f.Required
39}
40
41// TakesValue returns true of the flag takes a value, otherwise false
42func (f *PathFlag) TakesValue() bool {
43	return true
44}
45
46// GetUsage returns the usage string for the flag
47func (f *PathFlag) GetUsage() string {
48	return f.Usage
49}
50
51// GetValue returns the flags value as string representation and an empty
52// string if the flag takes no value at all.
53func (f *PathFlag) GetValue() string {
54	return f.Value
55}
56
57// Apply populates the flag given the flag set and environment
58func (f *PathFlag) Apply(set *flag.FlagSet) error {
59	if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
60		f.Value = val
61		f.HasBeenSet = true
62	}
63
64	for _, name := range f.Names() {
65		if f.Destination != nil {
66			set.StringVar(f.Destination, name, f.Value, f.Usage)
67			continue
68		}
69		set.String(name, f.Value, f.Usage)
70	}
71
72	return nil
73}
74
75// Path looks up the value of a local PathFlag, returns
76// "" if not found
77func (c *Context) Path(name string) string {
78	if fs := lookupFlagSet(name, c); fs != nil {
79		return lookupPath(name, fs)
80	}
81
82	return ""
83}
84
85func lookupPath(name string, set *flag.FlagSet) string {
86	f := set.Lookup(name)
87	if f != nil {
88		parsed, err := f.Value.String(), error(nil)
89		if err != nil {
90			return ""
91		}
92		return parsed
93	}
94	return ""
95}
96