forked from urfave/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
flag_string.go
66 lines (53 loc) · 1.31 KB
/
flag_string.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
package cli
import (
"fmt"
"strings"
)
type StringFlag = FlagBase[string, StringConfig, stringValue]
// StringConfig defines the configuration for string flags
type StringConfig struct {
// Whether to trim whitespace of parsed value
TrimSpace bool
}
// -- string Value
type stringValue struct {
destination *string
trimSpace bool
}
// Below functions are to satisfy the ValueCreator interface
func (s stringValue) Create(val string, p *string, c StringConfig) Value {
*p = val
return &stringValue{
destination: p,
trimSpace: c.TrimSpace,
}
}
func (s stringValue) ToString(val string) string {
if val == "" {
return val
}
return fmt.Sprintf("%q", val)
}
// Below functions are to satisfy the flag.Value interface
func (s *stringValue) Set(val string) error {
if s.trimSpace {
val = strings.TrimSpace(val)
}
*s.destination = val
return nil
}
func (s *stringValue) Get() any { return *s.destination }
func (s *stringValue) String() string {
if s.destination != nil {
return *s.destination
}
return ""
}
func (cmd *Command) String(name string) string {
if v, ok := cmd.Value(name).(string); ok {
tracef("string available for flag name %[1]q with value=%[2]v (cmd=%[3]q)", name, v, cmd.Name)
return v
}
tracef("string NOT available for flag name %[1]q (cmd=%[2]q)", name, cmd.Name)
return ""
}