Skip to content

Commit 787fa6f

Browse files
Command now uses the new usage mechanism
1 parent 6f19d1f commit 787fa6f

3 files changed

Lines changed: 41 additions & 24 deletions

File tree

command.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -583,14 +583,12 @@ func writeArgumentsSection(cmd *Command, s *strings.Builder) error {
583583
tw := style.Tabwriter(s)
584584

585585
for _, arg := range cmd.args {
586-
switch arg.Default() {
587-
case "":
588-
// It's required
589-
fmt.Fprintf(tw, " %s\t%s\t%s\t[required]\n", style.Bold.Text(arg.Name()), arg.Type(), arg.Usage())
590-
default:
591-
// It has a default
592-
fmt.Fprintf(tw, " %s\t%s\t%s\t[default %s]\n", style.Bold.Text(arg.Name()), arg.Type(), arg.Usage(), arg.Default())
586+
line := fmt.Sprintf(" %s\t%s\t%s\t[required]", style.Bold.Text(arg.Name()), arg.Type(), arg.Usage())
587+
if arg.Default() != "" {
588+
line = fmt.Sprintf(" %s\t%s\t%s\t[default %s]", style.Bold.Text(arg.Name()), arg.Type(), arg.Usage(), arg.Default())
593589
}
590+
591+
fmt.Fprintln(tw, line)
594592
}
595593

596594
if err := tw.Flush(); err != nil {
@@ -669,14 +667,25 @@ func writeFlags(cmd *Command, s *strings.Builder) error {
669667
shorthand = "N/A"
670668
}
671669

672-
fmt.Fprintf(
673-
tw,
674-
" %s\t--%s\t%s\t%s\n",
675-
style.Bold.Text(shorthand),
670+
line := fmt.Sprintf(
671+
" %s\t--%s\t%s\t%s\t", style.Bold.Text(shorthand),
676672
style.Bold.Text(name),
677673
fl.Type(),
678674
fl.Usage(),
679675
)
676+
677+
if fl.Default() != "" {
678+
line = fmt.Sprintf(
679+
" %s\t--%s\t%s\t%s\t[default: %s]",
680+
style.Bold.Text(shorthand),
681+
style.Bold.Text(name),
682+
fl.Type(),
683+
fl.Usage(),
684+
fl.Default(),
685+
)
686+
}
687+
688+
fmt.Fprintln(tw, line)
680689
}
681690

682691
if err := tw.Flush(); err != nil {

internal/flag/flag.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,6 @@ func New[T flag.Flaggable](p *T, name string, short rune, usage string, config C
5555
short: short,
5656
}
5757

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-
7158
return flag, nil
7259
}
7360

@@ -87,6 +74,21 @@ func (f Flag[T]) Usage() string {
8774
return f.usage
8875
}
8976

77+
// Default returns the default value for the flag, as a string.
78+
//
79+
// If the flag's default is unset (i.e. the zero value for its type),
80+
// an empty string is returned.
81+
func (f Flag[T]) Default() string {
82+
// Special case a --help flag, because if we didn't, when you call --help
83+
// it would show up with a default of true because you've passed it
84+
// so it's value is true here
85+
if isZeroIsh(*f.value) || f.name == "help" {
86+
return ""
87+
}
88+
89+
return f.String()
90+
}
91+
9092
// NoArgValue returns a string representation of value the flag should hold
9193
// when it is given no arguments on the command line. For example a boolean flag
9294
// --delete, when passed without arguments implies --delete true.

internal/flag/value.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ type Value interface {
1414
// String returns the stored value of a flag as a string.
1515
String() string
1616

17+
// Default return the default value of a flag as a string.
18+
//
19+
// If the flag's default is the zero value for it's type,
20+
// an empty string is returned.
21+
Default() string
22+
1723
// NoArgValue returns astring representation of the value of the flag when no
1824
// args are passed (e.g --bool implies --bool true).
1925
NoArgValue() string

0 commit comments

Comments
 (0)