@@ -93,10 +93,10 @@ func (c commander) run(flagSet *flag.FlagSet, cmdName, usageText string, args []
9393 // If the first arg is this registered commmand in the loop, then try and run it, then exit
9494
9595 // Set up the usage function for this subcommand
96- // If the subcommand has a usageFunc defined, then use it
9796 if cmd .usageFunc != nil {
97+ // If the subcommand has a usageFunc defined, then use it
9898 cmd .flagSet .Usage = cmd .usageFunc
99- } else {
99+ } else {
100100 // If the subcommand does not have a usageFunc defined,
101101 // then define a simple default one,
102102 // using the list of flags defined in the subcommand, and their description strings
@@ -163,7 +163,23 @@ func (c commander) run(flagSet *flag.FlagSet, cmdName, usageText string, args []
163163 os .Exit (0 )
164164 }
165165
166- // If the first arg didn't match any registered commands, print errors and exit the application
167- log .Printf ("%s: unknown subcommand %q" , cmdName , name )
168- log .Fatalf ("Run '%s help' for usage." , cmdName )
166+ // To make it after the big loop, that means name didn't match any registered commands
167+ if name != "" {
168+ log .Printf ("%s: unknown command %q" , cmdName , name )
169+ flagSet .Usage ()
170+ os .Exit (2 )
171+ }
172+
173+ // Special case to handle --help usage text for src command
174+ if helpRequested {
175+ // Set output to stdout, for usage / helper text printed for the --help flag (flag package defaults to stderr)
176+ flagSet .SetOutput (os .Stdout )
177+ flagSet .Usage ()
178+ os .Exit (0 )
179+ }
180+
181+ // Special case to handle src command with no args
182+ flagSet .Usage ()
183+ os .Exit (2 )
184+
169185}
0 commit comments