mirror of
https://github.com/spf13/cobra
synced 2024-11-24 22:57:12 +00:00
Set a different stream for deprecation errors.
Signed-off-by: Daniel Nephin <dnephin@gmail.com>
This commit is contained in:
parent
6a8bd97bdb
commit
60b5e87117
1 changed files with 25 additions and 9 deletions
20
command.go
20
command.go
|
@ -111,6 +111,7 @@ type Command struct {
|
||||||
|
|
||||||
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
|
||||||
|
deprecatedWriter *io.Writer // Stream for writing deprecation messages
|
||||||
usageFunc func(*Command) error // Usage can be defined by application
|
usageFunc func(*Command) error // Usage can be defined by application
|
||||||
usageTemplate string // Can be defined by Application
|
usageTemplate string // Can be defined by Application
|
||||||
helpTemplate string // Can be defined by Application
|
helpTemplate string // Can be defined by Application
|
||||||
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue