Set a different stream for deprecation errors.

Signed-off-by: Daniel Nephin <dnephin@gmail.com>
This commit is contained in:
Daniel Nephin 2016-06-23 13:57:39 -04:00
parent 6a8bd97bdb
commit 60b5e87117

View file

@ -109,13 +109,14 @@ type Command struct {
flagErrorBuf *bytes.Buffer flagErrorBuf *bytes.Buffer
args []string // actual args parsed from flags args []string // actual args parsed from flags
output *io.Writer // nil means stderr; use Out() method instead output *io.Writer // nil means stderr; use Out() method instead
usageFunc func(*Command) error // Usage can be defined by application deprecatedWriter *io.Writer // Stream for writing deprecation messages
usageTemplate string // Can be defined by Application usageFunc func(*Command) error // Usage can be defined by application
helpTemplate string // Can be defined by Application usageTemplate string // Can be defined by Application
helpFunc func(*Command, []string) // Help can be defined by application helpTemplate string // Can be defined by Application
helpCommand *Command // The help command helpFunc func(*Command, []string) // Help can be defined by application
helpCommand *Command // The help command
// The global normalization function that we can use on every pFlag set and children commands // The global normalization function that we can use on every pFlag set and children commands
globNormFunc func(f *flag.FlagSet, name string) flag.NormalizedName globNormFunc func(f *flag.FlagSet, name string) flag.NormalizedName
@ -159,6 +160,11 @@ func (c *Command) SetOutput(output io.Writer) {
c.output = &output c.output = &output
} }
// SetDeprecatedWriter sets a writer to use for deprecation messages.
func (c *Command) SetDeprecatedWriter(output io.Writer) {
c.deprecatedWriter = &output
}
// Usage can be defined by application // Usage can be defined by application
func (c *Command) SetUsageFunc(f func(*Command) error) { func (c *Command) SetUsageFunc(f func(*Command) error) {
c.usageFunc = f c.usageFunc = f
@ -517,7 +523,7 @@ func (c *Command) execute(a []string) (err error) {
} }
if len(c.Deprecated) > 0 { if len(c.Deprecated) > 0 {
c.Printf("Command %q is deprecated, %s\n", c.Name(), c.Deprecated) c.printDeprecated()
} }
// initialize help flag as the last point possible to allow for user // initialize help flag as the last point possible to allow for user
@ -734,7 +740,7 @@ func (c commandSorterByName) Less(i, j int) bool { return c[i].Name() < c[j].Nam
// Commands returns a sorted slice of child commands. // Commands returns a sorted slice of child commands.
func (c *Command) Commands() []*Command { func (c *Command) Commands() []*Command {
// do not sort commands if it already sorted or sorting was disabled // do not sort commands if it already sorted or sorting was disabled
if EnableCommandSorting && !c.commandsAreSorted{ if EnableCommandSorting && !c.commandsAreSorted {
sort.Sort(commandSorterByName(c.commands)) sort.Sort(commandSorterByName(c.commands))
c.commandsAreSorted = true c.commandsAreSorted = true
} }
@ -821,6 +827,16 @@ func (c *Command) Printf(format string, i ...interface{}) {
c.Print(str) c.Print(str)
} }
func (c *Command) printDeprecated() {
var out io.Writer
if c.deprecatedWriter != nil {
out = *c.deprecatedWriter
} else {
out = c.Out()
}
fmt.Fprintf(out, "Command %q is deprecated, %s\n", c.Name(), c.Deprecated)
}
// Output the usage for the command // Output the usage for the command
// Used when a user provides invalid input // Used when a user provides invalid input
// Can be defined by user by overriding UsageFunc // Can be defined by user by overriding UsageFunc