34 // E.g. 'go run ...' - 'run' is the command. Cobra requires |
35 // E.g. 'go run ...' - 'run' is the command. Cobra requires |
35 // you to define the usage and description as part of your command |
36 // you to define the usage and description as part of your command |
36 // definition to ensure usability. |
37 // definition to ensure usability. |
37 type Command struct { |
38 type Command struct { |
38 // Use is the one-line usage message. |
39 // Use is the one-line usage message. |
|
40 // Recommended syntax is as follow: |
|
41 // [ ] identifies an optional argument. Arguments that are not enclosed in brackets are required. |
|
42 // ... indicates that you can specify multiple values for the previous argument. |
|
43 // | indicates mutually exclusive information. You can use the argument to the left of the separator or the |
|
44 // argument to the right of the separator. You cannot use both arguments in a single use of the command. |
|
45 // { } delimits a set of mutually exclusive arguments when one of the arguments is required. If the arguments are |
|
46 // optional, they are enclosed in brackets ([ ]). |
|
47 // Example: add [-F file | -D dir]... [-f format] profile |
39 Use string |
48 Use string |
40 |
49 |
41 // Aliases is an array of aliases that can be used instead of the first word in Use. |
50 // Aliases is an array of aliases that can be used instead of the first word in Use. |
42 Aliases []string |
51 Aliases []string |
43 |
52 |
52 Long string |
61 Long string |
53 |
62 |
54 // Example is examples of how to use the command. |
63 // Example is examples of how to use the command. |
55 Example string |
64 Example string |
56 |
65 |
57 // ValidArgs is list of all valid non-flag arguments that are accepted in bash completions |
66 // ValidArgs is list of all valid non-flag arguments that are accepted in shell completions |
58 ValidArgs []string |
67 ValidArgs []string |
|
68 // ValidArgsFunction is an optional function that provides valid non-flag arguments for shell completion. |
|
69 // It is a dynamic version of using ValidArgs. |
|
70 // Only one of ValidArgs and ValidArgsFunction can be used for a command. |
|
71 ValidArgsFunction func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) |
59 |
72 |
60 // Expected arguments |
73 // Expected arguments |
61 Args PositionalArgs |
74 Args PositionalArgs |
62 |
75 |
63 // ArgAliases is List of aliases for ValidArgs. |
76 // ArgAliases is List of aliases for ValidArgs. |
64 // These are not suggested to the user in the bash completion, |
77 // These are not suggested to the user in the shell completion, |
65 // but accepted if entered manually. |
78 // but accepted if entered manually. |
66 ArgAliases []string |
79 ArgAliases []string |
67 |
80 |
68 // BashCompletionFunction is custom functions used by the bash autocompletion generator. |
81 // BashCompletionFunction is custom bash functions used by the legacy bash autocompletion generator. |
|
82 // For portability with other shells, it is recommended to instead use ValidArgsFunction |
69 BashCompletionFunction string |
83 BashCompletionFunction string |
70 |
84 |
71 // Deprecated defines, if this command is deprecated and should print this string when used. |
85 // Deprecated defines, if this command is deprecated and should print this string when used. |
72 Deprecated string |
86 Deprecated string |
73 |
|
74 // Hidden defines, if this command is hidden and should NOT show up in the list of available commands. |
|
75 Hidden bool |
|
76 |
87 |
77 // Annotations are key/value pairs that can be used by applications to identify or |
88 // Annotations are key/value pairs that can be used by applications to identify or |
78 // group commands. |
89 // group commands. |
79 Annotations map[string]string |
90 Annotations map[string]string |
80 |
91 |
81 // Version defines the version for this command. If this value is non-empty and the command does not |
92 // Version defines the version for this command. If this value is non-empty and the command does not |
82 // define a "version" flag, a "version" boolean flag will be added to the command and, if specified, |
93 // define a "version" flag, a "version" boolean flag will be added to the command and, if specified, |
83 // will print content of the "Version" variable. |
94 // will print content of the "Version" variable. A shorthand "v" flag will also be added if the |
|
95 // command does not define one. |
84 Version string |
96 Version string |
85 |
97 |
86 // The *Run functions are executed in the following order: |
98 // The *Run functions are executed in the following order: |
87 // * PersistentPreRun() |
99 // * PersistentPreRun() |
88 // * PreRun() |
100 // * PreRun() |
110 // PersistentPostRun: children of this command will inherit and execute after PostRun. |
122 // PersistentPostRun: children of this command will inherit and execute after PostRun. |
111 PersistentPostRun func(cmd *Command, args []string) |
123 PersistentPostRun func(cmd *Command, args []string) |
112 // PersistentPostRunE: PersistentPostRun but returns an error. |
124 // PersistentPostRunE: PersistentPostRun but returns an error. |
113 PersistentPostRunE func(cmd *Command, args []string) error |
125 PersistentPostRunE func(cmd *Command, args []string) error |
114 |
126 |
115 // SilenceErrors is an option to quiet errors down stream. |
|
116 SilenceErrors bool |
|
117 |
|
118 // SilenceUsage is an option to silence usage when an error occurs. |
|
119 SilenceUsage bool |
|
120 |
|
121 // DisableFlagParsing disables the flag parsing. |
|
122 // If this is true all flags will be passed to the command as arguments. |
|
123 DisableFlagParsing bool |
|
124 |
|
125 // DisableAutoGenTag defines, if gen tag ("Auto generated by spf13/cobra...") |
|
126 // will be printed by generating docs for this command. |
|
127 DisableAutoGenTag bool |
|
128 |
|
129 // DisableFlagsInUseLine will disable the addition of [flags] to the usage |
|
130 // line of a command when printing help or generating docs |
|
131 DisableFlagsInUseLine bool |
|
132 |
|
133 // DisableSuggestions disables the suggestions based on Levenshtein distance |
|
134 // that go along with 'unknown command' messages. |
|
135 DisableSuggestions bool |
|
136 // SuggestionsMinimumDistance defines minimum levenshtein distance to display suggestions. |
|
137 // Must be > 0. |
|
138 SuggestionsMinimumDistance int |
|
139 |
|
140 // TraverseChildren parses flags on all parents before executing child command. |
|
141 TraverseChildren bool |
|
142 |
|
143 //FParseErrWhitelist flag parse errors to be ignored |
|
144 FParseErrWhitelist FParseErrWhitelist |
|
145 |
|
146 // commands is the list of commands supported by this program. |
|
147 commands []*Command |
|
148 // parent is a parent command for this command. |
|
149 parent *Command |
|
150 // Max lengths of commands' string lengths for use in padding. |
|
151 commandsMaxUseLen int |
|
152 commandsMaxCommandPathLen int |
|
153 commandsMaxNameLen int |
|
154 // commandsAreSorted defines, if command slice are sorted or not. |
|
155 commandsAreSorted bool |
|
156 // commandCalledAs is the name or alias value used to call this command. |
|
157 commandCalledAs struct { |
|
158 name string |
|
159 called bool |
|
160 } |
|
161 |
|
162 // args is actual args parsed from flags. |
127 // args is actual args parsed from flags. |
163 args []string |
128 args []string |
164 // flagErrorBuf contains all error messages from pflag. |
129 // flagErrorBuf contains all error messages from pflag. |
165 flagErrorBuf *bytes.Buffer |
130 flagErrorBuf *bytes.Buffer |
166 // flags is full set of flags. |
131 // flags is full set of flags. |
198 inReader io.Reader |
163 inReader io.Reader |
199 // outWriter is a writer defined by the user that replaces stdout |
164 // outWriter is a writer defined by the user that replaces stdout |
200 outWriter io.Writer |
165 outWriter io.Writer |
201 // errWriter is a writer defined by the user that replaces stderr |
166 // errWriter is a writer defined by the user that replaces stderr |
202 errWriter io.Writer |
167 errWriter io.Writer |
|
168 |
|
169 //FParseErrWhitelist flag parse errors to be ignored |
|
170 FParseErrWhitelist FParseErrWhitelist |
|
171 |
|
172 // CompletionOptions is a set of options to control the handling of shell completion |
|
173 CompletionOptions CompletionOptions |
|
174 |
|
175 // commandsAreSorted defines, if command slice are sorted or not. |
|
176 commandsAreSorted bool |
|
177 // commandCalledAs is the name or alias value used to call this command. |
|
178 commandCalledAs struct { |
|
179 name string |
|
180 called bool |
|
181 } |
|
182 |
|
183 ctx context.Context |
|
184 |
|
185 // commands is the list of commands supported by this program. |
|
186 commands []*Command |
|
187 // parent is a parent command for this command. |
|
188 parent *Command |
|
189 // Max lengths of commands' string lengths for use in padding. |
|
190 commandsMaxUseLen int |
|
191 commandsMaxCommandPathLen int |
|
192 commandsMaxNameLen int |
|
193 |
|
194 // TraverseChildren parses flags on all parents before executing child command. |
|
195 TraverseChildren bool |
|
196 |
|
197 // Hidden defines, if this command is hidden and should NOT show up in the list of available commands. |
|
198 Hidden bool |
|
199 |
|
200 // SilenceErrors is an option to quiet errors down stream. |
|
201 SilenceErrors bool |
|
202 |
|
203 // SilenceUsage is an option to silence usage when an error occurs. |
|
204 SilenceUsage bool |
|
205 |
|
206 // DisableFlagParsing disables the flag parsing. |
|
207 // If this is true all flags will be passed to the command as arguments. |
|
208 DisableFlagParsing bool |
|
209 |
|
210 // DisableAutoGenTag defines, if gen tag ("Auto generated by spf13/cobra...") |
|
211 // will be printed by generating docs for this command. |
|
212 DisableAutoGenTag bool |
|
213 |
|
214 // DisableFlagsInUseLine will disable the addition of [flags] to the usage |
|
215 // line of a command when printing help or generating docs |
|
216 DisableFlagsInUseLine bool |
|
217 |
|
218 // DisableSuggestions disables the suggestions based on Levenshtein distance |
|
219 // that go along with 'unknown command' messages. |
|
220 DisableSuggestions bool |
|
221 |
|
222 // SuggestionsMinimumDistance defines minimum levenshtein distance to display suggestions. |
|
223 // Must be > 0. |
|
224 SuggestionsMinimumDistance int |
|
225 } |
|
226 |
|
227 // Context returns underlying command context. If command wasn't |
|
228 // executed with ExecuteContext Context returns Background context. |
|
229 func (c *Command) Context() context.Context { |
|
230 return c.ctx |
203 } |
231 } |
204 |
232 |
205 // SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden |
233 // SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden |
206 // particularly useful when testing. |
234 // particularly useful when testing. |
207 func (c *Command) SetArgs(a []string) { |
235 func (c *Command) SetArgs(a []string) { |
226 // If newErr is nil, os.Stderr is used. |
254 // If newErr is nil, os.Stderr is used. |
227 func (c *Command) SetErr(newErr io.Writer) { |
255 func (c *Command) SetErr(newErr io.Writer) { |
228 c.errWriter = newErr |
256 c.errWriter = newErr |
229 } |
257 } |
230 |
258 |
231 // SetOut sets the source for input data |
259 // SetIn sets the source for input data |
232 // If newIn is nil, os.Stdin is used. |
260 // If newIn is nil, os.Stdin is used. |
233 func (c *Command) SetIn(newIn io.Reader) { |
261 func (c *Command) SetIn(newIn io.Reader) { |
234 c.inReader = newIn |
262 c.inReader = newIn |
235 } |
263 } |
236 |
264 |
295 // ErrOrStderr returns output to stderr |
323 // ErrOrStderr returns output to stderr |
296 func (c *Command) ErrOrStderr() io.Writer { |
324 func (c *Command) ErrOrStderr() io.Writer { |
297 return c.getErr(os.Stderr) |
325 return c.getErr(os.Stderr) |
298 } |
326 } |
299 |
327 |
300 // ErrOrStderr returns output to stderr |
328 // InOrStdin returns input to stdin |
301 func (c *Command) InOrStdin() io.Reader { |
329 func (c *Command) InOrStdin() io.Reader { |
302 return c.getIn(os.Stdin) |
330 return c.getIn(os.Stdin) |
303 } |
331 } |
304 |
332 |
305 func (c *Command) getOut(def io.Writer) io.Writer { |
333 func (c *Command) getOut(def io.Writer) io.Writer { |
367 if c.HasParent() { |
395 if c.HasParent() { |
368 return c.Parent().HelpFunc() |
396 return c.Parent().HelpFunc() |
369 } |
397 } |
370 return func(c *Command, a []string) { |
398 return func(c *Command, a []string) { |
371 c.mergePersistentFlags() |
399 c.mergePersistentFlags() |
|
400 // The help should be sent to stdout |
|
401 // See https://github.com/spf13/cobra/issues/1002 |
372 err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c) |
402 err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c) |
373 if err != nil { |
403 if err != nil { |
374 c.Println(err) |
404 c.PrintErrln(err) |
375 } |
405 } |
376 } |
406 } |
377 } |
407 } |
378 |
408 |
379 // Help puts out the help for the command. |
409 // Help puts out the help for the command. |
855 for _, x := range initializers { |
885 for _, x := range initializers { |
856 x() |
886 x() |
857 } |
887 } |
858 } |
888 } |
859 |
889 |
|
890 // ExecuteContext is the same as Execute(), but sets the ctx on the command. |
|
891 // Retrieve ctx by calling cmd.Context() inside your *Run lifecycle or ValidArgs |
|
892 // functions. |
|
893 func (c *Command) ExecuteContext(ctx context.Context) error { |
|
894 c.ctx = ctx |
|
895 return c.Execute() |
|
896 } |
|
897 |
860 // Execute uses the args (os.Args[1:] by default) |
898 // Execute uses the args (os.Args[1:] by default) |
861 // and run through the command tree finding appropriate matches |
899 // and run through the command tree finding appropriate matches |
862 // for commands and then corresponding flags. |
900 // for commands and then corresponding flags. |
863 func (c *Command) Execute() error { |
901 func (c *Command) Execute() error { |
864 _, err := c.ExecuteC() |
902 _, err := c.ExecuteC() |
865 return err |
903 return err |
866 } |
904 } |
867 |
905 |
|
906 // ExecuteContextC is the same as ExecuteC(), but sets the ctx on the command. |
|
907 // Retrieve ctx by calling cmd.Context() inside your *Run lifecycle or ValidArgs |
|
908 // functions. |
|
909 func (c *Command) ExecuteContextC(ctx context.Context) (*Command, error) { |
|
910 c.ctx = ctx |
|
911 return c.ExecuteC() |
|
912 } |
|
913 |
868 // ExecuteC executes the command. |
914 // ExecuteC executes the command. |
869 func (c *Command) ExecuteC() (cmd *Command, err error) { |
915 func (c *Command) ExecuteC() (cmd *Command, err error) { |
|
916 if c.ctx == nil { |
|
917 c.ctx = context.Background() |
|
918 } |
|
919 |
870 // Regardless of what command execute is called on, run on Root only |
920 // Regardless of what command execute is called on, run on Root only |
871 if c.HasParent() { |
921 if c.HasParent() { |
872 return c.Root().ExecuteC() |
922 return c.Root().ExecuteC() |
873 } |
923 } |
874 |
924 |
875 // windows hook |
925 // windows hook |
876 if preExecHookFn != nil { |
926 if preExecHookFn != nil { |
877 preExecHookFn(c) |
927 preExecHookFn(c) |
878 } |
928 } |
879 |
929 |
880 // initialize help as the last point possible to allow for user |
930 // initialize help at the last point to allow for user overriding |
881 // overriding |
|
882 c.InitDefaultHelpCmd() |
931 c.InitDefaultHelpCmd() |
|
932 // initialize completion at the last point to allow for user overriding |
|
933 c.initDefaultCompletionCmd() |
883 |
934 |
884 args := c.args |
935 args := c.args |
885 |
936 |
886 // Workaround FAIL with "go test -v" or "cobra.test -test.v", see #155 |
937 // Workaround FAIL with "go test -v" or "cobra.test -test.v", see #155 |
887 if c.args == nil && filepath.Base(os.Args[0]) != "cobra.test" { |
938 if c.args == nil && filepath.Base(os.Args[0]) != "cobra.test" { |
888 args = os.Args[1:] |
939 args = os.Args[1:] |
889 } |
940 } |
|
941 |
|
942 // initialize the hidden command to be used for shell completion |
|
943 c.initCompleteCmd(args) |
890 |
944 |
891 var flags []string |
945 var flags []string |
892 if c.TraverseChildren { |
946 if c.TraverseChildren { |
893 cmd, flags, err = c.Traverse(args) |
947 cmd, flags, err = c.Traverse(args) |
894 } else { |
948 } else { |
898 // If found parse to a subcommand and then failed, talk about the subcommand |
952 // If found parse to a subcommand and then failed, talk about the subcommand |
899 if cmd != nil { |
953 if cmd != nil { |
900 c = cmd |
954 c = cmd |
901 } |
955 } |
902 if !c.SilenceErrors { |
956 if !c.SilenceErrors { |
903 c.Println("Error:", err.Error()) |
957 c.PrintErrln("Error:", err.Error()) |
904 c.Printf("Run '%v --help' for usage.\n", c.CommandPath()) |
958 c.PrintErrf("Run '%v --help' for usage.\n", c.CommandPath()) |
905 } |
959 } |
906 return c, err |
960 return c, err |
907 } |
961 } |
908 |
962 |
909 cmd.commandCalledAs.called = true |
963 cmd.commandCalledAs.called = true |
910 if cmd.commandCalledAs.name == "" { |
964 if cmd.commandCalledAs.name == "" { |
911 cmd.commandCalledAs.name = cmd.Name() |
965 cmd.commandCalledAs.name = cmd.Name() |
|
966 } |
|
967 |
|
968 // We have to pass global context to children command |
|
969 // if context is present on the parent command. |
|
970 if cmd.ctx == nil { |
|
971 cmd.ctx = c.ctx |
912 } |
972 } |
913 |
973 |
914 err = cmd.execute(flags) |
974 err = cmd.execute(flags) |
915 if err != nil { |
975 if err != nil { |
916 // Always show help if requested, even if SilenceErrors is in |
976 // Always show help if requested, even if SilenceErrors is in |
918 if err == flag.ErrHelp { |
978 if err == flag.ErrHelp { |
919 cmd.HelpFunc()(cmd, args) |
979 cmd.HelpFunc()(cmd, args) |
920 return cmd, nil |
980 return cmd, nil |
921 } |
981 } |
922 |
982 |
923 // If root command has SilentErrors flagged, |
983 // If root command has SilenceErrors flagged, |
924 // all subcommands should respect it |
984 // all subcommands should respect it |
925 if !cmd.SilenceErrors && !c.SilenceErrors { |
985 if !cmd.SilenceErrors && !c.SilenceErrors { |
926 c.Println("Error:", err.Error()) |
986 c.PrintErrln("Error:", err.Error()) |
927 } |
987 } |
928 |
988 |
929 // If root command has SilentUsage flagged, |
989 // If root command has SilenceUsage flagged, |
930 // all subcommands should respect it |
990 // all subcommands should respect it |
931 if !cmd.SilenceUsage && !c.SilenceUsage { |
991 if !cmd.SilenceUsage && !c.SilenceUsage { |
932 c.Println(cmd.UsageString()) |
992 c.Println(cmd.UsageString()) |
933 } |
993 } |
934 } |
994 } |
941 } |
1001 } |
942 return c.Args(c, args) |
1002 return c.Args(c, args) |
943 } |
1003 } |
944 |
1004 |
945 func (c *Command) validateRequiredFlags() error { |
1005 func (c *Command) validateRequiredFlags() error { |
|
1006 if c.DisableFlagParsing { |
|
1007 return nil |
|
1008 } |
|
1009 |
946 flags := c.Flags() |
1010 flags := c.Flags() |
947 missingFlagNames := []string{} |
1011 missingFlagNames := []string{} |
948 flags.VisitAll(func(pflag *flag.Flag) { |
1012 flags.VisitAll(func(pflag *flag.Flag) { |
949 requiredAnnotation, found := pflag.Annotations[BashCompOneRequiredFlag] |
1013 requiredAnnotation, found := pflag.Annotations[BashCompOneRequiredFlag] |
950 if !found { |
1014 if !found { |
992 if c.Name() == "" { |
1056 if c.Name() == "" { |
993 usage += "this command" |
1057 usage += "this command" |
994 } else { |
1058 } else { |
995 usage += c.Name() |
1059 usage += c.Name() |
996 } |
1060 } |
997 c.Flags().Bool("version", false, usage) |
1061 if c.Flags().ShorthandLookup("v") == nil { |
|
1062 c.Flags().BoolP("version", "v", false, usage) |
|
1063 } else { |
|
1064 c.Flags().Bool("version", false, usage) |
|
1065 } |
998 } |
1066 } |
999 } |
1067 } |
1000 |
1068 |
1001 // InitDefaultHelpCmd adds default help command to c. |
1069 // InitDefaultHelpCmd adds default help command to c. |
1002 // It is called automatically by executing the c or by calling help and usage. |
1070 // It is called automatically by executing the c or by calling help and usage. |
1010 c.helpCommand = &Command{ |
1078 c.helpCommand = &Command{ |
1011 Use: "help [command]", |
1079 Use: "help [command]", |
1012 Short: "Help about any command", |
1080 Short: "Help about any command", |
1013 Long: `Help provides help for any command in the application. |
1081 Long: `Help provides help for any command in the application. |
1014 Simply type ` + c.Name() + ` help [path to command] for full details.`, |
1082 Simply type ` + c.Name() + ` help [path to command] for full details.`, |
1015 |
1083 ValidArgsFunction: func(c *Command, args []string, toComplete string) ([]string, ShellCompDirective) { |
|
1084 var completions []string |
|
1085 cmd, _, e := c.Root().Find(args) |
|
1086 if e != nil { |
|
1087 return nil, ShellCompDirectiveNoFileComp |
|
1088 } |
|
1089 if cmd == nil { |
|
1090 // Root help command. |
|
1091 cmd = c.Root() |
|
1092 } |
|
1093 for _, subCmd := range cmd.Commands() { |
|
1094 if subCmd.IsAvailableCommand() || subCmd == cmd.helpCommand { |
|
1095 if strings.HasPrefix(subCmd.Name(), toComplete) { |
|
1096 completions = append(completions, fmt.Sprintf("%s\t%s", subCmd.Name(), subCmd.Short)) |
|
1097 } |
|
1098 } |
|
1099 } |
|
1100 return completions, ShellCompDirectiveNoFileComp |
|
1101 }, |
1016 Run: func(c *Command, args []string) { |
1102 Run: func(c *Command, args []string) { |
1017 cmd, _, e := c.Root().Find(args) |
1103 cmd, _, e := c.Root().Find(args) |
1018 if cmd == nil || e != nil { |
1104 if cmd == nil || e != nil { |
1019 c.Printf("Unknown help topic %#q\n", args) |
1105 c.Printf("Unknown help topic %#q\n", args) |
1020 c.Root().Usage() |
1106 CheckErr(c.Root().Usage()) |
1021 } else { |
1107 } else { |
1022 cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown |
1108 cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown |
1023 cmd.Help() |
1109 CheckErr(cmd.Help()) |
1024 } |
1110 } |
1025 }, |
1111 }, |
1026 } |
1112 } |
1027 } |
1113 } |
1028 c.RemoveCommand(c.helpCommand) |
1114 c.RemoveCommand(c.helpCommand) |
1137 fmt.Fprint(c.ErrOrStderr(), i...) |
1223 fmt.Fprint(c.ErrOrStderr(), i...) |
1138 } |
1224 } |
1139 |
1225 |
1140 // PrintErrln is a convenience method to Println to the defined Err output, fallback to Stderr if not set. |
1226 // PrintErrln is a convenience method to Println to the defined Err output, fallback to Stderr if not set. |
1141 func (c *Command) PrintErrln(i ...interface{}) { |
1227 func (c *Command) PrintErrln(i ...interface{}) { |
1142 c.Print(fmt.Sprintln(i...)) |
1228 c.PrintErr(fmt.Sprintln(i...)) |
1143 } |
1229 } |
1144 |
1230 |
1145 // PrintErrf is a convenience method to Printf to the defined Err output, fallback to Stderr if not set. |
1231 // PrintErrf is a convenience method to Printf to the defined Err output, fallback to Stderr if not set. |
1146 func (c *Command) PrintErrf(format string, i ...interface{}) { |
1232 func (c *Command) PrintErrf(format string, i ...interface{}) { |
1147 c.Print(fmt.Sprintf(format, i...)) |
1233 c.PrintErr(fmt.Sprintf(format, i...)) |
1148 } |
1234 } |
1149 |
1235 |
1150 // CommandPath returns the full path to this command. |
1236 // CommandPath returns the full path to this command. |
1151 func (c *Command) CommandPath() string { |
1237 func (c *Command) CommandPath() string { |
1152 if c.HasParent() { |
1238 if c.HasParent() { |
1545 c.flagErrorBuf = new(bytes.Buffer) |
1631 c.flagErrorBuf = new(bytes.Buffer) |
1546 } |
1632 } |
1547 beforeErrorBufLen := c.flagErrorBuf.Len() |
1633 beforeErrorBufLen := c.flagErrorBuf.Len() |
1548 c.mergePersistentFlags() |
1634 c.mergePersistentFlags() |
1549 |
1635 |
1550 //do it here after merging all flags and just before parse |
1636 // do it here after merging all flags and just before parse |
1551 c.Flags().ParseErrorsWhitelist = flag.ParseErrorsWhitelist(c.FParseErrWhitelist) |
1637 c.Flags().ParseErrorsWhitelist = flag.ParseErrorsWhitelist(c.FParseErrWhitelist) |
1552 |
1638 |
1553 err := c.Flags().Parse(args) |
1639 err := c.Flags().Parse(args) |
1554 // Print warnings if they occurred (e.g. deprecated flag messages). |
1640 // Print warnings if they occurred (e.g. deprecated flag messages). |
1555 if c.flagErrorBuf.Len()-beforeErrorBufLen > 0 && err == nil { |
1641 if c.flagErrorBuf.Len()-beforeErrorBufLen > 0 && err == nil { |