@@ -17,8 +17,10 @@ import (
1717)
1818
1919const (
20- helpBufferSize = 1024 // helpBufferSize is sufficient to hold most command --help text.
21- versionBufferSize = 256 // versionBufferSize is sufficient to hold all the --version text.
20+ helpBufferSize = 1024 // helpBufferSize is sufficient to hold most command --help text.
21+ versionBufferSize = 256 // versionBufferSize is sufficient to hold all the --version text.
22+ defaultVersion = "dev" // defaultVersion is the version shown in --version when the user has not provided one.
23+ defaultShort = "A placeholder for something cool" // defaultShort is the default value for cli.Short.
2224)
2325
2426// Builder is a function that constructs and returns a [Command], it makes constructing
@@ -51,8 +53,8 @@ func New(name string, options ...Option) (*Command, error) {
5153 stderr : os .Stderr ,
5254 args : os .Args [1 :],
5355 name : name ,
54- version : "dev" ,
55- short : "A placeholder for something cool" ,
56+ version : defaultVersion ,
57+ short : defaultShort ,
5658 argValidator : AnyArgs (),
5759 }
5860
@@ -218,7 +220,7 @@ func (cmd *Command) Execute() error {
218220 }
219221
220222 if helpCalled {
221- if err := defaultHelp (cmd ); err != nil {
223+ if err := showHelp (cmd ); err != nil {
222224 return fmt .Errorf ("help function returned an error: %w" , err )
223225 }
224226
@@ -234,8 +236,8 @@ func (cmd *Command) Execute() error {
234236 }
235237
236238 if versionCalled {
237- if err := defaultVersion (cmd ); err != nil {
238- return fmt .Errorf ("version function returned an error : %w" , err )
239+ if err := showVersion (cmd ); err != nil {
240+ return fmt .Errorf ("could not show version : %w" , err )
239241 }
240242
241243 return nil
@@ -279,7 +281,7 @@ func (cmd *Command) Execute() error {
279281
280282 // The only way we get here is if the command has subcommands defined but got no arguments given to it
281283 // so just show the usage and error
282- if err := defaultHelp (cmd ); err != nil {
284+ if err := showHelp (cmd ); err != nil {
283285 return err
284286 }
285287
@@ -487,8 +489,8 @@ func stripFlags(cmd *Command, args []string) []string {
487489 return argsWithoutFlags
488490}
489491
490- // defaultHelp is the default for a command's helpFunc.
491- func defaultHelp (cmd * Command ) error {
492+ // showHelp is the default for a command's helpFunc.
493+ func showHelp (cmd * Command ) error {
492494 if cmd == nil {
493495 return errors .New ("defaultHelp called on a nil Command" )
494496 }
@@ -580,6 +582,8 @@ func defaultHelp(cmd *Command) error {
580582 writeFooter (cmd , s )
581583 }
582584
585+ // Note: It's important to use cmd.Stderr() here over cmd.stderr
586+ // as it resolves to the root's stderr
583587 fmt .Fprint (cmd .Stderr (), s .String ())
584588
585589 return nil
@@ -700,15 +704,22 @@ func writeFooter(cmd *Command, s *strings.Builder) {
700704 s .WriteByte ('\n' )
701705}
702706
703- // defaultVersion is the default for a command's versionFunc .
704- func defaultVersion (cmd * Command ) error {
707+ // showVersion is the default implementation of the --version flag .
708+ func showVersion (cmd * Command ) error {
705709 if cmd == nil {
706710 return errors .New ("defaultVersion called on a nil Command" )
707711 }
708712
713+ name := cmd .name // Incase we need to show the subcommand name
714+
715+ if cmd .version == defaultVersion {
716+ // User has not set a version for this command, so we show the root version info
717+ cmd = cmd .root ()
718+ }
719+
709720 s := & strings.Builder {}
710721 s .Grow (versionBufferSize )
711- s .WriteString (style .Title .Text (cmd . name ))
722+ s .WriteString (style .Title .Text (name ))
712723 s .WriteString ("\n \n " )
713724 s .WriteString (style .Bold .Text ("Version:" ))
714725 s .WriteString (" " )
@@ -729,7 +740,9 @@ func defaultVersion(cmd *Command) error {
729740 s .WriteString ("\n " )
730741 }
731742
732- fmt .Fprint (cmd .stderr , s .String ())
743+ // Note: It's important to use cmd.Stderr() here over cmd.stderr
744+ // as it resolves to the root's stderr
745+ fmt .Fprint (cmd .Stderr (), s .String ())
733746
734747 return nil
735748}
0 commit comments