Skip to content

Commit 31c0a70

Browse files
authored
Merge pull request #6 from urfave/fix-issue-5
Fix the incompatibility that occurred after updating `urfave/cli` (v3.0.0-alpha9.2)
2 parents 1da0091 + a82f479 commit 31c0a70

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

.github/workflows/main.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ jobs:
1010
strategy:
1111
matrix:
1212
os: [ubuntu-latest, macos-latest, windows-latest]
13-
go: [1.18.x, 1.19.x, 1.20.x]
13+
go: [stable, oldstable]
1414
runs-on: ${{ matrix.os }}
1515
steps:
16-
- uses: actions/setup-go@v4
16+
- uses: actions/setup-go@v5
1717
with:
1818
go-version: ${{ matrix.go }}
19-
- uses: actions/checkout@v3
19+
- uses: actions/checkout@v4
2020
with:
2121
fetch-depth: 0
2222
- run: make

docs.go

+34-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"os"
9+
"reflect"
910
"regexp"
1011
"runtime"
1112
"sort"
@@ -277,7 +278,7 @@ func prepareFlags(
277278
// flagDetails returns a string containing the flags metadata
278279
func flagDetails(flag cli.DocGenerationFlag) string {
279280
description := flag.GetUsage()
280-
value := flag.GetValue()
281+
value := getFlagDefaultValue(flag)
281282
if value != "" {
282283
description += " (default: " + value + ")"
283284
}
@@ -404,7 +405,7 @@ func (tt tabularTemplate) PrepareFlags(flags []cli.Flag) []cliTabularFlagTemplat
404405
Usage: tt.PrepareMultilineString(flag.GetUsage()),
405406
EnvVars: flag.GetEnvVars(),
406407
TakesValue: flag.TakesValue(),
407-
Default: flag.GetValue(),
408+
Default: getFlagDefaultValue(flag),
408409
}
409410

410411
if boolFlag, isBool := appFlag.(*cli.BoolFlag); isBool {
@@ -554,3 +555,34 @@ func (tabularTemplate) Prettify(s string) string {
554555

555556
return s + "\n" // add an extra newline
556557
}
558+
559+
// getFlagDefaultValue returns the default value of a flag. Previously, the [cli.DocGenerationFlag] interface included
560+
// a GetValue string method, but it was removed in https://github.com/urfave/cli/pull/1988.
561+
// This function serves as a workaround, attempting to retrieve the value using the removed method; if that fails, it
562+
// tries to obtain it via reflection (the [cli.FlagBase] still has a Value field).
563+
func getFlagDefaultValue(f cli.DocGenerationFlag) string {
564+
if !f.TakesValue() {
565+
return ""
566+
}
567+
568+
if v, ok := f.(interface{ GetValue() string }); ok {
569+
return v.GetValue()
570+
}
571+
572+
var ref = reflect.ValueOf(f)
573+
if ref.Kind() != reflect.Ptr {
574+
return ""
575+
} else {
576+
ref = ref.Elem()
577+
}
578+
579+
if ref.Kind() != reflect.Struct {
580+
return ""
581+
}
582+
583+
if val := ref.FieldByName("Value"); val.IsValid() && val.Type().Kind() != reflect.Bool {
584+
return fmt.Sprintf("%v", val.Interface())
585+
}
586+
587+
return ""
588+
}

go.mod

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ go 1.18
44

55
require (
66
github.com/cpuguy83/go-md2man/v2 v2.0.2
7-
github.com/stretchr/testify v1.8.4
8-
github.com/urfave/cli/v3 v3.0.0-alpha4
7+
github.com/stretchr/testify v1.9.0
8+
github.com/urfave/cli/v3 v3.0.0-alpha9.2
99
)
1010

1111
require (
1212
github.com/davecgh/go-spew v1.1.1 // indirect
1313
github.com/pmezard/go-difflib v1.0.0 // indirect
1414
github.com/russross/blackfriday/v2 v2.1.0 // indirect
15-
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
1615
gopkg.in/yaml.v3 v3.0.1 // indirect
1716
)

go.sum

+4-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +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.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
10-
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
11-
github.com/urfave/cli/v3 v3.0.0-alpha4 h1:RJFGIs3mcalmc2YgliDh0Pa4l79S+Dqdz7cW8Fcp7Rg=
12-
github.com/urfave/cli/v3 v3.0.0-alpha4/go.mod h1:ZFqSEHhze0duJACOdz43I5IcnKhf4RoTlOoUMBUggOI=
13-
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
14-
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
9+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
10+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
11+
github.com/urfave/cli/v3 v3.0.0-alpha9.2 h1:CL8llQj3dGRLVQQzHxS+ZYRLanOuhyK1fXgLKD+qV+Y=
12+
github.com/urfave/cli/v3 v3.0.0-alpha9.2/go.mod h1:FnIeEMYu+ko8zP1F9Ypr3xkZMIDqW3DR92yUtY39q1Y=
1513
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1614
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1715
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

0 commit comments

Comments
 (0)