From 16423c255dc3cdf9c173cd255eb1b50fac0efb44 Mon Sep 17 00:00:00 2001 From: Joshua Swick Date: Thu, 10 Apr 2025 13:24:58 -0600 Subject: [PATCH 1/4] prioritize default text --- docs.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/docs.go b/docs.go index e593126..4815cc3 100644 --- a/docs.go +++ b/docs.go @@ -565,14 +565,8 @@ func getFlagDefaultValue(f cli.DocGenerationFlag) string { return "" } - if v, ok := f.(interface{ GetValue() string }); ok { - return v.GetValue() - } - var ref = reflect.ValueOf(f) - if ref.Kind() != reflect.Ptr { - return "" - } else { + if ref.Kind() == reflect.Ptr { ref = ref.Elem() } @@ -580,6 +574,10 @@ func getFlagDefaultValue(f cli.DocGenerationFlag) string { return "" } + if defaultTextVal := ref.FieldByName("DefaultText"); defaultTextVal.IsValid() && defaultTextVal.Kind() == reflect.String && defaultTextVal.String() != "" { + return defaultTextVal.String() + } + if val := ref.FieldByName("Value"); val.IsValid() && val.Type().Kind() != reflect.Bool { return fmt.Sprintf("%v", val.Interface()) } From 90e5d23aa1474ad212bceb50b38093f7f0de4cd1 Mon Sep 17 00:00:00 2001 From: Joshua Swick Date: Thu, 10 Apr 2025 14:06:33 -0600 Subject: [PATCH 2/4] init tests --- docs_test.go | 14 ++++++++++++++ testdata/expected-doc-full.md | 3 +++ testdata/expected-tabular-markdown-full.md | 1 + 3 files changed, 18 insertions(+) diff --git a/docs_test.go b/docs_test.go index 66986d1..1393b19 100644 --- a/docs_test.go +++ b/docs_test.go @@ -3,6 +3,7 @@ package docs import ( "bytes" "embed" + "fmt" "io" "io/fs" "net/mail" @@ -62,6 +63,11 @@ func buildExtendedTestCommand() *cli.Command { Name: "hidden-flag", Hidden: true, }, + &cli.StringFlag{ + Name: "dir", + Value: pwd(), + DefaultText: "$PWD", + }, }, Commands: []*cli.Command{{ Aliases: []string{"c"}, @@ -182,6 +188,14 @@ func TestToTabularMarkdown(t *testing.T) { }) } +func pwd() string { + pwd, err := os.Getwd() + if err != nil { + fmt.Println(err) + } + return pwd +} + func TestToTabularMarkdownFailed(t *testing.T) { tpl := MarkdownTabularDocTemplate t.Cleanup(func() { MarkdownTabularDocTemplate = tpl }) diff --git a/testdata/expected-doc-full.md b/testdata/expected-doc-full.md index 80ff6a7..7094947 100644 --- a/testdata/expected-doc-full.md +++ b/testdata/expected-doc-full.md @@ -8,6 +8,7 @@ greet ``` [--another-flag|-b] +[--dir]=[value] [--flag|--fl|-f]=[value] [--socket|-s]=[value] ``` @@ -26,6 +27,8 @@ app [first_arg] [second_arg] **--another-flag, -b**: another usage text +**--dir**="": (default: $PWD) + **--flag, --fl, -f**="": **--socket, -s**="": some 'usage' text (default: value) diff --git a/testdata/expected-tabular-markdown-full.md b/testdata/expected-tabular-markdown-full.md index 2828e84..f755d6d 100644 --- a/testdata/expected-tabular-markdown-full.md +++ b/testdata/expected-tabular-markdown-full.md @@ -19,6 +19,7 @@ Global flags: | `--socket="…"` (`-s`) | some 'usage' text | `value` | *none* | | `--flag="…"` (`--fl`, `-f`) | | | *none* | | `--another-flag` (`-b`) | another usage text | `false` | `EXAMPLE_VARIABLE_NAME` | +| `--dir="…"` | | `$PWD` | *none* | ### `config` command (aliases: `c`) From bdc82a039940654b32e70efba97dd7ccb3c8c932 Mon Sep 17 00:00:00 2001 From: Joshua Swick Date: Thu, 10 Apr 2025 20:46:08 -0600 Subject: [PATCH 3/4] more tests --- docs_test.go | 6 +----- testdata/expected-doc-full.man | 4 ++++ testdata/expected-doc-no-authors.md | 3 +++ testdata/expected-doc-no-commands.md | 3 +++ testdata/expected-doc-no-usagetext.md | 3 +++ testdata/expected-tabular-markdown-custom-app-path.md | 1 + 6 files changed, 15 insertions(+), 5 deletions(-) diff --git a/docs_test.go b/docs_test.go index 1393b19..ad2b409 100644 --- a/docs_test.go +++ b/docs_test.go @@ -3,7 +3,6 @@ package docs import ( "bytes" "embed" - "fmt" "io" "io/fs" "net/mail" @@ -189,10 +188,7 @@ func TestToTabularMarkdown(t *testing.T) { } func pwd() string { - pwd, err := os.Getwd() - if err != nil { - fmt.Println(err) - } + pwd, _ := os.Getwd() return pwd } diff --git a/testdata/expected-doc-full.man b/testdata/expected-doc-full.man index a2131f0..2e2adf0 100644 --- a/testdata/expected-doc-full.man +++ b/testdata/expected-doc-full.man @@ -15,6 +15,7 @@ greet .nf [--another-flag|-b] +[--dir]=[value] [--flag|--fl|-f]=[value] [--socket|-s]=[value] @@ -43,6 +44,9 @@ app [first_arg] [second_arg] .PP \fB--another-flag, -b\fP: another usage text +.PP +\fB--dir\fP="": (default: $PWD) + .PP \fB--flag, --fl, -f\fP="": diff --git a/testdata/expected-doc-no-authors.md b/testdata/expected-doc-no-authors.md index 80ff6a7..7094947 100644 --- a/testdata/expected-doc-no-authors.md +++ b/testdata/expected-doc-no-authors.md @@ -8,6 +8,7 @@ greet ``` [--another-flag|-b] +[--dir]=[value] [--flag|--fl|-f]=[value] [--socket|-s]=[value] ``` @@ -26,6 +27,8 @@ app [first_arg] [second_arg] **--another-flag, -b**: another usage text +**--dir**="": (default: $PWD) + **--flag, --fl, -f**="": **--socket, -s**="": some 'usage' text (default: value) diff --git a/testdata/expected-doc-no-commands.md b/testdata/expected-doc-no-commands.md index 4db2a37..5028cde 100644 --- a/testdata/expected-doc-no-commands.md +++ b/testdata/expected-doc-no-commands.md @@ -8,6 +8,7 @@ greet ``` [--another-flag|-b] +[--dir]=[value] [--flag|--fl|-f]=[value] [--socket|-s]=[value] ``` @@ -26,6 +27,8 @@ app [first_arg] [second_arg] **--another-flag, -b**: another usage text +**--dir**="": (default: $PWD) + **--flag, --fl, -f**="": **--socket, -s**="": some 'usage' text (default: value) diff --git a/testdata/expected-doc-no-usagetext.md b/testdata/expected-doc-no-usagetext.md index fab4ba1..ee30c25 100644 --- a/testdata/expected-doc-no-usagetext.md +++ b/testdata/expected-doc-no-usagetext.md @@ -8,6 +8,7 @@ greet ``` [--another-flag|-b] +[--dir]=[value] [--flag|--fl|-f]=[value] [--socket|-s]=[value] ``` @@ -26,6 +27,8 @@ greet [GLOBAL OPTIONS] [command [COMMAND OPTIONS]] [ARGUMENTS...] **--another-flag, -b**: another usage text +**--dir**="": (default: $PWD) + **--flag, --fl, -f**="": **--socket, -s**="": some 'usage' text (default: value) diff --git a/testdata/expected-tabular-markdown-custom-app-path.md b/testdata/expected-tabular-markdown-custom-app-path.md index 5aad2df..34925d9 100644 --- a/testdata/expected-tabular-markdown-custom-app-path.md +++ b/testdata/expected-tabular-markdown-custom-app-path.md @@ -19,6 +19,7 @@ Global flags: | `--socket="…"` (`-s`) | some 'usage' text | `value` | *none* | | `--flag="…"` (`--fl`, `-f`) | | | *none* | | `--another-flag` (`-b`) | another usage text | `false` | `EXAMPLE_VARIABLE_NAME` | +| `--dir="…"` | | `$PWD` | *none* | ### `config` command (aliases: `c`) From a376063f7019738ab7f450a80b1e0f2f64b76683 Mon Sep 17 00:00:00 2001 From: Joshua Swick Date: Fri, 11 Apr 2025 15:48:31 -0600 Subject: [PATCH 4/4] use different default test default ran into a different bug so im avoiding that --- docs_test.go | 2 +- testdata/expected-doc-full.man | 2 +- testdata/expected-doc-full.md | 2 +- testdata/expected-doc-no-authors.md | 2 +- testdata/expected-doc-no-commands.md | 2 +- testdata/expected-doc-no-usagetext.md | 2 +- testdata/expected-tabular-markdown-custom-app-path.md | 2 +- testdata/expected-tabular-markdown-full.md | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs_test.go b/docs_test.go index ad2b409..faaa470 100644 --- a/docs_test.go +++ b/docs_test.go @@ -65,7 +65,7 @@ func buildExtendedTestCommand() *cli.Command { &cli.StringFlag{ Name: "dir", Value: pwd(), - DefaultText: "$PWD", + DefaultText: "\".\"", }, }, Commands: []*cli.Command{{ diff --git a/testdata/expected-doc-full.man b/testdata/expected-doc-full.man index 2e2adf0..b927164 100644 --- a/testdata/expected-doc-full.man +++ b/testdata/expected-doc-full.man @@ -45,7 +45,7 @@ app [first_arg] [second_arg] \fB--another-flag, -b\fP: another usage text .PP -\fB--dir\fP="": (default: $PWD) +\fB--dir\fP="": (default: ".") .PP \fB--flag, --fl, -f\fP="": diff --git a/testdata/expected-doc-full.md b/testdata/expected-doc-full.md index 7094947..7f7e1cf 100644 --- a/testdata/expected-doc-full.md +++ b/testdata/expected-doc-full.md @@ -27,7 +27,7 @@ app [first_arg] [second_arg] **--another-flag, -b**: another usage text -**--dir**="": (default: $PWD) +**--dir**="": (default: ".") **--flag, --fl, -f**="": diff --git a/testdata/expected-doc-no-authors.md b/testdata/expected-doc-no-authors.md index 7094947..7f7e1cf 100644 --- a/testdata/expected-doc-no-authors.md +++ b/testdata/expected-doc-no-authors.md @@ -27,7 +27,7 @@ app [first_arg] [second_arg] **--another-flag, -b**: another usage text -**--dir**="": (default: $PWD) +**--dir**="": (default: ".") **--flag, --fl, -f**="": diff --git a/testdata/expected-doc-no-commands.md b/testdata/expected-doc-no-commands.md index 5028cde..afa7d1b 100644 --- a/testdata/expected-doc-no-commands.md +++ b/testdata/expected-doc-no-commands.md @@ -27,7 +27,7 @@ app [first_arg] [second_arg] **--another-flag, -b**: another usage text -**--dir**="": (default: $PWD) +**--dir**="": (default: ".") **--flag, --fl, -f**="": diff --git a/testdata/expected-doc-no-usagetext.md b/testdata/expected-doc-no-usagetext.md index ee30c25..b078b8b 100644 --- a/testdata/expected-doc-no-usagetext.md +++ b/testdata/expected-doc-no-usagetext.md @@ -27,7 +27,7 @@ greet [GLOBAL OPTIONS] [command [COMMAND OPTIONS]] [ARGUMENTS...] **--another-flag, -b**: another usage text -**--dir**="": (default: $PWD) +**--dir**="": (default: ".") **--flag, --fl, -f**="": diff --git a/testdata/expected-tabular-markdown-custom-app-path.md b/testdata/expected-tabular-markdown-custom-app-path.md index 34925d9..62b0a36 100644 --- a/testdata/expected-tabular-markdown-custom-app-path.md +++ b/testdata/expected-tabular-markdown-custom-app-path.md @@ -19,7 +19,7 @@ Global flags: | `--socket="…"` (`-s`) | some 'usage' text | `value` | *none* | | `--flag="…"` (`--fl`, `-f`) | | | *none* | | `--another-flag` (`-b`) | another usage text | `false` | `EXAMPLE_VARIABLE_NAME` | -| `--dir="…"` | | `$PWD` | *none* | +| `--dir="…"` | | `"."` | *none* | ### `config` command (aliases: `c`) diff --git a/testdata/expected-tabular-markdown-full.md b/testdata/expected-tabular-markdown-full.md index f755d6d..2a5b513 100644 --- a/testdata/expected-tabular-markdown-full.md +++ b/testdata/expected-tabular-markdown-full.md @@ -19,7 +19,7 @@ Global flags: | `--socket="…"` (`-s`) | some 'usage' text | `value` | *none* | | `--flag="…"` (`--fl`, `-f`) | | | *none* | | `--another-flag` (`-b`) | another usage text | `false` | `EXAMPLE_VARIABLE_NAME` | -| `--dir="…"` | | `$PWD` | *none* | +| `--dir="…"` | | `"."` | *none* | ### `config` command (aliases: `c`)