Debian Codegraph @

vendor/github.com/urfave/cli/v2/flag_string.go

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