diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f98f5b7d..c9140c15 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,13 +10,13 @@ jobs: strategy: matrix: os: [ubuntu-latest, macos-latest, windows-latest] - go: [1.18.x, 1.19.x, 1.20.x] + go: [stable, oldstable] runs-on: ${{ matrix.os }} steps: - - uses: actions/setup-go@v4 + - uses: actions/setup-go@v5 with: go-version: ${{ matrix.go }} - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: fetch-depth: 0 - run: make diff --git a/docs.go b/docs.go index 8ff9ab30..e5931261 100644 --- a/docs.go +++ b/docs.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "os" + "reflect" "regexp" "runtime" "sort" @@ -277,7 +278,7 @@ func prepareFlags( // flagDetails returns a string containing the flags metadata func flagDetails(flag cli.DocGenerationFlag) string { description := flag.GetUsage() - value := flag.GetValue() + value := getFlagDefaultValue(flag) if value != "" { description += " (default: " + value + ")" } @@ -404,7 +405,7 @@ func (tt tabularTemplate) PrepareFlags(flags []cli.Flag) []cliTabularFlagTemplat Usage: tt.PrepareMultilineString(flag.GetUsage()), EnvVars: flag.GetEnvVars(), TakesValue: flag.TakesValue(), - Default: flag.GetValue(), + Default: getFlagDefaultValue(flag), } if boolFlag, isBool := appFlag.(*cli.BoolFlag); isBool { @@ -554,3 +555,34 @@ func (tabularTemplate) Prettify(s string) string { return s + "\n" // add an extra newline } + +// getFlagDefaultValue returns the default value of a flag. Previously, the [cli.DocGenerationFlag] interface included +// a GetValue string method, but it was removed in https://github.com/urfave/cli/pull/1988. +// This function serves as a workaround, attempting to retrieve the value using the removed method; if that fails, it +// tries to obtain it via reflection (the [cli.FlagBase] still has a Value field). +func getFlagDefaultValue(f cli.DocGenerationFlag) string { + if !f.TakesValue() { + return "" + } + + if v, ok := f.(interface{ GetValue() string }); ok { + return v.GetValue() + } + + var ref = reflect.ValueOf(f) + if ref.Kind() != reflect.Ptr { + return "" + } else { + ref = ref.Elem() + } + + if ref.Kind() != reflect.Struct { + return "" + } + + if val := ref.FieldByName("Value"); val.IsValid() && val.Type().Kind() != reflect.Bool { + return fmt.Sprintf("%v", val.Interface()) + } + + return "" +} diff --git a/go.mod b/go.mod index aad6d5f3..3faa2d20 100644 --- a/go.mod +++ b/go.mod @@ -4,14 +4,13 @@ go 1.18 require ( github.com/cpuguy83/go-md2man/v2 v2.0.2 - github.com/stretchr/testify v1.8.4 - github.com/urfave/cli/v3 v3.0.0-alpha4 + github.com/stretchr/testify v1.9.0 + github.com/urfave/cli/v3 v3.0.0-alpha9.2 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect - github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 126cb5e8..1ffd8d4f 100644 --- a/go.sum +++ b/go.sum @@ -6,12 +6,10 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/urfave/cli/v3 v3.0.0-alpha4 h1:RJFGIs3mcalmc2YgliDh0Pa4l79S+Dqdz7cW8Fcp7Rg= -github.com/urfave/cli/v3 v3.0.0-alpha4/go.mod h1:ZFqSEHhze0duJACOdz43I5IcnKhf4RoTlOoUMBUggOI= -github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= -github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/urfave/cli/v3 v3.0.0-alpha9.2 h1:CL8llQj3dGRLVQQzHxS+ZYRLanOuhyK1fXgLKD+qV+Y= +github.com/urfave/cli/v3 v3.0.0-alpha9.2/go.mod h1:FnIeEMYu+ko8zP1F9Ypr3xkZMIDqW3DR92yUtY39q1Y= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=