Skip to content

Commit df1d75f

Browse files
Always align flag defaults in usage text (#187)
1 parent c47181d commit df1d75f

16 files changed

Lines changed: 51 additions & 46 deletions

command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ func stripFlags(cmd *Command, args []string) []string {
459459
// showHelp is the default for a command's helpFunc.
460460
func showHelp(cmd *Command) error {
461461
if cmd == nil {
462-
return errors.New("defaultHelp called on a nil Command")
462+
return errors.New("showHelp called on a nil Command")
463463
}
464464

465465
usage, err := cmd.flagSet().Usage()

internal/flag/flag.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,26 @@ func New[T flag.Flaggable](p *T, name string, short rune, usage string, config C
4848

4949
*p = config.DefaultValue
5050

51-
// If the default value is not the zero value for the type, it is treated as
52-
// significant and shown to the user
53-
if !isZeroIsh(*p) {
54-
// \t so that defaults get aligned by tabwriter when the command
55-
// dumps the flags
56-
usage += fmt.Sprintf("\t[default: %v]", *p)
57-
}
58-
5951
flag := Flag[T]{
6052
value: p,
6153
name: name,
6254
usage: usage,
6355
short: short,
6456
}
6557

58+
// TODO(@FollowTheProcess): This needs to live in command.go and we should iterate over the flagset
59+
// adding the values to the tabwriter as we go rather than relying on the flagset.Usage() method
60+
// to provide *all* the usage
61+
62+
// If the default value is not the zero value for the type, it is treated as
63+
// significant and shown to the user
64+
flag.usage += "\t"
65+
if !isZeroIsh(*p) {
66+
// \t so that defaults get aligned by tabwriter when the command
67+
// dumps the flags
68+
flag.usage += fmt.Sprintf("[default: %s]", flag.String())
69+
}
70+
6671
return flag, nil
6772
}
6873

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
-c --count int Count things
2-
-h --help bool Show help for test
3-
-t --thing string Name the thing
4-
N/A --update bool Update something
5-
-V --version bool Show version info for test
1+
-c --count int Count things
2+
-h --help bool Show help for test
3+
-t --thing string Name the thing
4+
N/A --update bool Update something
5+
-V --version bool Show version info for test
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
-h --help bool Show help for test
2-
N/A --update bool Update something
3-
-V --version bool Show version info for test
1+
-h --help bool Show help for test
2+
N/A --update bool Update something
3+
-V --version bool Show version info for test
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
-h --help bool Show help for test
2-
-V --version bool Show version info for test
1+
-h --help bool Show help for test
2+
-V --version bool Show version info for test

testdata/snapshots/TestHelp/default_long.snap.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ Usage: test [OPTIONS] ARGS...
44

55
Options:
66

7-
-h --help bool Show help for test
8-
-V --version bool Show version info for test
7+
-h --help bool Show help for test
8+
-V --version bool Show version info for test

testdata/snapshots/TestHelp/default_short.snap.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ Usage: test [OPTIONS] ARGS...
44

55
Options:
66

7-
-h --help bool Show help for test
8-
-V --version bool Show version info for test
7+
-h --help bool Show help for test
8+
-V --version bool Show version info for test

testdata/snapshots/TestHelp/full_description_strip_whitespace.snap.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ Usage: test [OPTIONS] ARGS...
66

77
Options:
88

9-
-h --help bool Show help for test
10-
-V --version bool Show version info for test
9+
-h --help bool Show help for test
10+
-V --version bool Show version info for test

testdata/snapshots/TestHelp/subcommands_different_lengths.snap.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Commands:
1212

1313
Options:
1414

15-
-h --help bool Show help for test
16-
-V --version bool Show version info for test
15+
-h --help bool Show help for test
16+
-V --version bool Show version info for test
1717

1818
Use "test [command] -h/--help" for more information about a command.

testdata/snapshots/TestHelp/with_examples.snap.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ Examples:
1212

1313
Options:
1414

15-
-h --help bool Show help for test
16-
-V --version bool Show version info for test
15+
-h --help bool Show help for test
16+
-V --version bool Show version info for test

0 commit comments

Comments
 (0)