Skip to content

Commit 565f23b

Browse files
committed
get default value of bool flags always too
1 parent c12711c commit 565f23b

File tree

7 files changed

+30
-31
lines changed

7 files changed

+30
-31
lines changed

docs.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -570,10 +570,6 @@ func (tabularTemplate) Prettify(s string) string {
570570
// tries to obtain it via reflection (the [cli.FlagBase] still has a Value field).
571571
// It also checks if there is a DefaultText and if set returns it.
572572
func getFlagDefaultValue(f cli.DocGenerationFlag) (value, text string) {
573-
if !f.TakesValue() {
574-
return "", ""
575-
}
576-
577573
// GetDefaultText also returns GetValue so we have to use reflection
578574
if ref := reflect.ValueOf(f); ref.Kind() == reflect.Ptr && ref.Elem().Kind() == reflect.Struct {
579575
if val := ref.Elem().FieldByName("DefaultText"); val.IsValid() && val.Type().Kind() == reflect.String {
@@ -583,8 +579,11 @@ func getFlagDefaultValue(f cli.DocGenerationFlag) (value, text string) {
583579
}
584580
}
585581

586-
if boolFlag, isBool := f.(*cli.BoolFlag); isBool {
587-
return strconv.FormatBool(boolFlag.Value), ""
582+
if !f.TakesValue() {
583+
if boolFlag, isBool := f.(*cli.BoolFlag); isBool {
584+
return strconv.FormatBool(boolFlag.Value), ""
585+
}
586+
return "", ""
588587
}
589588

590589
return f.GetValue(), ""

testdata/expected-doc-full.man

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ app [first_arg] [second_arg]
4242

4343
.SH GLOBAL OPTIONS
4444
.PP
45-
\fB--another-flag, -b\fP: another usage text
45+
\fB--another-flag, -b\fP: another usage text (default: false)
4646

4747
.PP
4848
\fB--flag, --fl, -f\fP="":
@@ -60,7 +60,7 @@ app [first_arg] [second_arg]
6060
another usage test
6161

6262
.PP
63-
\fB--another-flag, -b\fP: another usage text
63+
\fB--another-flag, -b\fP: another usage text (default: false)
6464

6565
.PP
6666
\fB--flag, --fl, -f\fP="":
@@ -70,7 +70,7 @@ another usage test
7070
another usage test
7171

7272
.PP
73-
\fB--sub-command-flag, -s\fP: some usage text
73+
\fB--sub-command-flag, -s\fP: some usage text (default: false)
7474

7575
.PP
7676
\fB--sub-flag, --sub-fl, -s\fP="":
@@ -103,7 +103,7 @@ Should be a part of the same code block
103103
.RE
104104

105105
.PP
106-
\fB--another-flag, -b\fP: another usage text
106+
\fB--another-flag, -b\fP: another usage text (default: false)
107107

108108
.PP
109109
\fB--flag, --fl, -f\fP="":
@@ -121,4 +121,4 @@ Single line of UsageText
121121
.RE
122122

123123
.PP
124-
\fB--sub-command-flag, -s\fP: some usage text
124+
\fB--sub-command-flag, -s\fP: some usage text (default: false)

testdata/expected-doc-full.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ app [first_arg] [second_arg]
2525

2626
# GLOBAL OPTIONS
2727

28-
**--another-flag, -b**: another usage text
28+
**--another-flag, -b**: another usage text (default: false)
2929

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

@@ -40,15 +40,15 @@ app [first_arg] [second_arg]
4040

4141
another usage test
4242

43-
**--another-flag, -b**: another usage text
43+
**--another-flag, -b**: another usage text (default: false)
4444

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

4747
### sub-config, s, ss
4848

4949
another usage test
5050

51-
**--sub-command-flag, -s**: some usage text
51+
**--sub-command-flag, -s**: some usage text (default: false)
5252

5353
**--sub-flag, --sub-fl, -s**="":
5454

@@ -74,7 +74,7 @@ standard usage text
7474

7575
Should be a part of the same code block
7676

77-
**--another-flag, -b**: another usage text
77+
**--another-flag, -b**: another usage text (default: false)
7878

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

@@ -84,4 +84,4 @@ standard usage text
8484

8585
>Single line of UsageText
8686
87-
**--sub-command-flag, -s**: some usage text
87+
**--sub-command-flag, -s**: some usage text (default: false)

testdata/expected-doc-no-authors.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ app [first_arg] [second_arg]
2525

2626
# GLOBAL OPTIONS
2727

28-
**--another-flag, -b**: another usage text
28+
**--another-flag, -b**: another usage text (default: false)
2929

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

@@ -40,15 +40,15 @@ app [first_arg] [second_arg]
4040

4141
another usage test
4242

43-
**--another-flag, -b**: another usage text
43+
**--another-flag, -b**: another usage text (default: false)
4444

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

4747
### sub-config, s, ss
4848

4949
another usage test
5050

51-
**--sub-command-flag, -s**: some usage text
51+
**--sub-command-flag, -s**: some usage text (default: false)
5252

5353
**--sub-flag, --sub-fl, -s**="":
5454

@@ -74,7 +74,7 @@ standard usage text
7474

7575
Should be a part of the same code block
7676

77-
**--another-flag, -b**: another usage text
77+
**--another-flag, -b**: another usage text (default: false)
7878

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

@@ -84,4 +84,4 @@ standard usage text
8484

8585
>Single line of UsageText
8686
87-
**--sub-command-flag, -s**: some usage text
87+
**--sub-command-flag, -s**: some usage text (default: false)

testdata/expected-doc-no-commands.md

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

2626
# GLOBAL OPTIONS
2727

28-
**--another-flag, -b**: another usage text
28+
**--another-flag, -b**: another usage text (default: false)
2929

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

testdata/expected-doc-no-flags.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ app [first_arg] [second_arg]
2222

2323
another usage test
2424

25-
**--another-flag, -b**: another usage text
25+
**--another-flag, -b**: another usage text (default: false)
2626

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

2929
### sub-config, s, ss
3030

3131
another usage test
3232

33-
**--sub-command-flag, -s**: some usage text
33+
**--sub-command-flag, -s**: some usage text (default: false)
3434

3535
**--sub-flag, --sub-fl, -s**="":
3636

@@ -56,7 +56,7 @@ standard usage text
5656

5757
Should be a part of the same code block
5858

59-
**--another-flag, -b**: another usage text
59+
**--another-flag, -b**: another usage text (default: false)
6060

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

@@ -66,4 +66,4 @@ standard usage text
6666

6767
>Single line of UsageText
6868
69-
**--sub-command-flag, -s**: some usage text
69+
**--sub-command-flag, -s**: some usage text (default: false)

testdata/expected-doc-no-usagetext.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ greet [GLOBAL OPTIONS] [command [COMMAND OPTIONS]] [ARGUMENTS...]
2525

2626
# GLOBAL OPTIONS
2727

28-
**--another-flag, -b**: another usage text
28+
**--another-flag, -b**: another usage text (default: false)
2929

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

@@ -40,15 +40,15 @@ greet [GLOBAL OPTIONS] [command [COMMAND OPTIONS]] [ARGUMENTS...]
4040

4141
another usage test
4242

43-
**--another-flag, -b**: another usage text
43+
**--another-flag, -b**: another usage text (default: false)
4444

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

4747
### sub-config, s, ss
4848

4949
another usage test
5050

51-
**--sub-command-flag, -s**: some usage text
51+
**--sub-command-flag, -s**: some usage text (default: false)
5252

5353
**--sub-flag, --sub-fl, -s**="":
5454

@@ -74,7 +74,7 @@ standard usage text
7474

7575
Should be a part of the same code block
7676

77-
**--another-flag, -b**: another usage text
77+
**--another-flag, -b**: another usage text (default: false)
7878

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

@@ -84,4 +84,4 @@ standard usage text
8484

8585
>Single line of UsageText
8686
87-
**--sub-command-flag, -s**: some usage text
87+
**--sub-command-flag, -s**: some usage text (default: false)

0 commit comments

Comments
 (0)