Skip to content

Commit 72b87d1

Browse files
authored
Merge pull request #20 from urfave/fix_reflection
Fix: Remove reflection in getFlagDefaultValue
2 parents 9c1a23c + 771733a commit 72b87d1

File tree

5 files changed

+52
-51
lines changed

5 files changed

+52
-51
lines changed

docs.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"io"
88
"os"
9-
"reflect"
109
"regexp"
1110
"runtime"
1211
"sort"
@@ -567,13 +566,8 @@ func (tabularTemplate) Prettify(s string) string {
567566
// getFlagDefaultValue returns the default text or default value of a flag.
568567
// cli.BoolFlag will always return an default.
569568
func getFlagDefaultValue(f cli.DocGenerationFlag) (value, text string) {
570-
// GetDefaultText also returns GetValue so we have to use reflection
571-
if ref := reflect.ValueOf(f); ref.Kind() == reflect.Ptr && ref.Elem().Kind() == reflect.Struct {
572-
if val := ref.Elem().FieldByName("DefaultText"); val.IsValid() && val.Type().Kind() == reflect.String {
573-
if defaultText := val.Interface().(string); defaultText != "" {
574-
return "", defaultText
575-
}
576-
}
569+
if defaultText := f.GetDefaultText(); defaultText != "" {
570+
return "", defaultText
577571
}
578572

579573
if !f.TakesValue() {
@@ -583,5 +577,12 @@ func getFlagDefaultValue(f cli.DocGenerationFlag) (value, text string) {
583577
return "", ""
584578
}
585579

586-
return f.GetValue(), ""
580+
if value := f.GetValue(); value != "" {
581+
if _, ok := f.(*cli.StringFlag); ok {
582+
return strings.Trim(value, "\""), ""
583+
} else {
584+
return value, ""
585+
}
586+
}
587+
return "", ""
587588
}

docs_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ func buildFlagTest(t *testing.T) *cli.Command {
5151
&cli.StringMapFlag{Name: "string-map-set", Value: map[string]string{"a": "b"}},
5252
&cli.StringSliceFlag{Name: "string-slice-empty"},
5353
&cli.StringSliceFlag{Name: "string-slice-set", Value: []string{"a", "b"}},
54-
&cli.IntFlag{Name: "string-empty"},
55-
&cli.IntFlag{Name: "string-set", Value: 42},
56-
&cli.UintFlag{Name: "string-empty"},
57-
&cli.UintFlag{Name: "string-set", Value: 21},
58-
&cli.FloatFlag{Name: "string-empty"},
59-
&cli.FloatFlag{Name: "string-set", Value: 88.32},
60-
&cli.DurationFlag{Name: "string-empty"},
61-
&cli.DurationFlag{Name: "string-set", Value: 12 * time.Minute},
62-
&cli.TimestampFlag{Name: "string-empty"},
63-
&cli.TimestampFlag{Name: "string-set", Value: time.Unix(1729456800, 0).UTC()},
54+
&cli.IntFlag{Name: "int-empty"},
55+
&cli.IntFlag{Name: "int-set", Value: 42},
56+
&cli.UintFlag{Name: "uint-empty"},
57+
&cli.UintFlag{Name: "uint-set", Value: 21},
58+
&cli.FloatFlag{Name: "float-empty"},
59+
&cli.FloatFlag{Name: "float-set", Value: 88.32},
60+
&cli.DurationFlag{Name: "duration-empty"},
61+
&cli.DurationFlag{Name: "duration-set", Value: 12 * time.Minute},
62+
&cli.TimestampFlag{Name: "timestamp-empty"},
63+
&cli.TimestampFlag{Name: "timestamp-set", Value: time.Unix(1729456800, 0).UTC()},
6464
}}
6565
}
6666

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ go 1.24.4
44

55
require (
66
github.com/cpuguy83/go-md2man/v2 v2.0.2
7-
github.com/stretchr/testify v1.10.0
8-
github.com/urfave/cli/v3 v3.4.1
7+
github.com/stretchr/testify v1.11.1
8+
github.com/urfave/cli/v3 v3.5.0
99
)
1010

1111
require (

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
66
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
77
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
88
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
9-
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
10-
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
11-
github.com/urfave/cli/v3 v3.4.1 h1:1M9UOCy5bLmGnuu1yn3t3CB4rG79Rtoxuv1sPhnm6qM=
12-
github.com/urfave/cli/v3 v3.4.1/go.mod h1:FJSKtM/9AiiTOJL4fJ6TbMUkxBXn7GO9guZqoZtpYpo=
9+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
10+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
11+
github.com/urfave/cli/v3 v3.5.0 h1:qCuFMmdayTF3zmjG8TSsoBzrDqszNrklYg2x3g4MSgw=
12+
github.com/urfave/cli/v3 v3.5.0/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso=
1313
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1414
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1515
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

testdata/expected-doc-all-flag-types.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ main
99
```
1010
[--bool-false]
1111
[--bool-true]
12-
[--string-empty]=[value]
13-
[--string-empty]=[value]
14-
[--string-empty]=[value]
15-
[--string-empty]=[value]
16-
[--string-empty]=[value]
12+
[--duration-empty]=[value]
13+
[--duration-set]=[value]
14+
[--float-empty]=[value]
15+
[--float-set]=[value]
16+
[--int-empty]=[value]
17+
[--int-set]=[value]
1718
[--string-empty]=[value]
1819
[--string-map-empty]=[value]
1920
[--string-map-set]=[value]
2021
[--string-set]=[value]
21-
[--string-set]=[value]
22-
[--string-set]=[value]
23-
[--string-set]=[value]
24-
[--string-set]=[value]
25-
[--string-set]=[value]
2622
[--string-slice-empty]=[value]
2723
[--string-slice-set]=[value]
24+
[--timestamp-empty]=[value]
25+
[--timestamp-set]=[value]
26+
[--uint-empty]=[value]
27+
[--uint-set]=[value]
2828
```
2929

3030
**Usage**:
@@ -39,35 +39,35 @@ main [GLOBAL OPTIONS] [command [COMMAND OPTIONS]] [ARGUMENTS...]
3939

4040
**--bool-true**: (default: true)
4141

42-
**--string-empty**="":
42+
**--duration-empty**="": (default: 0s)
4343

44-
**--string-empty**="": (default: 0)
44+
**--duration-set**="": (default: 12m0s)
4545

46-
**--string-empty**="": (default: 0)
46+
**--float-empty**="": (default: 0)
4747

48-
**--string-empty**="": (default: 0)
48+
**--float-set**="": (default: 88.32)
4949

50-
**--string-empty**="": (default: 0001-01-01 00:00:00 +0000 UTC)
50+
**--int-empty**="": (default: 0)
5151

52-
**--string-empty**="": (default: 0s)
52+
**--int-set**="": (default: 42)
5353

54-
**--string-map-empty**="": (default: map[])
54+
**--string-empty**="":
5555

56-
**--string-map-set**="": (default: map[a:b])
56+
**--string-map-empty**="":
5757

58-
**--string-set**="": (default: 12m0s)
58+
**--string-map-set**="": (default: a="b")
5959

60-
**--string-set**="": (default: 2024-10-20 20:40:00 +0000 UTC)
60+
**--string-set**="": (default: a string)
6161

62-
**--string-set**="": (default: 21)
62+
**--string-slice-empty**="":
6363

64-
**--string-set**="": (default: 42)
64+
**--string-slice-set**="": (default: "a", "b")
6565

66-
**--string-set**="": (default: 88.32)
66+
**--timestamp-empty**="":
6767

68-
**--string-set**="": (default: a string)
68+
**--timestamp-set**="": (default: 2024-10-20 20:40:00 +0000 UTC)
6969

70-
**--string-slice-empty**="": (default: [])
70+
**--uint-empty**="": (default: 0)
7171

72-
**--string-slice-set**="": (default: [a b])
72+
**--uint-set**="": (default: 21)
7373

0 commit comments

Comments
 (0)