Debian Codegraph @

flag_string.go

raw symbols SCIP
1 package cli
2
3 import (
4 "fmt"
5 "strings"
6 )
7
8 type StringFlag = FlagBase[string, StringConfig, stringValue]
9
10 // StringConfig defines the configuration for string flags
11 type StringConfig struct {
12 // Whether to trim whitespace of parsed value
13 TrimSpace bool
14 }
15
16 // -- string Value
17 type stringValue struct {
18 destination *string
19 trimSpace bool
20 }
21
22 // Below functions are to satisfy the ValueCreator interface
23
24 func (s stringValue) Create(val string, p *string, c StringConfig) Value {
25 *p = val
26 return &stringValue{
27 destination: p,
28 trimSpace: c.TrimSpace,
29 }
30 }
31
32 func (s stringValue) ToString(val string) string {
33 s.destination = &val
34 return s.String()
35 }
36
37 // Below functions are to satisfy the flag.Value interface
38
39 func (s *stringValue) Set(val string) error {
40 if s.trimSpace {
41 val = strings.TrimSpace(val)
42 }
43 *s.destination = val
44 return nil
45 }
46
47 func (s *stringValue) Get() any { return *s.destination }
48
49 func (s *stringValue) String() string {
50 if s.destination != nil && *s.destination != "" {
51 return fmt.Sprintf("%q", *s.destination)
52 }
53 return ""
54 }
55
56 func (cmd *Command) String(name string) string {
57 if v, ok := cmd.Value(name).(string); ok {
58 tracef("string available for flag name %[1]q with value=%[2]v (cmd=%[3]q)", name, v, cmd.Name)
59 return v
60 }
61
62 tracef("string NOT available for flag name %[1]q (cmd=%[2]q)", name, cmd.Name)
63 return ""
64 }