| 1 |
package cli |
| 2 |
|
| 3 |
import ( |
| 4 |
"flag" |
| 5 |
"fmt" |
| 6 |
) |
| 7 |
|
| 8 |
type Path = string |
| 9 |
|
| 10 |
// TakesValue returns true of the flag takes a value, otherwise false |
| 11 |
func (f *PathFlag) TakesValue() bool { |
| 12 |
return true |
| 13 |
} |
| 14 |
|
| 15 |
// GetUsage returns the usage string for the flag |
| 16 |
func (f *PathFlag) GetUsage() string { |
| 17 |
return f.Usage |
| 18 |
} |
| 19 |
|
| 20 |
// GetCategory returns the category for the flag |
| 21 |
func (f *PathFlag) GetCategory() string { |
| 22 |
return f.Category |
| 23 |
} |
| 24 |
|
| 25 |
// GetValue returns the flags value as string representation and an empty |
| 26 |
// string if the flag takes no value at all. |
| 27 |
func (f *PathFlag) GetValue() string { |
| 28 |
return f.Value |
| 29 |
} |
| 30 |
|
| 31 |
// GetDefaultText returns the default text for this flag |
| 32 |
func (f *PathFlag) GetDefaultText() string { |
| 33 |
if f.DefaultText != "" { |
| 34 |
return f.DefaultText |
| 35 |
} |
| 36 |
val := f.Value |
| 37 |
if f.defaultValueSet { |
| 38 |
val = f.defaultValue |
| 39 |
} |
| 40 |
if val == "" { |
| 41 |
return val |
| 42 |
} |
| 43 |
return fmt.Sprintf("%q", val) |
| 44 |
} |
| 45 |
|
| 46 |
// GetEnvVars returns the env vars for this flag |
| 47 |
func (f *PathFlag) GetEnvVars() []string { |
| 48 |
return f.EnvVars |
| 49 |
} |
| 50 |
|
| 51 |
// Apply populates the flag given the flag set and environment |
| 52 |
func (f *PathFlag) Apply(set *flag.FlagSet) error { |
| 53 |
// set default value so that environment wont be able to overwrite it |
| 54 |
f.defaultValue = f.Value |
| 55 |
f.defaultValueSet = true |
| 56 |
|
| 57 |
if val, _, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found { |
| 58 |
f.Value = val |
| 59 |
f.HasBeenSet = true |
| 60 |
} |
| 61 |
|
| 62 |
for _, name := range f.Names() { |
| 63 |
if f.Destination != nil { |
| 64 |
set.StringVar(f.Destination, name, f.Value, f.Usage) |
| 65 |
continue |
| 66 |
} |
| 67 |
set.String(name, f.Value, f.Usage) |
| 68 |
} |
| 69 |
|
| 70 |
return nil |
| 71 |
} |
| 72 |
|
| 73 |
// Get returns the flag’s value in the given Context. |
| 74 |
func (f *PathFlag) Get(ctx *Context) string { |
| 75 |
return ctx.Path(f.Name) |
| 76 |
} |
| 77 |
|
| 78 |
// RunAction executes flag action if set |
| 79 |
func (f *PathFlag) RunAction(c *Context) error { |
| 80 |
if f.Action != nil { |
| 81 |
return f.Action(c, c.Path(f.Name)) |
| 82 |
} |
| 83 |
|
| 84 |
return nil |
| 85 |
} |
| 86 |
|
| 87 |
// Path looks up the value of a local PathFlag, returns |
| 88 |
// "" if not found |
| 89 |
func (cCtx *Context) Path(name string) string { |
| 90 |
if fs := cCtx.lookupFlagSet(name); fs != nil { |
| 91 |
return lookupPath(name, fs) |
| 92 |
} |
| 93 |
|
| 94 |
return "" |
| 95 |
} |
| 96 |
|
| 97 |
func lookupPath(name string, set *flag.FlagSet) string { |
| 98 |
if f := set.Lookup(name); f != nil { |
| 99 |
return f.Value.String() |
| 100 |
} |
| 101 |
return "" |
| 102 |
} |