Skip to content

Prioritize defaultText over value in docs #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,21 +565,19 @@ func getFlagDefaultValue(f cli.DocGenerationFlag) string {
return ""
}

if v, ok := f.(interface{ GetValue() string }); ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joshuaswickirl i'm having a hard time wrapping my head around this. You say that DocGenerationFlag has removed GetValue ?? It's still there. So I'm confused why we need all this in the first place. Moreover there is no need to use reflection here. You should be able to use be existing functions to achieve the desired result

return v.GetValue()
}

var ref = reflect.ValueOf(f)
if ref.Kind() != reflect.Ptr {
return ""
} else {
if ref.Kind() == reflect.Ptr {
ref = ref.Elem()
}

if ref.Kind() != reflect.Struct {
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())
}
Expand Down
10 changes: 10 additions & 0 deletions docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func buildExtendedTestCommand() *cli.Command {
Name: "hidden-flag",
Hidden: true,
},
&cli.StringFlag{
Name: "dir",
Value: pwd(),
DefaultText: "\".\"",
},
},
Commands: []*cli.Command{{
Aliases: []string{"c"},
Expand Down Expand Up @@ -182,6 +187,11 @@ func TestToTabularMarkdown(t *testing.T) {
})
}

func pwd() string {
pwd, _ := os.Getwd()
return pwd
}

func TestToTabularMarkdownFailed(t *testing.T) {
tpl := MarkdownTabularDocTemplate
t.Cleanup(func() { MarkdownTabularDocTemplate = tpl })
Expand Down
4 changes: 4 additions & 0 deletions testdata/expected-doc-full.man
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ greet

.nf
[--another-flag|-b]
[--dir]=[value]
[--flag|--fl|-f]=[value]
[--socket|-s]=[value]

Expand Down Expand Up @@ -43,6 +44,9 @@ app [first_arg] [second_arg]
.PP
\fB--another-flag, -b\fP: another usage text

.PP
\fB--dir\fP="": (default: ".")

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

Expand Down
3 changes: 3 additions & 0 deletions testdata/expected-doc-full.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ greet

```
[--another-flag|-b]
[--dir]=[value]
[--flag|--fl|-f]=[value]
[--socket|-s]=[value]
```
Expand All @@ -26,6 +27,8 @@ app [first_arg] [second_arg]

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

**--dir**="": (default: ".")

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

**--socket, -s**="": some 'usage' text (default: value)
Expand Down
3 changes: 3 additions & 0 deletions testdata/expected-doc-no-authors.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ greet

```
[--another-flag|-b]
[--dir]=[value]
[--flag|--fl|-f]=[value]
[--socket|-s]=[value]
```
Expand All @@ -26,6 +27,8 @@ app [first_arg] [second_arg]

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

**--dir**="": (default: ".")

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

**--socket, -s**="": some 'usage' text (default: value)
Expand Down
3 changes: 3 additions & 0 deletions testdata/expected-doc-no-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ greet

```
[--another-flag|-b]
[--dir]=[value]
[--flag|--fl|-f]=[value]
[--socket|-s]=[value]
```
Expand All @@ -26,6 +27,8 @@ app [first_arg] [second_arg]

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

**--dir**="": (default: ".")

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

**--socket, -s**="": some 'usage' text (default: value)
Expand Down
3 changes: 3 additions & 0 deletions testdata/expected-doc-no-usagetext.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ greet

```
[--another-flag|-b]
[--dir]=[value]
[--flag|--fl|-f]=[value]
[--socket|-s]=[value]
```
Expand All @@ -26,6 +27,8 @@ greet [GLOBAL OPTIONS] [command [COMMAND OPTIONS]] [ARGUMENTS...]

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

**--dir**="": (default: ".")

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

**--socket, -s**="": some 'usage' text (default: value)
Expand Down
1 change: 1 addition & 0 deletions testdata/expected-tabular-markdown-custom-app-path.md
Original file line number Diff line number Diff line change
Expand Up @@ -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="…"` | | `"."` | *none* |

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

Expand Down
1 change: 1 addition & 0 deletions testdata/expected-tabular-markdown-full.md
Original file line number Diff line number Diff line change
Expand Up @@ -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="…"` | | `"."` | *none* |

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

Expand Down
Loading