Revert "Get rid of flagErrorBuf (#417)"
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This reverts commit b655df6ce84126fe9272a3cd4d70555a3a4cfed4. Reason for revert: duplicate errors from flag. More in #426 Fix #426
authored by Albert Nigmatzianov · committed by Albert Nigmatzianov · parent 6dd9084 · Browse files · 1 file·+25 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+25 added−6 removed
- other:+25 added−6 removed
Provenance
- Commit
3d7bff8- Tree
67c9212- Author
Albert Nigmatzianov <albertnigma@gmail.com>- Committer
Albert Nigmatzianov <albertnigma@gmail.com>- Parent
6dd9084
1 file changed+25 added−6 removed
modified:command.go+25 added-6 removed
112 unmodified lines113 commandsMaxNameLen int114 // is commands slice are sorted or not115 commandsAreSorted bool116 117+ // flagErrorBuf contains all error messages from pflag.118+ flagErrorBuf *bytes.Buffer119+120 args []string // actual args parsed from flags121 output io.Writer // out writer if set in SetOutput(w)122 usageFunc func(*Command) error // Usage can be defined by application123 usageTemplate string // Can be defined by Application793 unmodified lines917 c.Println(" -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, " [P]")918 }919 })920 }921+ c.Println(x.flagErrorBuf)922 if x.HasSubCommands() {923 for _, y := range x.commands {924 debugflags(y)925 }132 unmodified lines1058 // to this command (local and persistent declared here and by all parents).1059 func (c *Command) Flags() *flag.FlagSet {1060 if c.flags == nil {1061 c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1062+ if c.flagErrorBuf == nil {1063+ c.flagErrorBuf = new(bytes.Buffer)1064+ }1058- c.flags.SetOutput(c.OutOrStderr())1065+ c.flags.SetOutput(c.flagErrorBuf)1066 }1067 1068 return c.flags1069 }16 unmodified lines1086 c.mergePersistentFlags()1087 1088 if c.lflags == nil {1089 c.lflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1090+ if c.flagErrorBuf == nil {1091+ c.flagErrorBuf = new(bytes.Buffer)1092+ }1083- c.lflags.SetOutput(c.OutOrStderr())1093+ c.lflags.SetOutput(c.flagErrorBuf)1094 }1095 c.lflags.SortFlags = c.Flags().SortFlags1096 1097 addToLocal := func(f *flag.Flag) {11 unmodified lines1109 c.mergePersistentFlags()1110 1111 if c.iflags == nil {1112 c.iflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1113+ if c.flagErrorBuf == nil {1114+ c.flagErrorBuf = new(bytes.Buffer)1115+ }1116+ c.iflags.SetOutput(c.flagErrorBuf)1117 }1118 1119 local := c.LocalFlags()1120 c.parentsPflags.VisitAll(func(f *flag.Flag) {12 unmodified lines1133 // PersistentFlags returns the persistent FlagSet specifically set in the current command.1134 func (c *Command) PersistentFlags() *flag.FlagSet {1135 if c.pflags == nil {1136 c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1137+ if c.flagErrorBuf == nil {1138+ c.flagErrorBuf = new(bytes.Buffer)1139+ }1123- c.pflags.SetOutput(c.OutOrStderr())1140+ c.pflags.SetOutput(c.flagErrorBuf)1141 }1142 return c.pflags1143 }1144 1145 // ResetFlags is used in testing.1146 func (c *Command) ResetFlags() {1147+ c.flagErrorBuf = new(bytes.Buffer)1148+ c.flagErrorBuf.Reset()1149 c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1131- c.flags.SetOutput(c.OutOrStderr())1150+ c.flags.SetOutput(c.flagErrorBuf)1151 c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1133- c.pflags.SetOutput(c.OutOrStderr())1152+ c.pflags.SetOutput(c.flagErrorBuf)1153 }1154 1155 // HasFlags checks if the command contains any flags (local plus persistent from the entire structure).1156 func (c *Command) HasFlags() bool {90 unmodified lines1247 // If c.parentsPflags == nil, it makes new.1248 func (c *Command) updateParentsPflags() {1249 if c.parentsPflags == nil {1250 c.parentsPflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1232- c.parentsPflags.SetOutput(c.OutOrStderr())1251+ c.parentsPflags.SetOutput(c.flagErrorBuf)1252 c.parentsPflags.SortFlags = false1253 }1254 1255 c.Root().PersistentFlags().AddFlagSet(flag.CommandLine)5 unmodified lines