Skip to content

Commit 994a960

Browse files
committed
Add more coverage
1 parent 7ff69f0 commit 994a960

File tree

9 files changed

+29
-38
lines changed

9 files changed

+29
-38
lines changed

flag_bool.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ func (b boolValue) Create(val bool, p *bool, c BoolConfig) Value {
5050

5151
// ToString formats the bool value
5252
func (b boolValue) ToString(value bool) string {
53-
return strconv.FormatBool(value)
53+
b.destination = &value
54+
return b.String()
5455
}
5556

5657
// Below functions are to satisfy the flag.Value interface

flag_duration.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ func (d durationValue) Create(val time.Duration, p *time.Duration, c NoConfig) V
1818
}
1919

2020
func (d durationValue) ToString(val time.Duration) string {
21-
return fmt.Sprintf("%v", val)
21+
d = durationValue(val)
22+
return d.String()
2223
}
2324

2425
// Below functions are to satisfy the flag.Value interface
@@ -34,7 +35,9 @@ func (d *durationValue) Set(s string) error {
3435

3536
func (d *durationValue) Get() any { return time.Duration(*d) }
3637

37-
func (d *durationValue) String() string { return (*time.Duration)(d).String() }
38+
func (d *durationValue) String() string {
39+
return fmt.Sprintf("%v", time.Duration(*d))
40+
}
3841

3942
func (cmd *Command) Duration(name string) time.Duration {
4043
if v, ok := cmd.Value(name).(time.Duration); ok {

flag_float.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ func (f floatValue[T]) Create(val T, p *T, c NoConfig) Value {
2525
}
2626

2727
func (f floatValue[T]) ToString(b T) string {
28-
return strconv.FormatFloat(float64(b), 'g', -1, int(unsafe.Sizeof(T(0))*8))
28+
f.val = &b
29+
return f.String()
2930
}
3031

3132
// Below functions are to satisfy the flag.Value interface

flag_generic.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ func (f genericValue) Create(val Value, p *Value, c NoConfig) Value {
1717
}
1818

1919
func (f genericValue) ToString(b Value) string {
20-
if b != nil {
21-
return b.String()
22-
}
23-
return ""
20+
f.val = b
21+
return f.String()
2422
}
2523

2624
// Below functions are to satisfy the flag.Value interface

flag_int.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@ func (i intValue[T]) Create(val T, p *T, c IntegerConfig) Value {
3636
}
3737

3838
func (i intValue[T]) ToString(b T) string {
39-
if i.base == 0 {
40-
i.base = 10
41-
}
42-
43-
return strconv.FormatInt(int64(b), i.base)
39+
i.val = &b
40+
return i.String()
4441
}
4542

4643
// Below functions are to satisfy the flag.Value interface

flag_slice_base.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ func (i *SliceBase[T, C, VC]) Set(value string) error {
8282

8383
// String returns a readable representation of this value (for usage defaults)
8484
func (i *SliceBase[T, C, VC]) String() string {
85-
v := i.Value()
86-
var t T
87-
if reflect.TypeOf(t).Kind() == reflect.String {
88-
return fmt.Sprintf("%v", v)
85+
var defaultVals []string
86+
var v VC
87+
for _, s := range *i.slice {
88+
defaultVals = append(defaultVals, v.ToString(s))
8989
}
90-
return fmt.Sprintf("%T{%s}", v, i.ToString(v))
90+
return strings.Join(defaultVals, ", ")
9191
}
9292

9393
// Serialize allows SliceBase to fulfill Serializer
@@ -110,10 +110,6 @@ func (i *SliceBase[T, C, VC]) Get() interface{} {
110110
}
111111

112112
func (i SliceBase[T, C, VC]) ToString(t []T) string {
113-
var defaultVals []string
114-
var v VC
115-
for _, s := range t {
116-
defaultVals = append(defaultVals, v.ToString(s))
117-
}
118-
return strings.Join(defaultVals, ", ")
113+
i.slice = &t
114+
return i.String()
119115
}

flag_string.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ func (s stringValue) Create(val string, p *string, c StringConfig) Value {
3030
}
3131

3232
func (s stringValue) ToString(val string) string {
33-
if val == "" {
34-
return val
35-
}
36-
return fmt.Sprintf("%q", val)
33+
s.destination = &val
34+
return s.String()
3735
}
3836

3937
// Below functions are to satisfy the flag.Value interface
@@ -49,8 +47,8 @@ func (s *stringValue) Set(val string) error {
4947
func (s *stringValue) Get() any { return *s.destination }
5048

5149
func (s *stringValue) String() string {
52-
if s.destination != nil {
53-
return *s.destination
50+
if s.destination != nil && *s.destination != "" {
51+
return fmt.Sprintf("%q", *s.destination)
5452
}
5553
return ""
5654
}

flag_timestamp.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ func (t timestampValue) ToString(b time.Time) string {
4444
if b.IsZero() {
4545
return ""
4646
}
47-
return fmt.Sprintf("%v", b)
47+
t.timestamp = &b
48+
return t.String()
4849
}
4950

5051
// Below functions are to satisfy the Value interface
@@ -122,7 +123,7 @@ func (t *timestampValue) Set(value string) error {
122123

123124
// String returns a readable representation of this value (for usage defaults)
124125
func (t *timestampValue) String() string {
125-
return fmt.Sprintf("%#v", t.timestamp)
126+
return fmt.Sprintf("%v", t.timestamp)
126127
}
127128

128129
// Get returns the flag structure

flag_uint.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,8 @@ func (i uintValue[T]) Create(val T, p *T, c IntegerConfig) Value {
3131
}
3232

3333
func (i uintValue[T]) ToString(b T) string {
34-
base := i.base
35-
if base == 0 {
36-
base = 10
37-
}
38-
39-
return strconv.FormatUint(uint64(b), base)
34+
i.val = &b
35+
return i.String()
4036
}
4137

4238
// Below functions are to satisfy the flag.Value interface

0 commit comments

Comments
 (0)