Skip to content
Myr

command.go

This file

Path

command.go

1,504 lines · 41.4 KiB · Go

Last changed by

Add `CalledAs` method to cobra.Command (w/ tests) (#567)Tim Peoplesbefore adoptioneb58983

History of this file

Take it

The walk stopped after 200 commits: lines older than 65a708c point at it.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1–13why, one step back
1// Copyright © 2013 Steve Francia <spf@spf13.com>.
2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.13

Use pflag.ShorthandLookupAlbert Nigmatzianovbefore adoption4061f41

lines 14–15why, one step back
14// Package cobra is a commander providing a simple interface to create powerful modern CLI interfaces.
15// In addition to providing an interface, Cobra simultaneously provides a controller to organize your application code.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 16–23why, one step back
16package cobra
1718import (19 "bytes"20 "fmt"21 "io"22 "os"23 "path/filepath"

Merge pull request #293 from andreykurilin/sort_commandsEric Parisbefore adoptionbc81c21

line 24why, one step back
24 "sort"

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 25–30why, one step back
25 "strings"
2627 flag "github.com/spf13/pflag"28)2930// Command is just that, a command for your application.

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

line 31why, one step back
31// E.g. 'go run ...' - 'run' is the command. Cobra requires

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 32–34why, one step back
32// you to define the usage and description as part of your command
33// definition to ensure usability.34type Command struct {

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

line 35why, one step back
35 // Use is the one-line usage message.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 36why, one step back
36 Use string

Divide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483

line 37why, one step back
37

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

line 38why, one step back
38 // Aliases is an array of aliases that can be used instead of the first word in Use.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 39why, one step back
39 Aliases []string

Divide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483

line 40why, one step back
40

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

lines 41–42why, one step back
41 // SuggestFor is an array of command names for which this command will be suggested -
42 // similar to aliases but only suggests.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 43why, one step back
43 SuggestFor []string

Divide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483

line 44why, one step back
44

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

line 45why, one step back
45 // Short is the short description shown in the 'help' output.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 46why, one step back
46 Short string

Divide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483

line 47why, one step back
47

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

line 48why, one step back
48 // Long is the long message shown in the 'help <this-command>' output.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 49why, one step back
49 Long string

Divide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483

line 50why, one step back
50

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

line 51why, one step back
51 // Example is examples of how to use the command.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 52why, one step back
52 Example string

Divide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483

line 53why, one step back
53

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

line 54why, one step back
54 // ValidArgs is list of all valid non-flag arguments that are accepted in bash completions

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 55why, one step back
55 ValidArgs []string

Divide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483

line 56why, one step back
56

Update documentationAlbert Nigmatzianovbefore adoption1723331

lines 57–59why, one step back
57 // Expected arguments
58 Args PositionalArgs59

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

lines 60–62why, one step back
60 // ArgAliases is List of aliases for ValidArgs.
61 // These are not suggested to the user in the bash completion,62 // but accepted if entered manually.

Merge pull request #257 from sttts/sttts-noun-aliasesEric Parisbefore adoption60267ae

line 63why, one step back
63 ArgAliases []string

Divide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483

line 64why, one step back
64

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

line 65why, one step back
65 // BashCompletionFunction is custom functions used by the bash autocompletion generator.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 66why, one step back
66 BashCompletionFunction string

Divide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483

line 67why, one step back
67

Group fields of CommandAlbert Nigmatzianovbefore adoption90d2fd8

line 68why, one step back
68 // Deprecated defines, if this command is deprecated and should print this string when used.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 69why, one step back
69 Deprecated string

Divide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483

line 70why, one step back
70

Group fields of CommandAlbert Nigmatzianovbefore adoption90d2fd8

line 71why, one step back
71 // Hidden defines, if this command is hidden and should NOT show up in the list of available commands.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 72why, one step back
72 Hidden bool

Divide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483

line 73why, one step back
73

Add a tags field for user defined tags on a Command. (#359)Daniel Nephinbefore adoption99600f6

line 74why, one step back
74 // Annotations are key/value pairs that can be used by applications to identify or

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

line 75why, one step back
75 // group commands.

Add a tags field for user defined tags on a Command. (#359)Daniel Nephinbefore adoption99600f6

line 76why, one step back
76 Annotations map[string]string

Divide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483

line 77why, one step back
77

Add support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce

lines 78–82why, one step back
78 // Version defines the version for this command. If this value is non-empty and the command does not
79 // define a "version" flag, a "version" boolean flag will be added to the command and, if specified,80 // will print content of the "Version" variable.81 Version string82

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 83–88why, one step back
83 // The *Run functions are executed in the following order:
84 // * PersistentPreRun()85 // * PreRun()86 // * Run()87 // * PostRun()88 // * PersistentPostRun()

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

line 89why, one step back
89 // All functions get the same args, the arguments after the command name.

Divide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483

line 90why, one step back
90 //

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

line 91why, one step back
91 // PersistentPreRun: children of this command will inherit and execute.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 92why, one step back
92 PersistentPreRun func(cmd *Command, args []string)

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

line 93why, one step back
93 // PersistentPreRunE: PersistentPreRun but returns an error.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 94–96why, one step back
94 PersistentPreRunE func(cmd *Command, args []string) error
95 // PreRun: children of this command will not inherit.96 PreRun func(cmd *Command, args []string)

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

line 97why, one step back
97 // PreRunE: PreRun but returns an error.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 98why, one step back
98 PreRunE func(cmd *Command, args []string) error

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

line 99why, one step back
99 // Run: Typically the actual work function. Most commands will only implement this.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 100why, one step back
100 Run func(cmd *Command, args []string)

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

line 101why, one step back
101 // RunE: Run but returns an error.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 102–104why, one step back
102 RunE func(cmd *Command, args []string) error
103 // PostRun: run after the Run command.104 PostRun func(cmd *Command, args []string)

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

line 105why, one step back
105 // PostRunE: PostRun but returns an error.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 106why, one step back
106 PostRunE func(cmd *Command, args []string) error

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

line 107why, one step back
107 // PersistentPostRun: children of this command will inherit and execute after PostRun.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 108why, one step back
108 PersistentPostRun func(cmd *Command, args []string)

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

line 109why, one step back
109 // PersistentPostRunE: PersistentPostRun but returns an error.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 110why, one step back
110 PersistentPostRunE func(cmd *Command, args []string) error

Divide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483

lines 111–117why, one step back
111
112 // SilenceErrors is an option to quiet errors down stream.113 SilenceErrors bool114115 // SilenceUsage is an option to silence usage when an error occurs.116 SilenceUsage bool117

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

lines 118–120why, one step back
118 // DisableFlagParsing disables the flag parsing.
119 // If this is true all flags will be passed to the command as arguments.120 DisableFlagParsing bool

Divide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483

line 121why, one step back
121

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

lines 122–123why, one step back
122 // DisableAutoGenTag defines, if gen tag ("Auto generated by spf13/cobra...")
123 // will be printed by generating docs for this command.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 124why, one step back
124 DisableAutoGenTag bool

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

line 125why, one step back
125

Disable flags in UseLine (#552)Daniel Nephinbefore adoption34ceca5

lines 126–129why, one step back
126 // DisableFlagsInUseLine will disable the addition of [flags] to the usage
127 // line of a command when printing help or generating docs128 DisableFlagsInUseLine bool129

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

lines 130–136why, one step back
130 // DisableSuggestions disables the suggestions based on Levenshtein distance
131 // that go along with 'unknown command' messages.132 DisableSuggestions bool133 // SuggestionsMinimumDistance defines minimum levenshtein distance to display suggestions.134 // Must be > 0.135 SuggestionsMinimumDistance int136

Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03

lines 137–139why, one step back
137 // TraverseChildren parses flags on all parents before executing child command.
138 TraverseChildren bool139

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

line 140why, one step back
140 // commands is the list of commands supported by this program.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 141why, one step back
141 commands []*Command

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

line 142why, one step back
142 // parent is a parent command for this command.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 143why, one step back
143 parent *Command

Divide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483

line 144why, one step back
144 // Max lengths of commands' string lengths for use in padding.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 145–147why, one step back
145 commandsMaxUseLen int
146 commandsMaxCommandPathLen int147 commandsMaxNameLen int

Group fields of CommandAlbert Nigmatzianovbefore adoption90d2fd8

line 148why, one step back
148 // commandsAreSorted defines, if command slice are sorted or not.

Merge pull request #293 from andreykurilin/sort_commandsEric Parisbefore adoptionbc81c21

line 149why, one step back
149 commandsAreSorted bool

Add `CalledAs` method to cobra.Command (w/ tests) (#567)Tim Peoplesbefore adoptioneb58983

lines 150–154why, one step back
150 // commandCalledAs is the name or alias value used to call this command.
151 commandCalledAs struct {152 name string153 called bool154 }

Group fields of CommandAlbert Nigmatzianovbefore adoption90d2fd8

line 155why, one step back
155

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

lines 156–157why, one step back
156 // args is actual args parsed from flags.
157 args []string

Group fields of CommandAlbert Nigmatzianovbefore adoption90d2fd8

lines 158–173why, one step back
158 // flagErrorBuf contains all error messages from pflag.
159 flagErrorBuf *bytes.Buffer160 // flags is full set of flags.161 flags *flag.FlagSet162 // pflags contains persistent flags.163 pflags *flag.FlagSet164 // lflags contains local flags.165 lflags *flag.FlagSet166 // iflags contains inherited flags.167 iflags *flag.FlagSet168 // parentsPflags is all persistent flags of cmd's parents.169 parentsPflags *flag.FlagSet170 // globNormFunc is the global normalization function171 // that we can use on every pflag set and children commands172 globNormFunc func(f *flag.FlagSet, name string) flag.NormalizedName173

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

lines 174–181why, one step back
174 // output is an output writer defined by user.
175 output io.Writer176 // usageFunc is usage func defined by user.177 usageFunc func(*Command) error178 // usageTemplate is usage template defined by user.179 usageTemplate string180 // flagErrorFunc is func defined by user and it's called when the parsing of181 // flags returns an error.

Merge pull request #283 from dnephin/add_flag_usage_funcEric Parisbefore adoptionb620a75

line 182why, one step back
182 flagErrorFunc func(*Command, error) error

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

lines 183–186why, one step back
183 // helpTemplate is help template defined by user.
184 helpTemplate string185 // helpFunc is help func defined by user.186 helpFunc func(*Command, []string)

Group fields of CommandAlbert Nigmatzianovbefore adoption90d2fd8

line 187why, one step back
187 // helpCommand is command with usage 'help'. If it's not defined by user,

Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d

lines 188–189why, one step back
188 // cobra uses default help command.
189 helpCommand *Command

Add support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce

lines 190–191why, one step back
190 // versionTemplate is the version template defined by user.
191 versionTemplate string

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 192–193why, one step back
192}
193

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 194why, one step back
194// SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 195–202why, one step back
195// particularly useful when testing.
196func (c *Command) SetArgs(a []string) {197 c.args = a198}199200// SetOutput sets the destination for usage and error messages.201// If output is nil, os.Stderr is used.202func (c *Command) SetOutput(output io.Writer) {

Avoid storing pointer to nil (#411)Tamir Dubersteinbefore adoption7aeaa2c

line 203why, one step back
203 c.output = output

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 204–205why, one step back
204}
205

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 206why, one step back
206// SetUsageFunc sets usage function. Usage can be defined by application.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 207–210why, one step back
207func (c *Command) SetUsageFunc(f func(*Command) error) {
208 c.usageFunc = f209}210

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 211why, one step back
211// SetUsageTemplate sets usage template. Can be defined by Application.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 212–215why, one step back
212func (c *Command) SetUsageTemplate(s string) {
213 c.usageTemplate = s214}215

Merge pull request #283 from dnephin/add_flag_usage_funcEric Parisbefore adoptionb620a75

line 216why, one step back
216// SetFlagErrorFunc sets a function to generate an error when flag parsing

Some fixes related to help command (#394)Albert Nigmatzianovbefore adoptionfcd0c5a

line 217why, one step back
217// fails.

Merge pull request #283 from dnephin/add_flag_usage_funcEric Parisbefore adoptionb620a75

lines 218–221why, one step back
218func (c *Command) SetFlagErrorFunc(f func(*Command, error) error) {
219 c.flagErrorFunc = f220}221

Some fixes related to help command (#394)Albert Nigmatzianovbefore adoptionfcd0c5a

line 222why, one step back
222// SetHelpFunc sets help function. Can be defined by Application.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 223–226why, one step back
223func (c *Command) SetHelpFunc(f func(*Command, []string)) {
224 c.helpFunc = f225}226

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 227why, one step back
227// SetHelpCommand sets help command.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 228–231why, one step back
228func (c *Command) SetHelpCommand(cmd *Command) {
229 c.helpCommand = cmd230}231

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 232why, one step back
232// SetHelpTemplate sets help template to be used. Application can use it to set custom template.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 233–236why, one step back
233func (c *Command) SetHelpTemplate(s string) {
234 c.helpTemplate = s235}236

Add support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce

lines 237–241why, one step back
237// SetVersionTemplate sets version template to be used. Application can use it to set custom template.
238func (c *Command) SetVersionTemplate(s string) {239 c.versionTemplate = s240}241

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 242–253why, one step back
242// SetGlobalNormalizationFunc sets a normalization function to all flag sets and also to child commands.
243// The user should not have a cyclic dependency on commands.244func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string) flag.NormalizedName) {245 c.Flags().SetNormalizeFunc(n)246 c.PersistentFlags().SetNormalizeFunc(n)247 c.globNormFunc = n248249 for _, command := range c.commands {250 command.SetGlobalNormalizationFunc(n)251 }252}253

Some fixes related to help command (#394)Albert Nigmatzianovbefore adoptionfcd0c5a

line 254why, one step back
254// OutOrStdout returns output to stdout.

Merge pull request #305 from fabianofranz/expose_outEric Parisbefore adoptionb24564e

line 255why, one step back
255func (c *Command) OutOrStdout() io.Writer {

Merge pull request #303 from fabianofranz/testing_help_exiting_1Eric Parisbefore adoptiona272c3c

lines 256–258why, one step back
256 return c.getOut(os.Stdout)
257}258

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 259why, one step back
259// OutOrStderr returns output to stderr

Merge pull request #305 from fabianofranz/expose_outEric Parisbefore adoptionb24564e

line 260why, one step back
260func (c *Command) OutOrStderr() io.Writer {

Merge pull request #303 from fabianofranz/testing_help_exiting_1Eric Parisbefore adoptiona272c3c

lines 261–265why, one step back
261 return c.getOut(os.Stderr)
262}263264func (c *Command) getOut(def io.Writer) io.Writer {265 if c.output != nil {

Avoid storing pointer to nil (#411)Tamir Dubersteinbefore adoption7aeaa2c

line 266why, one step back
266 return c.output

Merge pull request #303 from fabianofranz/testing_help_exiting_1Eric Parisbefore adoptiona272c3c

lines 267–274why, one step back
267 }
268 if c.HasParent() {269 return c.parent.getOut(def)270 }271 return def272}273274// UsageFunc returns either the function set by SetUsageFunc for this command

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 275why, one step back
275// or a parent, or it returns a default usage function.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 276–280why, one step back
276func (c *Command) UsageFunc() (f func(*Command) error) {
277 if c.usageFunc != nil {278 return c.usageFunc279 }280 if c.HasParent() {

Delete checkHelpFuncAlbert Nigmatzianovbefore adoption681a777

line 281why, one step back
281 return c.Parent().UsageFunc()

Merge pull request #260 from ianwalter/code-cleanupEric Parisbefore adoptionb0d571e

lines 282–283why, one step back
282 }
283 return func(c *Command) error {

Merge pull request #305 from fabianofranz/expose_outEric Parisbefore adoptionb24564e

lines 284–285why, one step back
284 c.mergePersistentFlags()
285 err := tmpl(c.OutOrStderr(), c.UsageTemplate(), c)

Merge pull request #260 from ianwalter/code-cleanupEric Parisbefore adoptionb0d571e

line 286why, one step back
286 if err != nil {

Merge pull request #303 from fabianofranz/testing_help_exiting_1Eric Parisbefore adoptiona272c3c

line 287why, one step back
287 c.Println(err)

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 288why, one step back
288 }

Merge pull request #260 from ianwalter/code-cleanupEric Parisbefore adoptionb0d571e

line 289why, one step back
289 return err

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 290–292why, one step back
290 }
291}292

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

lines 293–295why, one step back
293// Usage puts out the usage for the command.
294// Used when a user provides invalid input.295// Can be defined by user by overriding UsageFunc.

Merge pull request #308 from fabianofranz/restore_backwards_compatibilityEric Parisbefore adoptiondbb7c2d

lines 296–299why, one step back
296func (c *Command) Usage() error {
297 return c.UsageFunc()(c)298}299

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 300why, one step back
300// HelpFunc returns either the function set by SetHelpFunc for this command

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 301why, one step back
301// or a parent, or it returns a function with default help behavior.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 302why, one step back
302func (c *Command) HelpFunc() func(*Command, []string) {

Delete checkHelpFuncAlbert Nigmatzianovbefore adoption681a777

lines 303–304why, one step back
303 if c.helpFunc != nil {
304 return c.helpFunc

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 305why, one step back
305 }

Delete checkHelpFuncAlbert Nigmatzianovbefore adoption681a777

lines 306–309why, one step back
306 if c.HasParent() {
307 return c.Parent().HelpFunc()308 }309 return func(c *Command, a []string) {

Merge pull request #305 from fabianofranz/expose_outEric Parisbefore adoptionb24564e

lines 310–311why, one step back
310 c.mergePersistentFlags()
311 err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c)

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 312–317why, one step back
312 if err != nil {
313 c.Println(err)314 }315 }316}317

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

lines 318–320why, one step back
318// Help puts out the help for the command.
319// Used when a user calls help [command].320// Can be defined by user by overriding HelpFunc.

Merge pull request #308 from fabianofranz/restore_backwards_compatibilityEric Parisbefore adoptiondbb7c2d

lines 321–325why, one step back
321func (c *Command) Help() error {
322 c.HelpFunc()(c, []string{})323 return nil324}325

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 326why, one step back
326// UsageString return usage string.

Merge pull request #303 from fabianofranz/testing_help_exiting_1Eric Parisbefore adoptiona272c3c

lines 327–330why, one step back
327func (c *Command) UsageString() string {
328 tmpOutput := c.output329 bb := new(bytes.Buffer)330 c.SetOutput(bb)

Merge pull request #308 from fabianofranz/restore_backwards_compatibilityEric Parisbefore adoptiondbb7c2d

line 331why, one step back
331 c.Usage()

Merge pull request #303 from fabianofranz/testing_help_exiting_1Eric Parisbefore adoptiona272c3c

lines 332–335why, one step back
332 c.output = tmpOutput
333 return bb.String()334}335

Merge pull request #283 from dnephin/add_flag_usage_funcEric Parisbefore adoptionb620a75

lines 336–351why, one step back
336// FlagErrorFunc returns either the function set by SetFlagErrorFunc for this
337// command or a parent, or it returns a function which returns the original338// error.339func (c *Command) FlagErrorFunc() (f func(*Command, error) error) {340 if c.flagErrorFunc != nil {341 return c.flagErrorFunc342 }343344 if c.HasParent() {345 return c.parent.FlagErrorFunc()346 }347 return func(c *Command, err error) error {348 return err349 }350}351

Merge pull request #260 from ianwalter/code-cleanupEric Parisbefore adoptionb0d571e

line 352why, one step back
352var minUsagePadding = 25

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 353why, one step back
353

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 354why, one step back
354// UsagePadding return padding for the usage.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 355–358why, one step back
355func (c *Command) UsagePadding() int {
356 if c.parent == nil || minUsagePadding > c.parent.commandsMaxUseLen {357 return minUsagePadding358 }

Merge pull request #260 from ianwalter/code-cleanupEric Parisbefore adoptionb0d571e

line 359why, one step back
359 return c.parent.commandsMaxUseLen

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 360–361why, one step back
360}
361

Merge pull request #260 from ianwalter/code-cleanupEric Parisbefore adoptionb0d571e

line 362why, one step back
362var minCommandPathPadding = 11

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 363why, one step back
363

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 364why, one step back
364// CommandPathPadding return padding for the command path.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 365–368why, one step back
365func (c *Command) CommandPathPadding() int {
366 if c.parent == nil || minCommandPathPadding > c.parent.commandsMaxCommandPathLen {367 return minCommandPathPadding368 }

Merge pull request #260 from ianwalter/code-cleanupEric Parisbefore adoptionb0d571e

line 369why, one step back
369 return c.parent.commandsMaxCommandPathLen

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 370–371why, one step back
370}
371

Merge pull request #260 from ianwalter/code-cleanupEric Parisbefore adoptionb0d571e

line 372why, one step back
372var minNamePadding = 11

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 373why, one step back
373

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 374why, one step back
374// NamePadding returns padding for the name.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 375–378why, one step back
375func (c *Command) NamePadding() int {
376 if c.parent == nil || minNamePadding > c.parent.commandsMaxNameLen {377 return minNamePadding378 }

Merge pull request #260 from ianwalter/code-cleanupEric Parisbefore adoptionb0d571e

line 379why, one step back
379 return c.parent.commandsMaxNameLen

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 380–381why, one step back
380}
381

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 382why, one step back
382// UsageTemplate returns usage template for the command.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 383–389why, one step back
383func (c *Command) UsageTemplate() string {
384 if c.usageTemplate != "" {385 return c.usageTemplate386 }387388 if c.HasParent() {389 return c.parent.UsageTemplate()

Merge pull request #260 from ianwalter/code-cleanupEric Parisbefore adoptionb0d571e

lines 390–391why, one step back
390 }
391 return `Usage:{{if .Runnable}}

Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af

lines 392–393why, one step back
392 {{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
393 {{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 394–395why, one step back
394
395Aliases:

Keep only one newline between Aliases and Flags in help (#410)Albert Nigmatzianovbefore adoption9350054

line 396why, one step back
396 {{.NameAndAliases}}{{end}}{{if .HasExample}}

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 397–398why, one step back
397
398Examples:

Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af

line 399why, one step back
399{{.Example}}{{end}}{{if .HasAvailableSubCommands}}

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 400why, one step back
400

Some fixes related to help command (#394)Albert Nigmatzianovbefore adoptionfcd0c5a

lines 401–402why, one step back
401Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
402 {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 403–404why, one step back
403
404Flags:

Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af

line 405why, one step back
405{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 406–407why, one step back
406
407Global Flags:

Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af

line 408why, one step back
408{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 409why, one step back
409

Rename IsHelpCommand to IsAdditionalHelpTopicCommand (#398)Albert Nigmatzianovbefore adoption0960ff7

line 410why, one step back
410Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}

Some fixes related to help command (#394)Albert Nigmatzianovbefore adoptionfcd0c5a

line 411why, one step back
411 {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 412–416why, one step back
412
413Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}414`415}416

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 417why, one step back
417// HelpTemplate return help template for the command.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 418–424why, one step back
418func (c *Command) HelpTemplate() string {
419 if c.helpTemplate != "" {420 return c.helpTemplate421 }422423 if c.HasParent() {424 return c.parent.HelpTemplate()

Merge pull request #260 from ianwalter/code-cleanupEric Parisbefore adoptionb0d571e

line 425why, one step back
425 }

Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af

line 426why, one step back
426 return `{{with (or .Long .Short)}}{{. | trimTrailingWhitespaces}}

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 427–430why, one step back
427
428{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`429}430

Add support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce

lines 431–443why, one step back
431// VersionTemplate return version template for the command.
432func (c *Command) VersionTemplate() string {433 if c.versionTemplate != "" {434 return c.versionTemplate435 }436437 if c.HasParent() {438 return c.parent.VersionTemplate()439 }440 return `{{with .Name}}{{printf "%s " .}}{{end}}{{printf "version %s" .Version}}441`442}443

Use pflag.ShorthandLookupAlbert Nigmatzianovbefore adoption4061f41

lines 444–445why, one step back
444func hasNoOptDefVal(name string, fs *flag.FlagSet) bool {
445 flag := fs.Lookup(name)

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 446–448why, one step back
446 if flag == nil {
447 return false448 }

Simplify stripFlagsAlbert Nigmatzianovbefore adoptionf4f10f6

line 449why, one step back
449 return flag.NoOptDefVal != ""

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 450–451why, one step back
450}
451

Fix subcommand locations with NoOptDefValue flags (#391)Eric Parisbefore adoption92ea23a

line 452why, one step back
452func shortHasNoOptDefVal(name string, fs *flag.FlagSet) bool {

Use pflag.ShorthandLookupAlbert Nigmatzianovbefore adoption4061f41

lines 453–461why, one step back
453 if len(name) == 0 {
454 return false455 }456457 flag := fs.ShorthandLookup(name[:1])458 if flag == nil {459 return false460 }461 return flag.NoOptDefVal != ""

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 462–464why, one step back
462}
463464func stripFlags(args []string, c *Command) []string {

Simplify stripFlags functionAlbert Nigmatzianovbefore adoption9890b7b

line 465why, one step back
465 if len(args) == 0 {

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 466–470why, one step back
466 return args
467 }468 c.mergePersistentFlags()469470 commands := []string{}

Simplify stripFlagsAlbert Nigmatzianovbefore adoptionf4f10f6

line 471why, one step back
471 flags := c.Flags()

Simplify stripFlags functionAlbert Nigmatzianovbefore adoption9890b7b

lines 472–475why, one step back
472
473Loop:474 for len(args) > 0 {475 s := args[0]

Simplify stripFlagsAlbert Nigmatzianovbefore adoptionf4f10f6

line 476why, one step back
476 args = args[1:]

Fix tests so they give correct args (#445)Albert Nigmatzianovbefore adoption51b7cf5

lines 477–489why, one step back
477 switch {
478 case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], flags):479 // If '--flag arg' then480 // delete arg from args.481 fallthrough // (do the same as below)482 case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags):483 // If '-f arg' then484 // delete 'arg' from args or break the loop if len(args) <= 1.485 if len(args) <= 1 {486 break Loop487 } else {488 args = args[1:]489 continue

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 490why, one step back
490 }

Fix tests so they give correct args (#445)Albert Nigmatzianovbefore adoption51b7cf5

lines 491–492why, one step back
491 case s != "" && !strings.HasPrefix(s, "-"):
492 commands = append(commands, s)

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 493–512why, one step back
493 }
494 }495496 return commands497}498499// argsMinusFirstX removes only the first x from args. Otherwise, commands that look like500// openshift admin policy add-role-to-user admin my-user, lose the admin argument (arg[4]).501func argsMinusFirstX(args []string, x string) []string {502 for i, y := range args {503 if x == y {504 ret := []string{}505 ret = append(ret, args[:i]...)506 ret = append(ret, args[i+1:]...)507 return ret508 }509 }510 return args511}512

Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03

lines 513–517why, one step back
513func isFlagArg(arg string) bool {
514 return ((len(arg) >= 3 && arg[1] == '-') ||515 (len(arg) >= 2 && arg[0] == '-' && arg[1] != '-'))516}517

Add a tags field for user defined tags on a Command. (#359)Daniel Nephinbefore adoption99600f6

line 518why, one step back
518// Find the target command given the args and command tree

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 519–529why, one step back
519// Meant to be run on the highest node. Only searches down.
520func (c *Command) Find(args []string) (*Command, []string, error) {521 var innerfind func(*Command, []string) (*Command, []string)522523 innerfind = func(c *Command, innerArgs []string) (*Command, []string) {524 argsWOflags := stripFlags(innerArgs, c)525 if len(argsWOflags) == 0 {526 return c, innerArgs527 }528 nextSubCmd := argsWOflags[0]529

Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03

lines 530–532why, one step back
530 cmd := c.findNext(nextSubCmd)
531 if cmd != nil {532 return innerfind(cmd, argsMinusFirstX(innerArgs, nextSubCmd))

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 533–537why, one step back
533 }
534 return c, innerArgs535 }536537 commandFound, a := innerfind(c, args)

Refactor TakesArgs to use an interface for arg validation.Daniel Nephinbefore adoptionf20b4e9

lines 538–539why, one step back
538 if commandFound.Args == nil {
539 return commandFound, a, legacyArgs(commandFound, stripFlags(a, commandFound))

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 540–541why, one step back
540 }
541 return commandFound, a, nil

Allow commands to explicitly state if they do, or do not take arbitrary argumentsEric Parisbefore adoptiond89c499

lines 542–543why, one step back
542}
543

Refactor TakesArgs to use an interface for arg validation.Daniel Nephinbefore adoptionf20b4e9

line 544why, one step back
544func (c *Command) findSuggestions(arg string) string {

Allow commands to explicitly state if they do, or do not take arbitrary argumentsEric Parisbefore adoptiond89c499

lines 545–551why, one step back
545 if c.DisableSuggestions {
546 return ""547 }548 if c.SuggestionsMinimumDistance <= 0 {549 c.SuggestionsMinimumDistance = 2550 }551 suggestionsString := ""

Refactor TakesArgs to use an interface for arg validation.Daniel Nephinbefore adoptionf20b4e9

line 552why, one step back
552 if suggestions := c.SuggestionsFor(arg); len(suggestions) > 0 {

Allow commands to explicitly state if they do, or do not take arbitrary argumentsEric Parisbefore adoptiond89c499

lines 553–558why, one step back
553 suggestionsString += "\n\nDid you mean this?\n"
554 for _, s := range suggestions {555 suggestionsString += fmt.Sprintf("\t%v\n", s)556 }557 }558 return suggestionsString

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 559–560why, one step back
559}
560

Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03

lines 561–564why, one step back
561func (c *Command) findNext(next string) *Command {
562 matches := make([]*Command, 0)563 for _, cmd := range c.commands {564 if cmd.Name() == next || cmd.HasAlias(next) {

Add `CalledAs` method to cobra.Command (w/ tests) (#567)Tim Peoplesbefore adoptioneb58983

line 565why, one step back
565 cmd.commandCalledAs.name = next

Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03

lines 566–575why, one step back
566 return cmd
567 }568 if EnablePrefixMatching && cmd.hasNameOrAliasPrefix(next) {569 matches = append(matches, cmd)570 }571 }572573 if len(matches) == 1 {574 return matches[0]575 }

Add `CalledAs` method to cobra.Command (w/ tests) (#567)Tim Peoplesbefore adoptioneb58983

line 576why, one step back
576

Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03

lines 577–622why, one step back
577 return nil
578}579580// Traverse the command tree to find the command, and parse args for581// each parent.582func (c *Command) Traverse(args []string) (*Command, []string, error) {583 flags := []string{}584 inFlag := false585586 for i, arg := range args {587 switch {588 // A long flag with a space separated value589 case strings.HasPrefix(arg, "--") && !strings.Contains(arg, "="):590 // TODO: this isn't quite right, we should really check ahead for 'true' or 'false'591 inFlag = !hasNoOptDefVal(arg[2:], c.Flags())592 flags = append(flags, arg)593 continue594 // A short flag with a space separated value595 case strings.HasPrefix(arg, "-") && !strings.Contains(arg, "=") && len(arg) == 2 && !shortHasNoOptDefVal(arg[1:], c.Flags()):596 inFlag = true597 flags = append(flags, arg)598 continue599 // The value for a flag600 case inFlag:601 inFlag = false602 flags = append(flags, arg)603 continue604 // A flag without a value, or with an `=` separated value605 case isFlagArg(arg):606 flags = append(flags, arg)607 continue608 }609610 cmd := c.findNext(arg)611 if cmd == nil {612 return c, args, nil613 }614615 if err := c.ParseFlags(flags); err != nil {616 return nil, args, err617 }618 return cmd.Traverse(args[i+1:])619 }620 return c, args, nil621}622

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 623why, one step back
623// SuggestionsFor provides suggestions for the typedName.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 624–643why, one step back
624func (c *Command) SuggestionsFor(typedName string) []string {
625 suggestions := []string{}626 for _, cmd := range c.commands {627 if cmd.IsAvailableCommand() {628 levenshteinDistance := ld(typedName, cmd.Name(), true)629 suggestByLevenshtein := levenshteinDistance <= c.SuggestionsMinimumDistance630 suggestByPrefix := strings.HasPrefix(strings.ToLower(cmd.Name()), strings.ToLower(typedName))631 if suggestByLevenshtein || suggestByPrefix {632 suggestions = append(suggestions, cmd.Name())633 }634 for _, explicitSuggestion := range cmd.SuggestFor {635 if strings.EqualFold(typedName, explicitSuggestion) {636 suggestions = append(suggestions, cmd.Name())637 }638 }639 }640 }641 return suggestions642}643

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 644why, one step back
644// VisitParents visits all parents of the command and invokes fn on each parent.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 645why, one step back
645func (c *Command) VisitParents(fn func(*Command)) {

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

lines 646–648why, one step back
646 if c.HasParent() {
647 fn(c.Parent())648 c.Parent().VisitParents(fn)

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 649–651why, one step back
649 }
650}651

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 652why, one step back
652// Root finds root command.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 653why, one step back
653func (c *Command) Root() *Command {

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

lines 654–655why, one step back
654 if c.HasParent() {
655 return c.Parent().Root()

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 656why, one step back
656 }

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

line 657why, one step back
657 return c

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 658–659why, one step back
658}
659

Improve testsAlbert Nigmatzianovbefore adoption65c8acb

lines 660–661why, one step back
660// ArgsLenAtDash will return the length of c.Flags().Args at the moment
661// when a -- was found during args parsing.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 662–674why, one step back
662func (c *Command) ArgsLenAtDash() int {
663 return c.Flags().ArgsLenAtDash()664}665666func (c *Command) execute(a []string) (err error) {667 if c == nil {668 return fmt.Errorf("Called Execute() on a nil Command")669 }670671 if len(c.Deprecated) > 0 {672 c.Printf("Command %q is deprecated, %s\n", c.Name(), c.Deprecated)673 }674

Add support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce

line 675why, one step back
675 // initialize help and version flag at the last point possible to allow for user

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 676why, one step back
676 // overriding

Make initHelpFlag publicAlbert Nigmatzianovbefore adoptionfe69f2e

line 677why, one step back
677 c.InitDefaultHelpFlag()

Add support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce

line 678why, one step back
678 c.InitDefaultVersionFlag()

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 679–681why, one step back
679
680 err = c.ParseFlags(a)681 if err != nil {

Merge pull request #283 from dnephin/add_flag_usage_funcEric Parisbefore adoptionb620a75

line 682why, one step back
682 return c.FlagErrorFunc()(c, err)

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 683why, one step back
683 }

Make initHelpFlag publicAlbert Nigmatzianovbefore adoptionfe69f2e

lines 684–685why, one step back
684
685 // If help is called, regardless of other flags, return we want help.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 686–689why, one step back
686 // Also say we need help if the command isn't runnable.
687 helpVal, err := c.Flags().GetBool("help")688 if err != nil {689 // should be impossible to get here as we always declare a help

Make initHelpFlag publicAlbert Nigmatzianovbefore adoptionfe69f2e

line 690why, one step back
690 // flag in InitDefaultHelpFlag()

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 691–693why, one step back
691 c.Println("\"help\" flag declared as non-bool. Please correct your code")
692 return err693 }

Merge pull request #303 from fabianofranz/testing_help_exiting_1Eric Parisbefore adoptiona272c3c

line 694why, one step back
694

Ensure that '--version' flag works properly for root command (#595)Nick Miyakebefore adoptionccaecb1

line 695why, one step back
695 if helpVal {

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 696–698why, one step back
696 return flag.ErrHelp
697 }698

Add support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce

lines 699–712why, one step back
699 // for back-compat, only add version flag behavior if version is defined
700 if c.Version != "" {701 versionVal, err := c.Flags().GetBool("version")702 if err != nil {703 c.Println("\"version\" flag declared as non-bool. Please correct your code")704 return err705 }706 if versionVal {707 err := tmpl(c.OutOrStdout(), c.VersionTemplate(), c)708 if err != nil {709 c.Println(err)710 }711 return err712 }

Ensure that '--version' flag works properly for root command (#595)Nick Miyakebefore adoptionccaecb1

lines 713–716why, one step back
713 }
714715 if !c.Runnable() {716 return flag.ErrHelp

Add support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce

lines 717–718why, one step back
717 }
718

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 719why, one step back
719 c.preRun()

Merge pull request #289 from markbates/disable-flagsEric Parisbefore adoptionf447048

line 720why, one step back
720

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 721why, one step back
721 argWoFlags := c.Flags().Args()

Merge pull request #289 from markbates/disable-flagsEric Parisbefore adoptionf447048

lines 722–724why, one step back
722 if c.DisableFlagParsing {
723 argWoFlags = a724 }

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 725why, one step back
725

Refactor TakesArgs to use an interface for arg validation.Daniel Nephinbefore adoptionf20b4e9

lines 726–729why, one step back
726 if err := c.ValidateArgs(argWoFlags); err != nil {
727 return err728 }729

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 730–748why, one step back
730 for p := c; p != nil; p = p.Parent() {
731 if p.PersistentPreRunE != nil {732 if err := p.PersistentPreRunE(c, argWoFlags); err != nil {733 return err734 }735 break736 } else if p.PersistentPreRun != nil {737 p.PersistentPreRun(c, argWoFlags)738 break739 }740 }741 if c.PreRunE != nil {742 if err := c.PreRunE(c, argWoFlags); err != nil {743 return err744 }745 } else if c.PreRun != nil {746 c.PreRun(c, argWoFlags)747 }748

enforce required flags (#502)Di Xubefore adoption4d6af28

lines 749–751why, one step back
749 if err := c.validateRequiredFlags(); err != nil {
750 return err751 }

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 752–786why, one step back
752 if c.RunE != nil {
753 if err := c.RunE(c, argWoFlags); err != nil {754 return err755 }756 } else {757 c.Run(c, argWoFlags)758 }759 if c.PostRunE != nil {760 if err := c.PostRunE(c, argWoFlags); err != nil {761 return err762 }763 } else if c.PostRun != nil {764 c.PostRun(c, argWoFlags)765 }766 for p := c; p != nil; p = p.Parent() {767 if p.PersistentPostRunE != nil {768 if err := p.PersistentPostRunE(c, argWoFlags); err != nil {769 return err770 }771 break772 } else if p.PersistentPostRun != nil {773 p.PersistentPostRun(c, argWoFlags)774 break775 }776 }777778 return nil779}780781func (c *Command) preRun() {782 for _, x := range initializers {783 x()784 }785}786

Fix typosAlbert Nigmatzianovbefore adoptione606913

line 787why, one step back
787// Execute uses the args (os.Args[1:] by default)

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 788–794why, one step back
788// and run through the command tree finding appropriate matches
789// for commands and then corresponding flags.790func (c *Command) Execute() error {791 _, err := c.ExecuteC()792 return err793}794

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 795why, one step back
795// ExecuteC executes the command.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 796–808why, one step back
796func (c *Command) ExecuteC() (cmd *Command, err error) {
797 // Regardless of what command execute is called on, run on Root only798 if c.HasParent() {799 return c.Root().ExecuteC()800 }801802 // windows hook803 if preExecHookFn != nil {804 preExecHookFn(c)805 }806807 // initialize help as the last point possible to allow for user808 // overriding

Make initHelpCmd public (#436)Albert Nigmatzianovbefore adoption52ae6a1

line 809why, one step back
809 c.InitDefaultHelpCmd()

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 810–819why, one step back
810
811 var args []string812813 // Workaround FAIL with "go test -v" or "cobra.test -test.v", see #155814 if c.args == nil && filepath.Base(os.Args[0]) != "cobra.test" {815 args = os.Args[1:]816 } else {817 args = c.args818 }819

Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03

lines 820–825why, one step back
820 var flags []string
821 if c.TraverseChildren {822 cmd, flags, err = c.Traverse(args)823 } else {824 cmd, flags, err = c.Find(args)825 }

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 826–836why, one step back
826 if err != nil {
827 // If found parse to a subcommand and then failed, talk about the subcommand828 if cmd != nil {829 c = cmd830 }831 if !c.SilenceErrors {832 c.Println("Error:", err.Error())833 c.Printf("Run '%v --help' for usage.\n", c.CommandPath())834 }835 return c, err836 }

Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03

line 837why, one step back
837

Add `CalledAs` method to cobra.Command (w/ tests) (#567)Tim Peoplesbefore adoptioneb58983

lines 838–842why, one step back
838 cmd.commandCalledAs.called = true
839 if cmd.commandCalledAs.name == "" {840 cmd.commandCalledAs.name = cmd.Name()841 }842

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 843–863why, one step back
843 err = cmd.execute(flags)
844 if err != nil {845 // Always show help if requested, even if SilenceErrors is in846 // effect847 if err == flag.ErrHelp {848 cmd.HelpFunc()(cmd, args)849 return cmd, nil850 }851852 // If root command has SilentErrors flagged,853 // all subcommands should respect it854 if !cmd.SilenceErrors && !c.SilenceErrors {855 c.Println("Error:", err.Error())856 }857858 // If root command has SilentUsage flagged,859 // all subcommands should respect it860 if !cmd.SilenceUsage && !c.SilenceUsage {861 c.Println(cmd.UsageString())862 }863 }

Fix typosAlbert Nigmatzianovbefore adoptione606913

line 864why, one step back
864 return cmd, err

Refactor TakesArgs to use an interface for arg validation.Daniel Nephinbefore adoptionf20b4e9

lines 865–871why, one step back
865}
866867func (c *Command) ValidateArgs(args []string) error {868 if c.Args == nil {869 return nil870 }871 return c.Args(c, args)

enforce required flags (#502)Di Xubefore adoption4d6af28

lines 872–887why, one step back
872}
873874func (c *Command) validateRequiredFlags() error {875 flags := c.Flags()876 missingFlagNames := []string{}877 flags.VisitAll(func(pflag *flag.Flag) {878 requiredAnnotation, found := pflag.Annotations[BashCompOneRequiredFlag]879 if !found {880 return881 }882 if (requiredAnnotation[0] == "true") && !pflag.Changed {883 missingFlagNames = append(missingFlagNames, pflag.Name)884 }885 })886887 if len(missingFlagNames) > 0 {

Update error message for missing required flags (#580)Nick Miyakebefore adoption19e54c4

line 888why, one step back
888 return fmt.Errorf(`required flag(s) "%s" not set`, strings.Join(missingFlagNames, `", "`))

enforce required flags (#502)Di Xubefore adoption4d6af28

lines 889–890why, one step back
889 }
890 return nil

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 891–892why, one step back
891}
892

Make initHelpFlag publicAlbert Nigmatzianovbefore adoptionfe69f2e

lines 893–894why, one step back
893// InitDefaultHelpFlag adds default help flag to c.
894// It is called automatically by executing the c or by calling help and usage.

Add comment for InitDefaultHelpFlagAlbert Nigmatzianovbefore adoptiond83a1d7

line 895why, one step back
895// If c already has help flag, it will do nothing.

Make initHelpFlag publicAlbert Nigmatzianovbefore adoptionfe69f2e

line 896why, one step back
896func (c *Command) InitDefaultHelpFlag() {

Merge pull request #286 from dnephin/merge_flags_before_adding_helpEric Parisbefore adoptionec2fe78

line 897why, one step back
897 c.mergePersistentFlags()

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 898why, one step back
898 if c.Flags().Lookup("help") == nil {

Initialize help flag in initHelpCmdAlbert Nigmatzianovbefore adoption867d414

lines 899–905why, one step back
899 usage := "help for "
900 if c.Name() == "" {901 usage += "this command"902 } else {903 usage += c.Name()904 }905 c.Flags().BoolP("help", "h", false, usage)

Add support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce

lines 906–926why, one step back
906 }
907}908909// InitDefaultVersionFlag adds default version flag to c.910// It is called automatically by executing the c.911// If c already has a version flag, it will do nothing.912// If c.Version is empty, it will do nothing.913func (c *Command) InitDefaultVersionFlag() {914 if c.Version == "" {915 return916 }917918 c.mergePersistentFlags()919 if c.Flags().Lookup("version") == nil {920 usage := "version for "921 if c.Name() == "" {922 usage += "this command"923 } else {924 usage += c.Name()925 }926 c.Flags().Bool("version", false, usage)

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 927–929why, one step back
927 }
928}929

Make initHelpCmd public (#436)Albert Nigmatzianovbefore adoption52ae6a1

lines 930–933why, one step back
930// InitDefaultHelpCmd adds default help command to c.
931// It is called automatically by executing the c or by calling help and usage.932// If c already has help command or c has no subcommands, it will do nothing.933func (c *Command) InitDefaultHelpCmd() {

Fix InitDefaultHelpCmd when custom help command is setAlbert Nigmatzianovbefore adoption8c6fa02

line 934why, one step back
934 if !c.HasSubCommands() {

Make initHelpCmd public (#436)Albert Nigmatzianovbefore adoption52ae6a1

lines 935–936why, one step back
935 return
936 }

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 937why, one step back
937

Fix InitDefaultHelpCmd when custom help command is setAlbert Nigmatzianovbefore adoption8c6fa02

lines 938–942why, one step back
938 if c.helpCommand == nil {
939 c.helpCommand = &Command{940 Use: "help [command]",941 Short: "Help about any command",942 Long: `Help provides help for any command in the application.

Delete tab in the long description of helpAlbert Nigmatzianovbefore adoptiond994347

line 943why, one step back
943Simply type ` + c.Name() + ` help [path to command] for full details.`,

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 944why, one step back
944

Fix InitDefaultHelpCmd when custom help command is setAlbert Nigmatzianovbefore adoption8c6fa02

lines 945–955why, one step back
945 Run: func(c *Command, args []string) {
946 cmd, _, e := c.Root().Find(args)947 if cmd == nil || e != nil {948 c.Printf("Unknown help topic %#q\n", args)949 c.Root().Usage()950 } else {951 cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown952 cmd.Help()953 }954 },955 }

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 956why, one step back
956 }

remove help command before adding it (#399)Tamir Dubersteinbefore adoption7be4bed

line 957why, one step back
957 c.RemoveCommand(c.helpCommand)

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 958–960why, one step back
958 c.AddCommand(c.helpCommand)
959}960

Edit ResetFlags and ResetCommands descriptionsAlbert Nigmatzianovbefore adoptiond6a4305

line 961why, one step back
961// ResetCommands delete parent, subcommand and help command from c.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 962why, one step back
962func (c *Command) ResetCommands() {

Improve consistency of flags when using SetGlobalNormalizationFunc (#522)Diego Becciolinibefore adoption0dacccf

line 963why, one step back
963 c.parent = nil

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 964–965why, one step back
964 c.commands = nil
965 c.helpCommand = nil

Add only missing persistent flags of parents in mergePersistentFlagsAlbert Nigmatzianovbefore adoption3d89ed4

line 966why, one step back
966 c.parentsPflags = nil

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 967–968why, one step back
967}
968

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 969why, one step back
969// Sorts commands by their names.

Merge pull request #293 from andreykurilin/sort_commandsEric Parisbefore adoptionbc81c21

lines 970–976why, one step back
970type commandSorterByName []*Command
971972func (c commandSorterByName) Len() int { return len(c) }973func (c commandSorterByName) Swap(i, j int) { c[i], c[j] = c[j], c[i] }974func (c commandSorterByName) Less(i, j int) bool { return c[i].Name() < c[j].Name() }975976// Commands returns a sorted slice of child commands.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 977why, one step back
977func (c *Command) Commands() []*Command {

Merge pull request #293 from andreykurilin/sort_commandsEric Parisbefore adoptionbc81c21

line 978why, one step back
978 // do not sort commands if it already sorted or sorting was disabled

Merge pull request #303 from fabianofranz/testing_help_exiting_1Eric Parisbefore adoptiona272c3c

line 979why, one step back
979 if EnableCommandSorting && !c.commandsAreSorted {

Merge pull request #293 from andreykurilin/sort_commandsEric Parisbefore adoptionbc81c21

lines 980–982why, one step back
980 sort.Sort(commandSorterByName(c.commands))
981 c.commandsAreSorted = true982 }

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 983–1010why, one step back
983 return c.commands
984}985986// AddCommand adds one or more commands to this parent command.987func (c *Command) AddCommand(cmds ...*Command) {988 for i, x := range cmds {989 if cmds[i] == c {990 panic("Command can't be a child of itself")991 }992 cmds[i].parent = c993 // update max lengths994 usageLen := len(x.Use)995 if usageLen > c.commandsMaxUseLen {996 c.commandsMaxUseLen = usageLen997 }998 commandPathLen := len(x.CommandPath())999 if commandPathLen > c.commandsMaxCommandPathLen {1000 c.commandsMaxCommandPathLen = commandPathLen1001 }1002 nameLen := len(x.Name())1003 if nameLen > c.commandsMaxNameLen {1004 c.commandsMaxNameLen = nameLen1005 }1006 // If global normalization function exists, update all children1007 if c.globNormFunc != nil {1008 x.SetGlobalNormalizationFunc(c.globNormFunc)1009 }1010 c.commands = append(c.commands, x)

Merge pull request #293 from andreykurilin/sort_commandsEric Parisbefore adoptionbc81c21

line 1011why, one step back
1011 c.commandsAreSorted = false

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1012–1014why, one step back
1012 }
1013}1014

Merge pull request #278 from kevintpeng/patch-1Eric Parisbefore adoptionf368244

line 1015why, one step back
1015// RemoveCommand removes one or more commands from a parent command.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1016–1048why, one step back
1016func (c *Command) RemoveCommand(cmds ...*Command) {
1017 commands := []*Command{}1018main:1019 for _, command := range c.commands {1020 for _, cmd := range cmds {1021 if command == cmd {1022 command.parent = nil1023 continue main1024 }1025 }1026 commands = append(commands, command)1027 }1028 c.commands = commands1029 // recompute all lengths1030 c.commandsMaxUseLen = 01031 c.commandsMaxCommandPathLen = 01032 c.commandsMaxNameLen = 01033 for _, command := range c.commands {1034 usageLen := len(command.Use)1035 if usageLen > c.commandsMaxUseLen {1036 c.commandsMaxUseLen = usageLen1037 }1038 commandPathLen := len(command.CommandPath())1039 if commandPathLen > c.commandsMaxCommandPathLen {1040 c.commandsMaxCommandPathLen = commandPathLen1041 }1042 nameLen := len(command.Name())1043 if nameLen > c.commandsMaxNameLen {1044 c.commandsMaxNameLen = nameLen1045 }1046 }1047}1048

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1049why, one step back
1049// Print is a convenience method to Print to the defined output, fallback to Stderr if not set.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 1050why, one step back
1050func (c *Command) Print(i ...interface{}) {

Merge pull request #305 from fabianofranz/expose_outEric Parisbefore adoptionb24564e

line 1051why, one step back
1051 fmt.Fprint(c.OutOrStderr(), i...)

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1052–1053why, one step back
1052}
1053

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1054why, one step back
1054// Println is a convenience method to Println to the defined output, fallback to Stderr if not set.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 1055why, one step back
1055func (c *Command) Println(i ...interface{}) {

Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af

line 1056why, one step back
1056 c.Print(fmt.Sprintln(i...))

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1057–1058why, one step back
1057}
1058

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1059why, one step back
1059// Printf is a convenience method to Printf to the defined output, fallback to Stderr if not set.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 1060why, one step back
1060func (c *Command) Printf(format string, i ...interface{}) {

Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af

line 1061why, one step back
1061 c.Print(fmt.Sprintf(format, i...))

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1062–1065why, one step back
1062}
10631064// CommandPath returns the full path to this command.1065func (c *Command) CommandPath() string {

Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af

lines 1066–1067why, one step back
1066 if c.HasParent() {
1067 return c.Parent().CommandPath() + " " + c.Name()

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 1068why, one step back
1068 }

Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af

line 1069why, one step back
1069 return c.Name()

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1070–1071why, one step back
1070}
1071

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1072why, one step back
1072// UseLine puts out the full usage for a given command (including parents).

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 1073why, one step back
1073func (c *Command) UseLine() string {

Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af

line 1074why, one step back
1074 var useline string

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 1075why, one step back
1075 if c.HasParent() {

Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af

lines 1076–1078why, one step back
1076 useline = c.parent.CommandPath() + " " + c.Use
1077 } else {1078 useline = c.Use

Disable flags in UseLine (#552)Daniel Nephinbefore adoption34ceca5

lines 1079–1081why, one step back
1079 }
1080 if c.DisableFlagsInUseLine {1081 return useline

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 1082why, one step back
1082 }

Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af

lines 1083–1086why, one step back
1083 if c.HasAvailableFlags() && !strings.Contains(useline, "[flags]") {
1084 useline += " [flags]"1085 }1086 return useline

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1087–1088why, one step back
1087}
1088

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 1089why, one step back
1089// DebugFlags used to determine which flags have been assigned to which commands

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1090why, one step back
1090// and which persist.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1091–1100why, one step back
1091func (c *Command) DebugFlags() {
1092 c.Println("DebugFlags called on", c.Name())1093 var debugflags func(*Command)10941095 debugflags = func(x *Command) {1096 if x.HasFlags() || x.HasPersistentFlags() {1097 c.Println(x.Name())1098 }1099 if x.HasFlags() {1100 x.flags.VisitAll(func(f *flag.Flag) {

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

lines 1101–1102why, one step back
1101 if x.HasPersistentFlags() && x.persistentFlag(f.Name) != nil {
1102 c.Println(" -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, " [LP]")

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1103–1118why, one step back
1103 } else {
1104 c.Println(" -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, " [L]")1105 }1106 })1107 }1108 if x.HasPersistentFlags() {1109 x.pflags.VisitAll(func(f *flag.Flag) {1110 if x.HasFlags() {1111 if x.flags.Lookup(f.Name) == nil {1112 c.Println(" -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, " [P]")1113 }1114 } else {1115 c.Println(" -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, " [P]")1116 }1117 })1118 }

Revert "Get rid of flagErrorBuf (#417)"Albert Nigmatzianovbefore adoption3d7bff8

line 1119why, one step back
1119 c.Println(x.flagErrorBuf)

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1120–1131why, one step back
1120 if x.HasSubCommands() {
1121 for _, y := range x.commands {1122 debugflags(y)1123 }1124 }1125 }11261127 debugflags(c)1128}11291130// Name returns the command's name: the first word in the use line.1131func (c *Command) Name() string {

Delete name field from Command (#547)Albert Nigmatzianovbefore adoption40f1880

lines 1132–1135why, one step back
1132 name := c.Use
1133 i := strings.Index(name, " ")1134 if i >= 0 {1135 name = name[:i]

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 1136why, one step back
1136 }

Delete name field from Command (#547)Albert Nigmatzianovbefore adoption40f1880

line 1137why, one step back
1137 return name

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1138–1139why, one step back
1138}
1139

Merge pull request #260 from ianwalter/code-cleanupEric Parisbefore adoptionb0d571e

line 1140why, one step back
1140// HasAlias determines if a given string is an alias of the command.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1141–1149why, one step back
1141func (c *Command) HasAlias(s string) bool {
1142 for _, a := range c.Aliases {1143 if a == s {1144 return true1145 }1146 }1147 return false1148}1149

Add `CalledAs` method to cobra.Command (w/ tests) (#567)Tim Peoplesbefore adoptioneb58983

lines 1150–1158why, one step back
1150// CalledAs returns the command name or alias that was used to invoke
1151// this command or an empty string if the command has not been called.1152func (c *Command) CalledAs() string {1153 if c.commandCalledAs.called {1154 return c.commandCalledAs.name1155 }1156 return ""1157}1158

Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03

lines 1159–1162why, one step back
1159// hasNameOrAliasPrefix returns true if the Name or any of aliases start
1160// with prefix1161func (c *Command) hasNameOrAliasPrefix(prefix string) bool {1162 if strings.HasPrefix(c.Name(), prefix) {

Add `CalledAs` method to cobra.Command (w/ tests) (#567)Tim Peoplesbefore adoptioneb58983

line 1163why, one step back
1163 c.commandCalledAs.name = c.Name()

Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03

lines 1164–1167why, one step back
1164 return true
1165 }1166 for _, alias := range c.Aliases {1167 if strings.HasPrefix(alias, prefix) {

Add `CalledAs` method to cobra.Command (w/ tests) (#567)Tim Peoplesbefore adoptioneb58983

line 1168why, one step back
1168 c.commandCalledAs.name = alias

Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03

lines 1169–1175why, one step back
1169 return true
1170 }1171 }1172 return false1173}11741175// NameAndAliases returns a list of the command name and all aliases

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1176–1179why, one step back
1176func (c *Command) NameAndAliases() string {
1177 return strings.Join(append([]string{c.Name()}, c.Aliases...), ", ")1178}1179

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 1180why, one step back
1180// HasExample determines if the command has example.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1181–1184why, one step back
1181func (c *Command) HasExample() bool {
1182 return len(c.Example) > 01183}1184

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1185why, one step back
1185// Runnable determines if the command is itself runnable.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1186–1189why, one step back
1186func (c *Command) Runnable() bool {
1187 return c.Run != nil || c.RunE != nil1188}1189

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1190why, one step back
1190// HasSubCommands determines if the command has children commands.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1191–1195why, one step back
1191func (c *Command) HasSubCommands() bool {
1192 return len(c.commands) > 01193}11941195// IsAvailableCommand determines if a command is available as a non-help command

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1196why, one step back
1196// (this includes all non deprecated/hidden commands).

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1197–1212why, one step back
1197func (c *Command) IsAvailableCommand() bool {
1198 if len(c.Deprecated) != 0 || c.Hidden {1199 return false1200 }12011202 if c.HasParent() && c.Parent().helpCommand == c {1203 return false1204 }12051206 if c.Runnable() || c.HasAvailableSubCommands() {1207 return true1208 }12091210 return false1211}1212

Rename IsHelpCommand to IsAdditionalHelpTopicCommand (#398)Albert Nigmatzianovbefore adoption0960ff7

lines 1213–1218why, one step back
1213// IsAdditionalHelpTopicCommand determines if a command is an additional
1214// help topic command; additional help topic command is determined by the1215// fact that it is NOT runnable/hidden/deprecated, and has no sub commands that1216// are runnable/hidden/deprecated.1217// Concrete example: https://github.com/spf13/cobra/issues/393#issuecomment-282741924.1218func (c *Command) IsAdditionalHelpTopicCommand() bool {

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1219–1225why, one step back
1219 // if a command is runnable, deprecated, or hidden it is not a 'help' command
1220 if c.Runnable() || len(c.Deprecated) != 0 || c.Hidden {1221 return false1222 }12231224 // if any non-help sub commands are found, the command is not a 'help' command1225 for _, sub := range c.commands {

Rename IsHelpCommand to IsAdditionalHelpTopicCommand (#398)Albert Nigmatzianovbefore adoption0960ff7

line 1226why, one step back
1226 if !sub.IsAdditionalHelpTopicCommand() {

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1227–1234why, one step back
1227 return false
1228 }1229 }12301231 // the command either has no sub commands, or no non-help sub commands1232 return true1233}1234

Merge pull request #327 from bogem/fixesEric Parisbefore adoption1f47171

line 1235why, one step back
1235// HasHelpSubCommands determines if a command has any available 'help' sub commands

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 1236why, one step back
1236// that need to be shown in the usage/help default template under 'additional help

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1237why, one step back
1237// topics'.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1238–1240why, one step back
1238func (c *Command) HasHelpSubCommands() bool {
1239 // return true on the first found available 'help' sub command1240 for _, sub := range c.commands {

Rename IsHelpCommand to IsAdditionalHelpTopicCommand (#398)Albert Nigmatzianovbefore adoption0960ff7

line 1241why, one step back
1241 if sub.IsAdditionalHelpTopicCommand() {

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1242–1250why, one step back
1242 return true
1243 }1244 }12451246 // the command either has no sub commands, or no available 'help' sub commands1247 return false1248}12491250// HasAvailableSubCommands determines if a command has available sub commands that

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1251why, one step back
1251// need to be shown in the usage/help default template under 'available commands'.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1252–1260why, one step back
1252func (c *Command) HasAvailableSubCommands() bool {
1253 // return true on the first found available (non deprecated/help/hidden)1254 // sub command1255 for _, sub := range c.commands {1256 if sub.IsAvailableCommand() {1257 return true1258 }1259 }1260

Fix typo (#573)Pavol Loffaybefore adoptionf634327

line 1261why, one step back
1261 // the command either has no sub commands, or no available (non deprecated/help/hidden)

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1262–1265why, one step back
1262 // sub commands
1263 return false1264}1265

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1266why, one step back
1266// HasParent determines if the command is a child command.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1267–1270why, one step back
1267func (c *Command) HasParent() bool {
1268 return c.parent != nil1269}1270

fix grammatical error in command.go (#626)Shamus Taylorbefore adoption3a7eb14

line 1271why, one step back
1271// GlobalNormalizationFunc returns the global normalization function or nil if it doesn't exist.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1272–1275why, one step back
1272func (c *Command) GlobalNormalizationFunc() func(f *flag.FlagSet, name string) flag.NormalizedName {
1273 return c.globNormFunc1274}1275

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 1276why, one step back
1276// Flags returns the complete FlagSet that applies

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1277why, one step back
1277// to this command (local and persistent declared here and by all parents).

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1278–1280why, one step back
1278func (c *Command) Flags() *flag.FlagSet {
1279 if c.flags == nil {1280 c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)

Revert "Get rid of flagErrorBuf (#417)"Albert Nigmatzianovbefore adoption3d7bff8

lines 1281–1284why, one step back
1281 if c.flagErrorBuf == nil {
1282 c.flagErrorBuf = new(bytes.Buffer)1283 }1284 c.flags.SetOutput(c.flagErrorBuf)

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 1285why, one step back
1285 }

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

line 1286why, one step back
1286

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1287–1289why, one step back
1287 return c.flags
1288}1289

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1290why, one step back
1290// LocalNonPersistentFlags are flags specific to this command which will NOT persist to subcommands.

Merge pull request #288 from eparis/flags-vs-commandsEric Parisbefore adoptionc21ee9d

lines 1291–1302why, one step back
1291func (c *Command) LocalNonPersistentFlags() *flag.FlagSet {
1292 persistentFlags := c.PersistentFlags()12931294 out := flag.NewFlagSet(c.Name(), flag.ContinueOnError)1295 c.LocalFlags().VisitAll(func(f *flag.Flag) {1296 if persistentFlags.Lookup(f.Name) == nil {1297 out.AddFlag(f)1298 }1299 })1300 return out1301}1302

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1303why, one step back
1303// LocalFlags returns the local FlagSet specifically set in the current command.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1304–1306why, one step back
1304func (c *Command) LocalFlags() *flag.FlagSet {
1305 c.mergePersistentFlags()1306

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

lines 1307–1308why, one step back
1307 if c.lflags == nil {
1308 c.lflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)

Revert "Get rid of flagErrorBuf (#417)"Albert Nigmatzianovbefore adoption3d7bff8

lines 1309–1312why, one step back
1309 if c.flagErrorBuf == nil {
1310 c.flagErrorBuf = new(bytes.Buffer)1311 }1312 c.lflags.SetOutput(c.flagErrorBuf)

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 1313why, one step back
1313 }

Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867

line 1314why, one step back
1314 c.lflags.SortFlags = c.Flags().SortFlags

Improve consistency of flags when using SetGlobalNormalizationFunc (#522)Diego Becciolinibefore adoption0dacccf

lines 1315–1317why, one step back
1315 if c.globNormFunc != nil {
1316 c.lflags.SetNormalizeFunc(c.globNormFunc)1317 }

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

lines 1318–1323why, one step back
1318
1319 addToLocal := func(f *flag.Flag) {1320 if c.lflags.Lookup(f.Name) == nil && c.parentsPflags.Lookup(f.Name) == nil {1321 c.lflags.AddFlag(f)1322 }1323 }

Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867

line 1324why, one step back
1324 c.Flags().VisitAll(addToLocal)

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

lines 1325–1326why, one step back
1325 c.PersistentFlags().VisitAll(addToLocal)
1326 return c.lflags

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1327–1328why, one step back
1327}
1328

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1329why, one step back
1329// InheritedFlags returns all flags which were inherited from parents commands.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1330–1332why, one step back
1330func (c *Command) InheritedFlags() *flag.FlagSet {
1331 c.mergePersistentFlags()1332

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

lines 1333–1334why, one step back
1333 if c.iflags == nil {
1334 c.iflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)

Revert "Get rid of flagErrorBuf (#417)"Albert Nigmatzianovbefore adoption3d7bff8

lines 1335–1338why, one step back
1335 if c.flagErrorBuf == nil {
1336 c.flagErrorBuf = new(bytes.Buffer)1337 }1338 c.iflags.SetOutput(c.flagErrorBuf)

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

lines 1339–1340why, one step back
1339 }
1340

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 1341why, one step back
1341 local := c.LocalFlags()

Improve consistency of flags when using SetGlobalNormalizationFunc (#522)Diego Becciolinibefore adoption0dacccf

lines 1342–1345why, one step back
1342 if c.globNormFunc != nil {
1343 c.iflags.SetNormalizeFunc(c.globNormFunc)1344 }1345

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

lines 1346–1348why, one step back
1346 c.parentsPflags.VisitAll(func(f *flag.Flag) {
1347 if c.iflags.Lookup(f.Name) == nil && local.Lookup(f.Name) == nil {1348 c.iflags.AddFlag(f)

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 1349why, one step back
1349 }

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

lines 1350–1351why, one step back
1350 })
1351 return c.iflags

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1352–1353why, one step back
1352}
1353

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1354why, one step back
1354// NonInheritedFlags returns all flags which were not inherited from parent commands.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1355–1358why, one step back
1355func (c *Command) NonInheritedFlags() *flag.FlagSet {
1356 return c.LocalFlags()1357}1358

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1359why, one step back
1359// PersistentFlags returns the persistent FlagSet specifically set in the current command.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1360–1362why, one step back
1360func (c *Command) PersistentFlags() *flag.FlagSet {
1361 if c.pflags == nil {1362 c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)

Revert "Get rid of flagErrorBuf (#417)"Albert Nigmatzianovbefore adoption3d7bff8

lines 1363–1366why, one step back
1363 if c.flagErrorBuf == nil {
1364 c.flagErrorBuf = new(bytes.Buffer)1365 }1366 c.pflags.SetOutput(c.flagErrorBuf)

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1367–1370why, one step back
1367 }
1368 return c.pflags1369}1370

Edit ResetFlags and ResetCommands descriptionsAlbert Nigmatzianovbefore adoptiond6a4305

line 1371why, one step back
1371// ResetFlags deletes all flags from command.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 1372why, one step back
1372func (c *Command) ResetFlags() {

Revert "Get rid of flagErrorBuf (#417)"Albert Nigmatzianovbefore adoption3d7bff8

lines 1373–1374why, one step back
1373 c.flagErrorBuf = new(bytes.Buffer)
1374 c.flagErrorBuf.Reset()

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 1375why, one step back
1375 c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)

Revert "Get rid of flagErrorBuf (#417)"Albert Nigmatzianovbefore adoption3d7bff8

line 1376why, one step back
1376 c.flags.SetOutput(c.flagErrorBuf)

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 1377why, one step back
1377 c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)

Revert "Get rid of flagErrorBuf (#417)"Albert Nigmatzianovbefore adoption3d7bff8

line 1378why, one step back
1378 c.pflags.SetOutput(c.flagErrorBuf)

Improve consistency of flags when using SetGlobalNormalizationFunc (#522)Diego Becciolinibefore adoption0dacccf

lines 1379–1382why, one step back
1379
1380 c.lflags = nil1381 c.iflags = nil1382 c.parentsPflags = nil

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1383–1384why, one step back
1383}
1384

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 1385why, one step back
1385// HasFlags checks if the command contains any flags (local plus persistent from the entire structure).

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1386–1389why, one step back
1386func (c *Command) HasFlags() bool {
1387 return c.Flags().HasFlags()1388}1389

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 1390why, one step back
1390// HasPersistentFlags checks if the command contains persistent flags.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1391–1394why, one step back
1391func (c *Command) HasPersistentFlags() bool {
1392 return c.PersistentFlags().HasFlags()1393}1394

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 1395why, one step back
1395// HasLocalFlags checks if the command has flags specifically declared locally.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1396–1399why, one step back
1396func (c *Command) HasLocalFlags() bool {
1397 return c.LocalFlags().HasFlags()1398}1399

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 1400why, one step back
1400// HasInheritedFlags checks if the command has flags inherited from its parent command.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1401–1402why, one step back
1401func (c *Command) HasInheritedFlags() bool {
1402 return c.InheritedFlags().HasFlags()

Issue #267: Ensure hidden flags/subcommands do not change helpJohn Schnakebefore adoptione14e47b

lines 1403–1404why, one step back
1403}
1404

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 1405why, one step back
1405// HasAvailableFlags checks if the command contains any flags (local plus persistent from the entire

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1406why, one step back
1406// structure) which are not hidden or deprecated.

Issue #267: Ensure hidden flags/subcommands do not change helpJohn Schnakebefore adoptione14e47b

lines 1407–1410why, one step back
1407func (c *Command) HasAvailableFlags() bool {
1408 return c.Flags().HasAvailableFlags()1409}1410

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 1411why, one step back
1411// HasAvailablePersistentFlags checks if the command contains persistent flags which are not hidden or deprecated.

Issue #267: Ensure hidden flags/subcommands do not change helpJohn Schnakebefore adoptione14e47b

lines 1412–1415why, one step back
1412func (c *Command) HasAvailablePersistentFlags() bool {
1413 return c.PersistentFlags().HasAvailableFlags()1414}1415

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 1416why, one step back
1416// HasAvailableLocalFlags checks if the command has flags specifically declared locally which are not hidden

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1417why, one step back
1417// or deprecated.

Issue #267: Ensure hidden flags/subcommands do not change helpJohn Schnakebefore adoptione14e47b

lines 1418–1421why, one step back
1418func (c *Command) HasAvailableLocalFlags() bool {
1419 return c.LocalFlags().HasAvailableFlags()1420}1421

Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af

line 1422why, one step back
1422// HasAvailableInheritedFlags checks if the command has flags inherited from its parent command which are

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1423why, one step back
1423// not hidden or deprecated.

Issue #267: Ensure hidden flags/subcommands do not change helpJohn Schnakebefore adoptione14e47b

lines 1424–1425why, one step back
1424func (c *Command) HasAvailableInheritedFlags() bool {
1425 return c.InheritedFlags().HasAvailableFlags()

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1426–1427why, one step back
1426}
1427

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1428why, one step back
1428// Flag climbs up the command tree looking for matching flag.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1429–1438why, one step back
1429func (c *Command) Flag(name string) (flag *flag.Flag) {
1430 flag = c.Flags().Lookup(name)14311432 if flag == nil {1433 flag = c.persistentFlag(name)1434 }14351436 return1437}1438

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1439why, one step back
1439// Recursively find matching persistent flag.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1440–1444why, one step back
1440func (c *Command) persistentFlag(name string) (flag *flag.Flag) {
1441 if c.HasPersistentFlags() {1442 flag = c.PersistentFlags().Lookup(name)1443 }1444

Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867

lines 1445–1447why, one step back
1445 if flag == nil {
1446 c.updateParentsPflags()1447 flag = c.parentsPflags.Lookup(name)

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1448–1451why, one step back
1448 }
1449 return1450}1451

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1452why, one step back
1452// ParseFlags parses persistent flag tree and local flags.

Show messages if deprecated flags are usedAlbert Nigmatzianovbefore adoption99b5d83

line 1453why, one step back
1453func (c *Command) ParseFlags(args []string) error {

Merge pull request #289 from markbates/disable-flagsEric Parisbefore adoptionf447048

lines 1454–1456why, one step back
1454 if c.DisableFlagParsing {
1455 return nil1456 }

Show messages if deprecated flags are usedAlbert Nigmatzianovbefore adoption99b5d83

line 1457why, one step back
1457

Create new buffer if not present yet (#549)Thomas Cyronbefore adoption7b2c5ac

lines 1458–1460why, one step back
1458 if c.flagErrorBuf == nil {
1459 c.flagErrorBuf = new(bytes.Buffer)1460 }

Show messages if deprecated flags are usedAlbert Nigmatzianovbefore adoption99b5d83

line 1461why, one step back
1461 beforeErrorBufLen := c.flagErrorBuf.Len()

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 1462why, one step back
1462 c.mergePersistentFlags()

Show messages if deprecated flags are usedAlbert Nigmatzianovbefore adoption99b5d83

lines 1463–1469why, one step back
1463 err := c.Flags().Parse(args)
1464 // Print warnings if they occurred (e.g. deprecated flag messages).1465 if c.flagErrorBuf.Len()-beforeErrorBufLen > 0 && err == nil {1466 c.Print(c.flagErrorBuf.String())1467 }14681469 return err

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1470–1471why, one step back
1470}
1471

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1472why, one step back
1472// Parent returns a commands parent command.

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

lines 1473–1476why, one step back
1473func (c *Command) Parent() *Command {
1474 return c.parent1475}1476

Add only missing persistent flags of parents in mergePersistentFlagsAlbert Nigmatzianovbefore adoption3d89ed4

lines 1477–1479why, one step back
1477// mergePersistentFlags merges c.PersistentFlags() to c.Flags()
1478// and adds missing persistent flags of all parents.1479func (c *Command) mergePersistentFlags() {

Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867

line 1480why, one step back
1480 c.updateParentsPflags()

Fix incorrect merge of pflag.CommandLine to cmd.Flags()Albert Nigmatzianovbefore adoption7d22e77

line 1481why, one step back
1481 c.Flags().AddFlagSet(c.PersistentFlags())

Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867

line 1482why, one step back
1482 c.Flags().AddFlagSet(c.parentsPflags)

Add only missing persistent flags of parents in mergePersistentFlagsAlbert Nigmatzianovbefore adoption3d89ed4

lines 1483–1485why, one step back
1483}
14841485// updateParentsPflags updates c.parentsPflags by adding

Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867

line 1486why, one step back
1486// new persistent flags of all parents.

Add only missing persistent flags of parents in mergePersistentFlagsAlbert Nigmatzianovbefore adoption3d89ed4

line 1487why, one step back
1487// If c.parentsPflags == nil, it makes new.

Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867

line 1488why, one step back
1488func (c *Command) updateParentsPflags() {

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

lines 1489–1490why, one step back
1489 if c.parentsPflags == nil {
1490 c.parentsPflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)

Revert "Get rid of flagErrorBuf (#417)"Albert Nigmatzianovbefore adoption3d7bff8

line 1491why, one step back
1491 c.parentsPflags.SetOutput(c.flagErrorBuf)

Add only missing persistent flags of parents in mergePersistentFlagsAlbert Nigmatzianovbefore adoption3d89ed4

line 1492why, one step back
1492 c.parentsPflags.SortFlags = false

Improve consistency of flags when using SetGlobalNormalizationFunc (#522)Diego Becciolinibefore adoption0dacccf

lines 1493–1496why, one step back
1493 }
14941495 if c.globNormFunc != nil {1496 c.parentsPflags.SetNormalizeFunc(c.globNormFunc)

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 1497why, one step back
1497 }

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

line 1498why, one step back
1498

Add merging of CommandLine flags to Root commandAlbert Nigmatzianovbefore adoption458d797

lines 1499–1500why, one step back
1499 c.Root().PersistentFlags().AddFlagSet(flag.CommandLine)
1500

Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867

lines 1501–1502why, one step back
1501 c.VisitParents(func(parent *Command) {
1502 c.parentsPflags.AddFlagSet(parent.PersistentFlags())

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

line 1503why, one step back
1503 })

Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c

line 1504why, one step back
1504}