mirror of
https://github.com/spf13/cobra.git
synced 2024-12-19 01:55:03 -07:00
Compare commits
7 Commits
c346498200
...
88eaeb6adb
Author | SHA1 | Date | |
---|---|---|---|
|
88eaeb6adb | ||
|
9f9056765c | ||
|
8519630750 | ||
|
1374825960 | ||
|
e80bdb9a8c | ||
|
2f70b7caa3 | ||
|
5a0bfd38d5 |
104
command.go
104
command.go
@ -193,10 +193,17 @@ type Command struct {
|
||||
|
||||
// inReader is a reader defined by the user that replaces stdin
|
||||
inReader io.Reader
|
||||
// outWriter is a writer defined by the user that replaces stdout
|
||||
outWriter io.Writer
|
||||
// errWriter is a writer defined by the user that replaces stderr
|
||||
errWriter io.Writer
|
||||
// legacyOutWriter is a writer defined by the user that replaces stdout.
|
||||
// Deprecated: use outStreamWriter instead (see https://github.com/spf13/cobra/issues/1708)
|
||||
legacyOutWriter io.Writer
|
||||
// legacyErrWriter is a writer defined by the user that replaces stderr.
|
||||
// Deprecated: use errStreamWriter instead (see https://github.com/spf13/cobra/issues/1708)
|
||||
legacyErrWriter io.Writer
|
||||
|
||||
// outStreamWriter is a writer defined by the user that replaces stdout
|
||||
outStreamWriter io.Writer
|
||||
// errStreamWriter is a writer defined by the user that replaces stderr
|
||||
errStreamWriter io.Writer
|
||||
|
||||
// FParseErrWhitelist flag parse errors to be ignored
|
||||
FParseErrWhitelist FParseErrWhitelist
|
||||
@ -284,20 +291,36 @@ func (c *Command) SetArgs(a []string) {
|
||||
//
|
||||
// Deprecated: Use SetOut and/or SetErr instead
|
||||
func (c *Command) SetOutput(output io.Writer) {
|
||||
c.outWriter = output
|
||||
c.errWriter = output
|
||||
c.legacyOutWriter = output
|
||||
c.legacyErrWriter = output
|
||||
}
|
||||
|
||||
// SetOut sets the destination for usage messages.
|
||||
// If newOut is nil, os.Stdout is used.
|
||||
// Deprecated: Use SetOutFallback and/or SetErrFallback instead (see https://github.com/spf13/cobra/issues/1708)
|
||||
func (c *Command) SetOut(newOut io.Writer) {
|
||||
c.outWriter = newOut
|
||||
c.legacyOutWriter = newOut
|
||||
}
|
||||
|
||||
// SetErr sets the destination for error messages.
|
||||
// If newErr is nil, os.Stderr is used.
|
||||
// Deprecated: Use SetOutFallback and/or SetErrFallback instead (see https://github.com/spf13/cobra/issues/1708)
|
||||
func (c *Command) SetErr(newErr io.Writer) {
|
||||
c.errWriter = newErr
|
||||
c.legacyErrWriter = newErr
|
||||
}
|
||||
|
||||
// SetOutStream sets the destination for usage messages.
|
||||
// It includes (at least): --help, --version, completion.
|
||||
// If newOut is nil, os.Stdout is used.
|
||||
func (c *Command) SetOutStream(newOut io.Writer) {
|
||||
c.outStreamWriter = newOut
|
||||
}
|
||||
|
||||
// SetErrStream sets the destination for error messages.
|
||||
// It includes (at least): errors, usage, deprecations msgs, unknowns cmds/flags/topics msgs, DebugFlags.
|
||||
// If newErr is nil, os.Stderr is used.
|
||||
func (c *Command) SetErrStream(newErr io.Writer) {
|
||||
c.errStreamWriter = newErr
|
||||
}
|
||||
|
||||
// SetIn sets the source for input data
|
||||
@ -379,9 +402,10 @@ func (c *Command) OutOrStdout() io.Writer {
|
||||
return c.getOut(os.Stdout)
|
||||
}
|
||||
|
||||
// OutOrStderr returns output to stderr
|
||||
// OutOrStderr returns output to stderr.
|
||||
// Deprecated: Use OutOrStdout or ErrOrStderr instead
|
||||
func (c *Command) OutOrStderr() io.Writer {
|
||||
return c.getOut(os.Stderr)
|
||||
return c.getErrOrLegacyOutFallbackToStderr()
|
||||
}
|
||||
|
||||
// ErrOrStderr returns output to stderr
|
||||
@ -395,8 +419,11 @@ func (c *Command) InOrStdin() io.Reader {
|
||||
}
|
||||
|
||||
func (c *Command) getOut(def io.Writer) io.Writer {
|
||||
if c.outWriter != nil {
|
||||
return c.outWriter
|
||||
if c.outStreamWriter != nil {
|
||||
return c.outStreamWriter
|
||||
}
|
||||
if c.legacyOutWriter != nil {
|
||||
return c.legacyOutWriter
|
||||
}
|
||||
if c.HasParent() {
|
||||
return c.parent.getOut(def)
|
||||
@ -405,8 +432,11 @@ func (c *Command) getOut(def io.Writer) io.Writer {
|
||||
}
|
||||
|
||||
func (c *Command) getErr(def io.Writer) io.Writer {
|
||||
if c.errWriter != nil {
|
||||
return c.errWriter
|
||||
if c.legacyErrWriter != nil {
|
||||
return c.legacyErrWriter
|
||||
}
|
||||
if c.errStreamWriter != nil {
|
||||
return c.errStreamWriter
|
||||
}
|
||||
if c.HasParent() {
|
||||
return c.parent.getErr(def)
|
||||
@ -414,6 +444,22 @@ func (c *Command) getErr(def io.Writer) io.Writer {
|
||||
return def
|
||||
}
|
||||
|
||||
// getErrOrLegacyOutFallbackToStderr should only be used inside OutOrStderr.
|
||||
// Deprecated: this function exists to allow for backwards compatibility only
|
||||
// (see https://github.com/spf13/cobra/issues/1708)
|
||||
func (c *Command) getErrOrLegacyOutFallbackToStderr() io.Writer {
|
||||
if c.errStreamWriter != nil {
|
||||
return c.errStreamWriter
|
||||
}
|
||||
if c.legacyOutWriter != nil {
|
||||
return c.legacyOutWriter
|
||||
}
|
||||
if c.HasParent() {
|
||||
return c.parent.getErrOrLegacyOutFallbackToStderr()
|
||||
}
|
||||
return os.Stderr
|
||||
}
|
||||
|
||||
func (c *Command) getIn(def io.Reader) io.Reader {
|
||||
if c.inReader != nil {
|
||||
return c.inReader
|
||||
@ -481,18 +527,18 @@ func (c *Command) Help() error {
|
||||
// UsageString returns usage string.
|
||||
func (c *Command) UsageString() string {
|
||||
// Storing normal writers
|
||||
tmpOutput := c.outWriter
|
||||
tmpErr := c.errWriter
|
||||
tmpOutput := c.legacyOutWriter
|
||||
tmpErr := c.legacyErrWriter
|
||||
|
||||
bb := new(bytes.Buffer)
|
||||
c.outWriter = bb
|
||||
c.errWriter = bb
|
||||
c.legacyOutWriter = bb
|
||||
c.legacyErrWriter = bb
|
||||
|
||||
CheckErr(c.Usage())
|
||||
|
||||
// Setting things back to normal
|
||||
c.outWriter = tmpOutput
|
||||
c.errWriter = tmpErr
|
||||
c.legacyOutWriter = tmpOutput
|
||||
c.legacyErrWriter = tmpErr
|
||||
|
||||
return bb.String()
|
||||
}
|
||||
@ -1397,20 +1443,38 @@ main:
|
||||
}
|
||||
|
||||
// Print is a convenience method to Print to the defined output, fallback to Stderr if not set.
|
||||
// Deprecated: Use PrintOut or PrintErr instead.
|
||||
func (c *Command) Print(i ...interface{}) {
|
||||
fmt.Fprint(c.OutOrStderr(), i...)
|
||||
}
|
||||
|
||||
// Println is a convenience method to Println to the defined output, fallback to Stderr if not set.
|
||||
// Deprecated: Use PrintOutln or PrintErrln instead.
|
||||
func (c *Command) Println(i ...interface{}) {
|
||||
c.Print(fmt.Sprintln(i...))
|
||||
}
|
||||
|
||||
// Printf is a convenience method to Printf to the defined output, fallback to Stderr if not set.
|
||||
// Deprecated: Use PrintOutf or PrintErrf instead.
|
||||
func (c *Command) Printf(format string, i ...interface{}) {
|
||||
c.Print(fmt.Sprintf(format, i...))
|
||||
}
|
||||
|
||||
// PrintOut is a convenience method to Print to the defined OutStream, fallback to Stdout if not set.
|
||||
func (c *Command) PrintOut(i ...interface{}) {
|
||||
fmt.Fprint(c.OutOrStdout(), i...)
|
||||
}
|
||||
|
||||
// PrintOutln is a convenience method to Println to the defined OutStream, fallback to Stdout if not set.
|
||||
func (c *Command) PrintOutln(i ...interface{}) {
|
||||
c.PrintOut(fmt.Sprintln(i...))
|
||||
}
|
||||
|
||||
// PrintOutf is a convenience method to Printf to the defined OutStream, fallback to Stdout if not set.
|
||||
func (c *Command) PrintOutf(format string, i ...interface{}) {
|
||||
c.PrintOut(fmt.Sprintf(format, i...))
|
||||
}
|
||||
|
||||
// PrintErr is a convenience method to Print to the defined Err output, fallback to Stderr if not set.
|
||||
func (c *Command) PrintErr(i ...interface{}) {
|
||||
fmt.Fprint(c.ErrOrStderr(), i...)
|
||||
|
@ -141,9 +141,6 @@ func TestGenManSeeAlso(t *testing.T) {
|
||||
if err := assertLineFound(scanner, ".SH SEE ALSO"); err != nil {
|
||||
t.Fatalf("Couldn't find SEE ALSO section header: %v", err)
|
||||
}
|
||||
if err := assertNextLineEquals(scanner, ".PP"); err != nil {
|
||||
t.Fatalf("First line after SEE ALSO wasn't break-indent: %v", err)
|
||||
}
|
||||
if err := assertNextLineEquals(scanner, `\fBroot-bbb(1)\fP, \fBroot-ccc(1)\fP`); err != nil {
|
||||
t.Fatalf("Second line after SEE ALSO wasn't correct: %v", err)
|
||||
}
|
||||
|
2
go.mod
2
go.mod
@ -3,7 +3,7 @@ module github.com/spf13/cobra
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.4
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.6
|
||||
github.com/inconshreveable/mousetrap v1.1.0
|
||||
github.com/spf13/pflag v1.0.5
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
|
4
go.sum
4
go.sum
@ -1,5 +1,5 @@
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.6 h1:XJtiaUW6dEEqVuZiMTn1ldk455QWwEIsMIJlo5vtkx0=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||
|
Loading…
Reference in New Issue
Block a user