Skip to content
Myr

Show messages if deprecated flags are used

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix #463

authored by Albert Nigmatzianov · committed by Albert Nigmatzianov · parent b4dbd37 · Browse files · 2 files+30 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+30 added−3 removed

  • other:+30 added−3 removed

2 files+30 added−3 removed

modified:command_test.go+20 added
296 unmodified lines
297298 // Reset pflag.CommandLine flagset.299 pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)300}301302// TestUseDeprecatedFlags checks,303// if cobra.Execute() prints a message, if a deprecated flag is used.304// Related to https://github.com/spf13/cobra/issues/463.305func TestUseDeprecatedFlags(t *testing.T) {306 c := &Command{Use: "c", Run: func(*Command, []string) {}}307 output := new(bytes.Buffer)308 c.SetOutput(output)309 c.Flags().BoolP("deprecated", "d", false, "deprecated flag")310 c.Flags().MarkDeprecated("deprecated", "This flag is deprecated")311312 c.SetArgs([]string{"c", "-d"})313 if err := c.Execute(); err != nil {314 t.Error("Unexpected error:", err)315 }316 if !strings.Contains(output.String(), "This flag is deprecated") {317 t.Errorf("Expected to contain deprecated message, but got %q", output.String())318 }319320}
modified:command.go+10 added-3 removed
1247 unmodified lines
1248 return1249}12501251// ParseFlags parses persistent flag tree and local flags.1252func (c *Command) ParseFlags(args []string) (err error) {1252func (c *Command) ParseFlags(args []string) error {1253 if c.DisableFlagParsing {1254 return nil1255 }12561257 beforeErrorBufLen := c.flagErrorBuf.Len()1258 c.mergePersistentFlags()1257 err = c.Flags().Parse(args)1258 return1259 err := c.Flags().Parse(args)1260 // Print warnings if they occurred (e.g. deprecated flag messages).1261 if c.flagErrorBuf.Len()-beforeErrorBufLen > 0 && err == nil {1262 c.Print(c.flagErrorBuf.String())1263 }12641265 return err1266}12671268// Parent returns a commands parent command.1269func (c *Command) Parent() *Command {
27 unmodified lines