@@ -21,6 +21,7 @@ import (
21
21
"context"
22
22
"errors"
23
23
"fmt"
24
+ "github.com/leonelquinteros/gotext"
24
25
"io"
25
26
"os"
26
27
"path/filepath"
@@ -44,6 +45,12 @@ type Group struct {
44
45
Title string
45
46
}
46
47
48
+ // CommandUsageTemplateData is the data passed to the template of command usage
49
+ type CommandUsageTemplateData struct {
50
+ * Command
51
+ I18n * i18nCommandGlossary
52
+ }
53
+
47
54
// Command is just that, a command for your application.
48
55
// E.g. 'go run ...' - 'run' is the command. Cobra requires
49
56
// you to define the usage and description as part of your command
@@ -432,7 +439,11 @@ func (c *Command) UsageFunc() (f func(*Command) error) {
432
439
}
433
440
return func (c * Command ) error {
434
441
c .mergePersistentFlags ()
435
- err := tmpl (c .OutOrStderr (), c .UsageTemplate (), c )
442
+ data := CommandUsageTemplateData {
443
+ Command : c ,
444
+ I18n : getCommandGlossary (),
445
+ }
446
+ err := tmpl (c .OutOrStderr (), c .UsageTemplate (), data )
436
447
if err != nil {
437
448
c .PrintErrln (err )
438
449
}
@@ -549,35 +560,35 @@ func (c *Command) UsageTemplate() string {
549
560
if c .HasParent () {
550
561
return c .parent .UsageTemplate ()
551
562
}
552
- return `Usage :{{if .Runnable}}
563
+ return `{{.I18n.SectionUsage}} :{{if .Runnable}}
553
564
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
554
565
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
555
566
556
- Aliases :
567
+ {{.I18n.SectionAliases}} :
557
568
{{.NameAndAliases}}{{end}}{{if .HasExample}}
558
569
559
- Examples :
570
+ {{.I18n.SectionExamples}} :
560
571
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}{{$cmds := .Commands}}{{if eq (len .Groups) 0}}
561
572
562
- Available Commands :{{range $cmds}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
573
+ {{.I18n.SectionAvailableCommands}} :{{range $cmds}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
563
574
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{else}}{{range $group := .Groups}}
564
575
565
576
{{.Title}}{{range $cmds}}{{if (and (eq .GroupID $group.ID) (or .IsAvailableCommand (eq .Name "help")))}}
566
577
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if not .AllChildCommandsHaveGroup}}
567
578
568
- Additional Commands :{{range $cmds}}{{if (and (eq .GroupID "") (or .IsAvailableCommand (eq .Name "help")))}}
579
+ {{.I18n.SectionAdditionalCommands}} :{{range $cmds}}{{if (and (eq .GroupID "") (or .IsAvailableCommand (eq .Name "help")))}}
569
580
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
570
581
571
- Flags :
582
+ {{.I18n.SectionFlags}} :
572
583
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
573
584
574
- Global Flags :
585
+ {{.I18n.SectionGlobalFlags}} :
575
586
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
576
587
577
- Additional help topics :{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
588
+ {{.I18n.SectionAdditionalHelpTopics}} :{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
578
589
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
579
590
580
- Use "{{.CommandPath}} [command] --help" for more information about a command .{{end}}
591
+ {{.I18n. Use}} "{{.CommandPath}} [command] --help" {{.I18n.ForInfoAboutCommand}} .{{end}}
581
592
`
582
593
}
583
594
@@ -756,7 +767,7 @@ func (c *Command) findSuggestions(arg string) string {
756
767
}
757
768
var sb strings.Builder
758
769
if suggestions := c .SuggestionsFor (arg ); len (suggestions ) > 0 {
759
- sb .WriteString ("\n \n Did you mean this? \n " )
770
+ sb .WriteString ("\n \n " + gotext . Get ( "DidYouMeanThis" ) + " \n " )
760
771
for _ , s := range suggestions {
761
772
_ , _ = fmt .Fprintf (& sb , "\t %v\n " , s )
762
773
}
@@ -877,7 +888,7 @@ func (c *Command) execute(a []string) (err error) {
877
888
}
878
889
879
890
if len (c .Deprecated ) > 0 {
880
- c .Printf ("Command %q is deprecated, %s \n " , c .Name (), c .Deprecated )
891
+ c .Printf (gotext . Get ( "CommandDeprecatedWarning" ) + " \n " , c .Name (), c .Deprecated )
881
892
}
882
893
883
894
// initialize help and version flag at the last point possible to allow for user
@@ -1096,7 +1107,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
1096
1107
}
1097
1108
if ! c .SilenceErrors {
1098
1109
c .PrintErrln (c .ErrPrefix (), err .Error ())
1099
- c .PrintErrf ("Run '%v --help' for usage. \n " , c .CommandPath ())
1110
+ c .PrintErrf (gotext . Get ( "RunHelpTip" ) + " \n " , c .CommandPath ())
1100
1111
}
1101
1112
return c , err
1102
1113
}
@@ -1162,7 +1173,7 @@ func (c *Command) ValidateRequiredFlags() error {
1162
1173
})
1163
1174
1164
1175
if len (missingFlagNames ) > 0 {
1165
- return fmt .Errorf (`required flag(s) "%s" not set` , strings .Join (missingFlagNames , `", "` ))
1176
+ return fmt .Errorf (gotext . GetN ( "FlagNotSetError" , "FlagNotSetErrorPlural" , len ( missingFlagNames )) , strings .Join (missingFlagNames , `", "` ))
1166
1177
}
1167
1178
return nil
1168
1179
}
@@ -1186,9 +1197,9 @@ func (c *Command) checkCommandGroups() {
1186
1197
func (c * Command ) InitDefaultHelpFlag () {
1187
1198
c .mergePersistentFlags ()
1188
1199
if c .Flags ().Lookup ("help" ) == nil {
1189
- usage := "help for "
1200
+ usage := gotext . Get ( "HelpFor" ) + " "
1190
1201
if c .Name () == "" {
1191
- usage += "this command"
1202
+ usage += gotext . Get ( "ThisCommand" )
1192
1203
} else {
1193
1204
usage += c .Name ()
1194
1205
}
@@ -1208,9 +1219,9 @@ func (c *Command) InitDefaultVersionFlag() {
1208
1219
1209
1220
c .mergePersistentFlags ()
1210
1221
if c .Flags ().Lookup ("version" ) == nil {
1211
- usage := "version for "
1222
+ usage := gotext . Get ( "VersionFor" ) + " "
1212
1223
if c .Name () == "" {
1213
- usage += "this command"
1224
+ usage += gotext . Get ( "ThisCommand" )
1214
1225
} else {
1215
1226
usage += c .Name ()
1216
1227
}
@@ -1233,10 +1244,9 @@ func (c *Command) InitDefaultHelpCmd() {
1233
1244
1234
1245
if c .helpCommand == nil {
1235
1246
c .helpCommand = & Command {
1236
- Use : "help [command]" ,
1237
- Short : "Help about any command" ,
1238
- Long : `Help provides help for any command in the application.
1239
- Simply type ` + c .Name () + ` help [path to command] for full details.` ,
1247
+ Use : fmt .Sprintf ("help [%s]" , gotext .Get ("command" )),
1248
+ Short : gotext .Get ("CommandHelpShort" ),
1249
+ Long : fmt .Sprintf (gotext .Get ("CommandHelpLong" ), c .Name ()+ fmt .Sprintf (" help [%s]" , gotext .Get ("command" ))),
1240
1250
ValidArgsFunction : func (c * Command , args []string , toComplete string ) ([]string , ShellCompDirective ) {
1241
1251
var completions []string
1242
1252
cmd , _ , e := c .Root ().Find (args )
@@ -1259,7 +1269,7 @@ Simply type ` + c.Name() + ` help [path to command] for full details.`,
1259
1269
Run : func (c * Command , args []string ) {
1260
1270
cmd , _ , e := c .Root ().Find (args )
1261
1271
if cmd == nil || e != nil {
1262
- c .Printf ("Unknown help topic %#q \n " , args )
1272
+ c .Printf (gotext . Get ( "CommandHelpUnknownTopicError" ) + " \n " , args )
1263
1273
CheckErr (c .Root ().Usage ())
1264
1274
} else {
1265
1275
cmd .InitDefaultHelpFlag () // make possible 'help' flag to be shown
0 commit comments