mirror of https://github.com/spf13/cobra.git
parent
b4dbd37a01
commit
99b5d838ca
13
command.go
13
command.go
|
@ -1249,13 +1249,20 @@ func (c *Command) persistentFlag(name string) (flag *flag.Flag) {
|
|||
}
|
||||
|
||||
// ParseFlags parses persistent flag tree and local flags.
|
||||
func (c *Command) ParseFlags(args []string) (err error) {
|
||||
func (c *Command) ParseFlags(args []string) error {
|
||||
if c.DisableFlagParsing {
|
||||
return nil
|
||||
}
|
||||
|
||||
beforeErrorBufLen := c.flagErrorBuf.Len()
|
||||
c.mergePersistentFlags()
|
||||
err = c.Flags().Parse(args)
|
||||
return
|
||||
err := c.Flags().Parse(args)
|
||||
// Print warnings if they occurred (e.g. deprecated flag messages).
|
||||
if c.flagErrorBuf.Len()-beforeErrorBufLen > 0 && err == nil {
|
||||
c.Print(c.flagErrorBuf.String())
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Parent returns a commands parent command.
|
||||
|
|
|
@ -298,3 +298,23 @@ func TestMergeCommandLineToFlags(t *testing.T) {
|
|||
// Reset pflag.CommandLine flagset.
|
||||
pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)
|
||||
}
|
||||
|
||||
// TestUseDeprecatedFlags checks,
|
||||
// if cobra.Execute() prints a message, if a deprecated flag is used.
|
||||
// Related to https://github.com/spf13/cobra/issues/463.
|
||||
func TestUseDeprecatedFlags(t *testing.T) {
|
||||
c := &Command{Use: "c", Run: func(*Command, []string) {}}
|
||||
output := new(bytes.Buffer)
|
||||
c.SetOutput(output)
|
||||
c.Flags().BoolP("deprecated", "d", false, "deprecated flag")
|
||||
c.Flags().MarkDeprecated("deprecated", "This flag is deprecated")
|
||||
|
||||
c.SetArgs([]string{"c", "-d"})
|
||||
if err := c.Execute(); err != nil {
|
||||
t.Error("Unexpected error:", err)
|
||||
}
|
||||
if !strings.Contains(output.String(), "This flag is deprecated") {
|
||||
t.Errorf("Expected to contain deprecated message, but got %q", output.String())
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue