Skip to content

Commit 69089c2

Browse files
use DocGenerationFlag.GetDefaultText
1 parent a376063 commit 69089c2

9 files changed

+16
-34
lines changed

docs.go

Lines changed: 1 addition & 20 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"
@@ -564,23 +563,5 @@ func getFlagDefaultValue(f cli.DocGenerationFlag) string {
564563
if !f.TakesValue() {
565564
return ""
566565
}
567-
568-
var ref = reflect.ValueOf(f)
569-
if ref.Kind() == reflect.Ptr {
570-
ref = ref.Elem()
571-
}
572-
573-
if ref.Kind() != reflect.Struct {
574-
return ""
575-
}
576-
577-
if defaultTextVal := ref.FieldByName("DefaultText"); defaultTextVal.IsValid() && defaultTextVal.Kind() == reflect.String && defaultTextVal.String() != "" {
578-
return defaultTextVal.String()
579-
}
580-
581-
if val := ref.FieldByName("Value"); val.IsValid() && val.Type().Kind() != reflect.Bool {
582-
return fmt.Sprintf("%v", val.Interface())
583-
}
584-
585-
return ""
566+
return f.GetDefaultText()
586567
}

docs_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ func expectFileContent(t *testing.T, file, got string) {
2424
r := require.New(t)
2525
r.NoError(err)
2626
r.Equal(
27-
string(normalizeNewlines([]byte(got))),
2827
string(normalizeNewlines(data)),
28+
string(normalizeNewlines([]byte(got))),
2929
)
3030
}
3131

@@ -45,11 +45,12 @@ func buildExtendedTestCommand() *cli.Command {
4545
Name: "greet",
4646
Flags: []cli.Flag{
4747
&cli.StringFlag{
48-
Name: "socket",
49-
Aliases: []string{"s"},
50-
Usage: "some 'usage' text",
51-
Value: "value",
52-
TakesFile: true,
48+
Name: "socket",
49+
Aliases: []string{"s"},
50+
Usage: "some 'usage' text",
51+
Value: "value",
52+
DefaultText: "value",
53+
TakesFile: true,
5354
},
5455
&cli.StringFlag{Name: "flag", Aliases: []string{"fl", "f"}},
5556
&cli.BoolFlag{
@@ -65,7 +66,7 @@ func buildExtendedTestCommand() *cli.Command {
6566
&cli.StringFlag{
6667
Name: "dir",
6768
Value: pwd(),
68-
DefaultText: "\".\"",
69+
DefaultText: ".",
6970
},
7071
},
7172
Commands: []*cli.Command{{

testdata/expected-doc-full.man

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ app [first_arg] [second_arg]
4545
\fB--another-flag, -b\fP: another usage text
4646

4747
.PP
48-
\fB--dir\fP="": (default: ".")
48+
\fB--dir\fP="": (default: .)
4949

5050
.PP
5151
\fB--flag, --fl, -f\fP="":

testdata/expected-doc-full.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ app [first_arg] [second_arg]
2727

2828
**--another-flag, -b**: another usage text
2929

30-
**--dir**="": (default: ".")
30+
**--dir**="": (default: .)
3131

3232
**--flag, --fl, -f**="":
3333

testdata/expected-doc-no-authors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ app [first_arg] [second_arg]
2727

2828
**--another-flag, -b**: another usage text
2929

30-
**--dir**="": (default: ".")
30+
**--dir**="": (default: .)
3131

3232
**--flag, --fl, -f**="":
3333

testdata/expected-doc-no-commands.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ app [first_arg] [second_arg]
2727

2828
**--another-flag, -b**: another usage text
2929

30-
**--dir**="": (default: ".")
30+
**--dir**="": (default: .)
3131

3232
**--flag, --fl, -f**="":
3333

testdata/expected-doc-no-usagetext.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ greet [GLOBAL OPTIONS] [command [COMMAND OPTIONS]] [ARGUMENTS...]
2727

2828
**--another-flag, -b**: another usage text
2929

30-
**--dir**="": (default: ".")
30+
**--dir**="": (default: .)
3131

3232
**--flag, --fl, -f**="":
3333

testdata/expected-tabular-markdown-custom-app-path.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Global flags:
1919
| `--socket="…"` (`-s`) | some 'usage' text | `value` | *none* |
2020
| `--flag="…"` (`--fl`, `-f`) | | | *none* |
2121
| `--another-flag` (`-b`) | another usage text | `false` | `EXAMPLE_VARIABLE_NAME` |
22-
| `--dir="…"` | | `"."` | *none* |
22+
| `--dir="…"` | | `.` | *none* |
2323

2424
### `config` command (aliases: `c`)
2525

testdata/expected-tabular-markdown-full.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Global flags:
1919
| `--socket="…"` (`-s`) | some 'usage' text | `value` | *none* |
2020
| `--flag="…"` (`--fl`, `-f`) | | | *none* |
2121
| `--another-flag` (`-b`) | another usage text | `false` | `EXAMPLE_VARIABLE_NAME` |
22-
| `--dir="…"` | | `"."` | *none* |
22+
| `--dir="…"` | | `.` | *none* |
2323

2424
### `config` command (aliases: `c`)
2525

0 commit comments

Comments
 (0)