Skip to content
Myr

command.go

This file

Path

command.go

1,507 lines · 41.4 KiB · Go

Last changed by

Terminate the stripping of flags when -- is found (#649)Di Xubefore adoptionc439c4f

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

line 477why, one step back
477 switch {

Terminate the stripping of flags when -- is found (#649)Di Xubefore adoptionc439c4f

lines 478–480why, one step back
478 case s == "--":
479 // "--" terminates the flags480 break Loop

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

lines 481–492why, one step back
481 case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], flags):
482 // If '--flag arg' then483 // delete arg from args.484 fallthrough // (do the same as below)485 case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags):486 // If '-f arg' then487 // delete 'arg' from args or break the loop if len(args) <= 1.488 if len(args) <= 1 {489 break Loop490 } else {491 args = args[1:]492 continue

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

line 493why, one step back
493 }

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

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

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

lines 496–515why, one step back
496 }
497 }498499 return commands500}501502// argsMinusFirstX removes only the first x from args. Otherwise, commands that look like503// openshift admin policy add-role-to-user admin my-user, lose the admin argument (arg[4]).504func argsMinusFirstX(args []string, x string) []string {505 for i, y := range args {506 if x == y {507 ret := []string{}508 ret = append(ret, args[:i]...)509 ret = append(ret, args[i+1:]...)510 return ret511 }512 }513 return args514}515

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

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

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

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

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

lines 522–532why, one step back
522// Meant to be run on the highest node. Only searches down.
523func (c *Command) Find(args []string) (*Command, []string, error) {524 var innerfind func(*Command, []string) (*Command, []string)525526 innerfind = func(c *Command, innerArgs []string) (*Command, []string) {527 argsWOflags := stripFlags(innerArgs, c)528 if len(argsWOflags) == 0 {529 return c, innerArgs530 }531 nextSubCmd := argsWOflags[0]532

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

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

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

lines 536–540why, one step back
536 }
537 return c, innerArgs538 }539540 commandFound, a := innerfind(c, args)

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

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

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

lines 543–544why, one step back
543 }
544 return commandFound, a, nil

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

lines 545–546why, one step back
545}
546

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

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

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

lines 548–554why, one step back
548 if c.DisableSuggestions {
549 return ""550 }551 if c.SuggestionsMinimumDistance <= 0 {552 c.SuggestionsMinimumDistance = 2553 }554 suggestionsString := ""

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

line 555why, one step back
555 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 556–561why, one step back
556 suggestionsString += "\n\nDid you mean this?\n"
557 for _, s := range suggestions {558 suggestionsString += fmt.Sprintf("\t%v\n", s)559 }560 }561 return suggestionsString

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

lines 562–563why, one step back
562}
563

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

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

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

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

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

lines 569–578why, one step back
569 return cmd
570 }571 if EnablePrefixMatching && cmd.hasNameOrAliasPrefix(next) {572 matches = append(matches, cmd)573 }574 }575576 if len(matches) == 1 {577 return matches[0]578 }

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

line 579why, one step back
579

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

lines 580–625why, one step back
580 return nil
581}582583// Traverse the command tree to find the command, and parse args for584// each parent.585func (c *Command) Traverse(args []string) (*Command, []string, error) {586 flags := []string{}587 inFlag := false588589 for i, arg := range args {590 switch {591 // A long flag with a space separated value592 case strings.HasPrefix(arg, "--") && !strings.Contains(arg, "="):593 // TODO: this isn't quite right, we should really check ahead for 'true' or 'false'594 inFlag = !hasNoOptDefVal(arg[2:], c.Flags())595 flags = append(flags, arg)596 continue597 // A short flag with a space separated value598 case strings.HasPrefix(arg, "-") && !strings.Contains(arg, "=") && len(arg) == 2 && !shortHasNoOptDefVal(arg[1:], c.Flags()):599 inFlag = true600 flags = append(flags, arg)601 continue602 // The value for a flag603 case inFlag:604 inFlag = false605 flags = append(flags, arg)606 continue607 // A flag without a value, or with an `=` separated value608 case isFlagArg(arg):609 flags = append(flags, arg)610 continue611 }612613 cmd := c.findNext(arg)614 if cmd == nil {615 return c, args, nil616 }617618 if err := c.ParseFlags(flags); err != nil {619 return nil, args, err620 }621 return cmd.Traverse(args[i+1:])622 }623 return c, args, nil624}625

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

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

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

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

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

line 647why, one step back
647// 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 648why, one step back
648func (c *Command) VisitParents(fn func(*Command)) {

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

lines 649–651why, one step back
649 if c.HasParent() {
650 fn(c.Parent())651 c.Parent().VisitParents(fn)

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

lines 652–654why, one step back
652 }
653}654

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

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

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

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

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

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

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

line 659why, one step back
659 }

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

line 660why, one step back
660 return c

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

lines 661–662why, one step back
661}
662

Improve testsAlbert Nigmatzianovbefore adoption65c8acb

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

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

lines 665–677why, one step back
665func (c *Command) ArgsLenAtDash() int {
666 return c.Flags().ArgsLenAtDash()667}668669func (c *Command) execute(a []string) (err error) {670 if c == nil {671 return fmt.Errorf("Called Execute() on a nil Command")672 }673674 if len(c.Deprecated) > 0 {675 c.Printf("Command %q is deprecated, %s\n", c.Name(), c.Deprecated)676 }677

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

line 678why, one step back
678 // 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 679why, one step back
679 // overriding

Make initHelpFlag publicAlbert Nigmatzianovbefore adoptionfe69f2e

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

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

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

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

lines 682–684why, one step back
682
683 err = c.ParseFlags(a)684 if err != nil {

Merge pull request #283 from dnephin/add_flag_usage_funcEric Parisbefore adoptionb620a75

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

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

line 686why, one step back
686 }

Make initHelpFlag publicAlbert Nigmatzianovbefore adoptionfe69f2e

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

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

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

Make initHelpFlag publicAlbert Nigmatzianovbefore adoptionfe69f2e

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

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

lines 694–696why, one step back
694 c.Println("\"help\" flag declared as non-bool. Please correct your code")
695 return err696 }

Merge pull request #303 from fabianofranz/testing_help_exiting_1Eric Parisbefore adoptiona272c3c

line 697why, one step back
697

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

line 698why, one step back
698 if helpVal {

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

lines 699–701why, one step back
699 return flag.ErrHelp
700 }701

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

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

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

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

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

lines 720–721why, one step back
720 }
721

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

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

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

line 723why, one step back
723

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

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

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

lines 725–727why, one step back
725 if c.DisableFlagParsing {
726 argWoFlags = a727 }

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

line 728why, one step back
728

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

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

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

lines 733–751why, one step back
733 for p := c; p != nil; p = p.Parent() {
734 if p.PersistentPreRunE != nil {735 if err := p.PersistentPreRunE(c, argWoFlags); err != nil {736 return err737 }738 break739 } else if p.PersistentPreRun != nil {740 p.PersistentPreRun(c, argWoFlags)741 break742 }743 }744 if c.PreRunE != nil {745 if err := c.PreRunE(c, argWoFlags); err != nil {746 return err747 }748 } else if c.PreRun != nil {749 c.PreRun(c, argWoFlags)750 }751

enforce required flags (#502)Di Xubefore adoption4d6af28

lines 752–754why, one step back
752 if err := c.validateRequiredFlags(); err != nil {
753 return err754 }

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

lines 755–789why, one step back
755 if c.RunE != nil {
756 if err := c.RunE(c, argWoFlags); err != nil {757 return err758 }759 } else {760 c.Run(c, argWoFlags)761 }762 if c.PostRunE != nil {763 if err := c.PostRunE(c, argWoFlags); err != nil {764 return err765 }766 } else if c.PostRun != nil {767 c.PostRun(c, argWoFlags)768 }769 for p := c; p != nil; p = p.Parent() {770 if p.PersistentPostRunE != nil {771 if err := p.PersistentPostRunE(c, argWoFlags); err != nil {772 return err773 }774 break775 } else if p.PersistentPostRun != nil {776 p.PersistentPostRun(c, argWoFlags)777 break778 }779 }780781 return nil782}783784func (c *Command) preRun() {785 for _, x := range initializers {786 x()787 }788}789

Fix typosAlbert Nigmatzianovbefore adoptione606913

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

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

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

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

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

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

lines 799–811why, one step back
799func (c *Command) ExecuteC() (cmd *Command, err error) {
800 // Regardless of what command execute is called on, run on Root only801 if c.HasParent() {802 return c.Root().ExecuteC()803 }804805 // windows hook806 if preExecHookFn != nil {807 preExecHookFn(c)808 }809810 // initialize help as the last point possible to allow for user811 // overriding

Make initHelpCmd public (#436)Albert Nigmatzianovbefore adoption52ae6a1

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

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

lines 813–822why, one step back
813
814 var args []string815816 // Workaround FAIL with "go test -v" or "cobra.test -test.v", see #155817 if c.args == nil && filepath.Base(os.Args[0]) != "cobra.test" {818 args = os.Args[1:]819 } else {820 args = c.args821 }822

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

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

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

lines 829–839why, one step back
829 if err != nil {
830 // If found parse to a subcommand and then failed, talk about the subcommand831 if cmd != nil {832 c = cmd833 }834 if !c.SilenceErrors {835 c.Println("Error:", err.Error())836 c.Printf("Run '%v --help' for usage.\n", c.CommandPath())837 }838 return c, err839 }

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

line 840why, one step back
840

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

lines 841–845why, one step back
841 cmd.commandCalledAs.called = true
842 if cmd.commandCalledAs.name == "" {843 cmd.commandCalledAs.name = cmd.Name()844 }845

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

lines 846–866why, one step back
846 err = cmd.execute(flags)
847 if err != nil {848 // Always show help if requested, even if SilenceErrors is in849 // effect850 if err == flag.ErrHelp {851 cmd.HelpFunc()(cmd, args)852 return cmd, nil853 }854855 // If root command has SilentErrors flagged,856 // all subcommands should respect it857 if !cmd.SilenceErrors && !c.SilenceErrors {858 c.Println("Error:", err.Error())859 }860861 // If root command has SilentUsage flagged,862 // all subcommands should respect it863 if !cmd.SilenceUsage && !c.SilenceUsage {864 c.Println(cmd.UsageString())865 }866 }

Fix typosAlbert Nigmatzianovbefore adoptione606913

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

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

lines 868–874why, one step back
868}
869870func (c *Command) ValidateArgs(args []string) error {871 if c.Args == nil {872 return nil873 }874 return c.Args(c, args)

enforce required flags (#502)Di Xubefore adoption4d6af28

lines 875–890why, one step back
875}
876877func (c *Command) validateRequiredFlags() error {878 flags := c.Flags()879 missingFlagNames := []string{}880 flags.VisitAll(func(pflag *flag.Flag) {881 requiredAnnotation, found := pflag.Annotations[BashCompOneRequiredFlag]882 if !found {883 return884 }885 if (requiredAnnotation[0] == "true") && !pflag.Changed {886 missingFlagNames = append(missingFlagNames, pflag.Name)887 }888 })889890 if len(missingFlagNames) > 0 {

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

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

enforce required flags (#502)Di Xubefore adoption4d6af28

lines 892–893why, one step back
892 }
893 return nil

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

lines 894–895why, one step back
894}
895

Make initHelpFlag publicAlbert Nigmatzianovbefore adoptionfe69f2e

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

Add comment for InitDefaultHelpFlagAlbert Nigmatzianovbefore adoptiond83a1d7

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

Make initHelpFlag publicAlbert Nigmatzianovbefore adoptionfe69f2e

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

Merge pull request #286 from dnephin/merge_flags_before_adding_helpEric Parisbefore adoptionec2fe78

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

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

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

Initialize help flag in initHelpCmdAlbert Nigmatzianovbefore adoption867d414

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

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

lines 909–929why, one step back
909 }
910}911912// InitDefaultVersionFlag adds default version flag to c.913// It is called automatically by executing the c.914// If c already has a version flag, it will do nothing.915// If c.Version is empty, it will do nothing.916func (c *Command) InitDefaultVersionFlag() {917 if c.Version == "" {918 return919 }920921 c.mergePersistentFlags()922 if c.Flags().Lookup("version") == nil {923 usage := "version for "924 if c.Name() == "" {925 usage += "this command"926 } else {927 usage += c.Name()928 }929 c.Flags().Bool("version", false, usage)

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

lines 930–932why, one step back
930 }
931}932

Make initHelpCmd public (#436)Albert Nigmatzianovbefore adoption52ae6a1

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

Fix InitDefaultHelpCmd when custom help command is setAlbert Nigmatzianovbefore adoption8c6fa02

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

Make initHelpCmd public (#436)Albert Nigmatzianovbefore adoption52ae6a1

lines 938–939why, one step back
938 return
939 }

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

line 940why, one step back
940

Fix InitDefaultHelpCmd when custom help command is setAlbert Nigmatzianovbefore adoption8c6fa02

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

Delete tab in the long description of helpAlbert Nigmatzianovbefore adoptiond994347

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

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

line 947why, one step back
947

Fix InitDefaultHelpCmd when custom help command is setAlbert Nigmatzianovbefore adoption8c6fa02

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

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

line 959why, one step back
959 }

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

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

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

lines 961–963why, one step back
961 c.AddCommand(c.helpCommand)
962}963

Edit ResetFlags and ResetCommands descriptionsAlbert Nigmatzianovbefore adoptiond6a4305

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

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

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

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

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

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

lines 967–968why, one step back
967 c.commands = nil
968 c.helpCommand = nil

Add only missing persistent flags of parents in mergePersistentFlagsAlbert Nigmatzianovbefore adoption3d89ed4

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

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

lines 970–971why, one step back
970}
971

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

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

Merge pull request #293 from andreykurilin/sort_commandsEric Parisbefore adoptionbc81c21

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

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

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

Merge pull request #293 from andreykurilin/sort_commandsEric Parisbefore adoptionbc81c21

line 981why, one step back
981 // 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 982why, one step back
982 if EnableCommandSorting && !c.commandsAreSorted {

Merge pull request #293 from andreykurilin/sort_commandsEric Parisbefore adoptionbc81c21

lines 983–985why, one step back
983 sort.Sort(commandSorterByName(c.commands))
984 c.commandsAreSorted = true985 }

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

lines 986–1013why, one step back
986 return c.commands
987}988989// AddCommand adds one or more commands to this parent command.990func (c *Command) AddCommand(cmds ...*Command) {991 for i, x := range cmds {992 if cmds[i] == c {993 panic("Command can't be a child of itself")994 }995 cmds[i].parent = c996 // update max lengths997 usageLen := len(x.Use)998 if usageLen > c.commandsMaxUseLen {999 c.commandsMaxUseLen = usageLen1000 }1001 commandPathLen := len(x.CommandPath())1002 if commandPathLen > c.commandsMaxCommandPathLen {1003 c.commandsMaxCommandPathLen = commandPathLen1004 }1005 nameLen := len(x.Name())1006 if nameLen > c.commandsMaxNameLen {1007 c.commandsMaxNameLen = nameLen1008 }1009 // If global normalization function exists, update all children1010 if c.globNormFunc != nil {1011 x.SetGlobalNormalizationFunc(c.globNormFunc)1012 }1013 c.commands = append(c.commands, x)

Merge pull request #293 from andreykurilin/sort_commandsEric Parisbefore adoptionbc81c21

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

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

lines 1015–1017why, one step back
1015 }
1016}1017

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

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

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

lines 1019–1051why, one step back
1019func (c *Command) RemoveCommand(cmds ...*Command) {
1020 commands := []*Command{}1021main:1022 for _, command := range c.commands {1023 for _, cmd := range cmds {1024 if command == cmd {1025 command.parent = nil1026 continue main1027 }1028 }1029 commands = append(commands, command)1030 }1031 c.commands = commands1032 // recompute all lengths1033 c.commandsMaxUseLen = 01034 c.commandsMaxCommandPathLen = 01035 c.commandsMaxNameLen = 01036 for _, command := range c.commands {1037 usageLen := len(command.Use)1038 if usageLen > c.commandsMaxUseLen {1039 c.commandsMaxUseLen = usageLen1040 }1041 commandPathLen := len(command.CommandPath())1042 if commandPathLen > c.commandsMaxCommandPathLen {1043 c.commandsMaxCommandPathLen = commandPathLen1044 }1045 nameLen := len(command.Name())1046 if nameLen > c.commandsMaxNameLen {1047 c.commandsMaxNameLen = nameLen1048 }1049 }1050}1051

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1052why, one step back
1052// 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 1053why, one step back
1053func (c *Command) Print(i ...interface{}) {

Merge pull request #305 from fabianofranz/expose_outEric Parisbefore adoptionb24564e

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

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

lines 1055–1056why, one step back
1055}
1056

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1057why, one step back
1057// 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 1058why, one step back
1058func (c *Command) Println(i ...interface{}) {

Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af

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

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

lines 1060–1061why, one step back
1060}
1061

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1062why, one step back
1062// 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 1063why, one step back
1063func (c *Command) Printf(format string, i ...interface{}) {

Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af

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

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

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

Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af

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

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

line 1071why, one step back
1071 }

Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af

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

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

lines 1073–1074why, one step back
1073}
1074

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

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

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

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

Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af

line 1077why, one step back
1077 var useline string

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

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

Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af

lines 1079–1081why, one step back
1079 useline = c.parent.CommandPath() + " " + c.Use
1080 } else {1081 useline = c.Use

Disable flags in UseLine (#552)Daniel Nephinbefore adoption34ceca5

lines 1082–1084why, one step back
1082 }
1083 if c.DisableFlagsInUseLine {1084 return useline

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

line 1085why, one step back
1085 }

Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af

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

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

lines 1090–1091why, one step back
1090}
1091

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

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

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

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

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

lines 1094–1103why, one step back
1094func (c *Command) DebugFlags() {
1095 c.Println("DebugFlags called on", c.Name())1096 var debugflags func(*Command)10971098 debugflags = func(x *Command) {1099 if x.HasFlags() || x.HasPersistentFlags() {1100 c.Println(x.Name())1101 }1102 if x.HasFlags() {1103 x.flags.VisitAll(func(f *flag.Flag) {

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

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

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

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

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

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

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

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

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

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

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

line 1139why, one step back
1139 }

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

line 1140why, one step back
1140 return name

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

lines 1141–1142why, one step back
1141}
1142

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

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

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

lines 1144–1152why, one step back
1144func (c *Command) HasAlias(s string) bool {
1145 for _, a := range c.Aliases {1146 if a == s {1147 return true1148 }1149 }1150 return false1151}1152

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

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

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

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

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

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

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

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

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

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

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

lines 1172–1178why, one step back
1172 return true
1173 }1174 }1175 return false1176}11771178// NameAndAliases returns a list of the command name and all aliases

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

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

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

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

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

lines 1184–1187why, one step back
1184func (c *Command) HasExample() bool {
1185 return len(c.Example) > 01186}1187

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

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

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

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

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

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

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

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

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

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

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

lines 1200–1215why, one step back
1200func (c *Command) IsAvailableCommand() bool {
1201 if len(c.Deprecated) != 0 || c.Hidden {1202 return false1203 }12041205 if c.HasParent() && c.Parent().helpCommand == c {1206 return false1207 }12081209 if c.Runnable() || c.HasAvailableSubCommands() {1210 return true1211 }12121213 return false1214}1215

Rename IsHelpCommand to IsAdditionalHelpTopicCommand (#398)Albert Nigmatzianovbefore adoption0960ff7

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

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

lines 1222–1228why, one step back
1222 // if a command is runnable, deprecated, or hidden it is not a 'help' command
1223 if c.Runnable() || len(c.Deprecated) != 0 || c.Hidden {1224 return false1225 }12261227 // if any non-help sub commands are found, the command is not a 'help' command1228 for _, sub := range c.commands {

Rename IsHelpCommand to IsAdditionalHelpTopicCommand (#398)Albert Nigmatzianovbefore adoption0960ff7

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

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

lines 1230–1237why, one step back
1230 return false
1231 }1232 }12331234 // the command either has no sub commands, or no non-help sub commands1235 return true1236}1237

Merge pull request #327 from bogem/fixesEric Parisbefore adoption1f47171

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

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

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

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

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

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

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

Rename IsHelpCommand to IsAdditionalHelpTopicCommand (#398)Albert Nigmatzianovbefore adoption0960ff7

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

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

lines 1245–1253why, one step back
1245 return true
1246 }1247 }12481249 // the command either has no sub commands, or no available 'help' sub commands1250 return false1251}12521253// HasAvailableSubCommands determines if a command has available sub commands that

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1254why, one step back
1254// 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 1255–1263why, one step back
1255func (c *Command) HasAvailableSubCommands() bool {
1256 // return true on the first found available (non deprecated/help/hidden)1257 // sub command1258 for _, sub := range c.commands {1259 if sub.IsAvailableCommand() {1260 return true1261 }1262 }1263

Fix typo (#573)Pavol Loffaybefore adoptionf634327

line 1264why, one step back
1264 // 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 1265–1268why, one step back
1265 // sub commands
1266 return false1267}1268

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

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

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

lines 1270–1273why, one step back
1270func (c *Command) HasParent() bool {
1271 return c.parent != nil1272}1273

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

line 1274why, one step back
1274// 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 1275–1278why, one step back
1275func (c *Command) GlobalNormalizationFunc() func(f *flag.FlagSet, name string) flag.NormalizedName {
1276 return c.globNormFunc1277}1278

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

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

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

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

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

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

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

lines 1284–1287why, one step back
1284 if c.flagErrorBuf == nil {
1285 c.flagErrorBuf = new(bytes.Buffer)1286 }1287 c.flags.SetOutput(c.flagErrorBuf)

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

line 1288why, one step back
1288 }

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

line 1289why, one step back
1289

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

lines 1290–1292why, one step back
1290 return c.flags
1291}1292

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

line 1293why, one step back
1293// 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 1294–1305why, one step back
1294func (c *Command) LocalNonPersistentFlags() *flag.FlagSet {
1295 persistentFlags := c.PersistentFlags()12961297 out := flag.NewFlagSet(c.Name(), flag.ContinueOnError)1298 c.LocalFlags().VisitAll(func(f *flag.Flag) {1299 if persistentFlags.Lookup(f.Name) == nil {1300 out.AddFlag(f)1301 }1302 })1303 return out1304}1305

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

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

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

lines 1307–1309why, one step back
1307func (c *Command) LocalFlags() *flag.FlagSet {
1308 c.mergePersistentFlags()1309

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

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

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

lines 1312–1315why, one step back
1312 if c.flagErrorBuf == nil {
1313 c.flagErrorBuf = new(bytes.Buffer)1314 }1315 c.lflags.SetOutput(c.flagErrorBuf)

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

line 1316why, one step back
1316 }

Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867

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

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

lines 1318–1320why, one step back
1318 if c.globNormFunc != nil {
1319 c.lflags.SetNormalizeFunc(c.globNormFunc)1320 }

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

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

Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867

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

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

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

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

lines 1330–1331why, one step back
1330}
1331

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

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

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

lines 1333–1335why, one step back
1333func (c *Command) InheritedFlags() *flag.FlagSet {
1334 c.mergePersistentFlags()1335

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

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

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

lines 1338–1341why, one step back
1338 if c.flagErrorBuf == nil {
1339 c.flagErrorBuf = new(bytes.Buffer)1340 }1341 c.iflags.SetOutput(c.flagErrorBuf)

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

lines 1342–1343why, one step back
1342 }
1343

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

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

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

lines 1345–1348why, one step back
1345 if c.globNormFunc != nil {
1346 c.iflags.SetNormalizeFunc(c.globNormFunc)1347 }1348

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

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

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

line 1352why, one step back
1352 }

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

lines 1353–1354why, one step back
1353 })
1354 return c.iflags

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

lines 1355–1356why, one step back
1355}
1356

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

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

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

lines 1358–1361why, one step back
1358func (c *Command) NonInheritedFlags() *flag.FlagSet {
1359 return c.LocalFlags()1360}1361

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

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

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

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

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

lines 1366–1369why, one step back
1366 if c.flagErrorBuf == nil {
1367 c.flagErrorBuf = new(bytes.Buffer)1368 }1369 c.pflags.SetOutput(c.flagErrorBuf)

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

lines 1370–1373why, one step back
1370 }
1371 return c.pflags1372}1373

Edit ResetFlags and ResetCommands descriptionsAlbert Nigmatzianovbefore adoptiond6a4305

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

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

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

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

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

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

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

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

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

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

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

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

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

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

lines 1382–1385why, one step back
1382
1383 c.lflags = nil1384 c.iflags = nil1385 c.parentsPflags = nil

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

lines 1386–1387why, one step back
1386}
1387

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

line 1388why, one step back
1388// 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 1389–1392why, one step back
1389func (c *Command) HasFlags() bool {
1390 return c.Flags().HasFlags()1391}1392

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

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

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

lines 1394–1397why, one step back
1394func (c *Command) HasPersistentFlags() bool {
1395 return c.PersistentFlags().HasFlags()1396}1397

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

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

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

lines 1399–1402why, one step back
1399func (c *Command) HasLocalFlags() bool {
1400 return c.LocalFlags().HasFlags()1401}1402

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

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

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

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

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

lines 1406–1407why, one step back
1406}
1407

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

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

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

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

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

lines 1410–1413why, one step back
1410func (c *Command) HasAvailableFlags() bool {
1411 return c.Flags().HasAvailableFlags()1412}1413

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

line 1414why, one step back
1414// 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 1415–1418why, one step back
1415func (c *Command) HasAvailablePersistentFlags() bool {
1416 return c.PersistentFlags().HasAvailableFlags()1417}1418

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

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

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

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

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

lines 1421–1424why, one step back
1421func (c *Command) HasAvailableLocalFlags() bool {
1422 return c.LocalFlags().HasAvailableFlags()1423}1424

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

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

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

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

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

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

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

lines 1429–1430why, one step back
1429}
1430

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

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

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

lines 1432–1441why, one step back
1432func (c *Command) Flag(name string) (flag *flag.Flag) {
1433 flag = c.Flags().Lookup(name)14341435 if flag == nil {1436 flag = c.persistentFlag(name)1437 }14381439 return1440}1441

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

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

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

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

Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867

lines 1448–1450why, one step back
1448 if flag == nil {
1449 c.updateParentsPflags()1450 flag = c.parentsPflags.Lookup(name)

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

lines 1451–1454why, one step back
1451 }
1452 return1453}1454

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

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

Show messages if deprecated flags are usedAlbert Nigmatzianovbefore adoption99b5d83

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

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

lines 1457–1459why, one step back
1457 if c.DisableFlagParsing {
1458 return nil1459 }

Show messages if deprecated flags are usedAlbert Nigmatzianovbefore adoption99b5d83

line 1460why, one step back
1460

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

lines 1461–1463why, one step back
1461 if c.flagErrorBuf == nil {
1462 c.flagErrorBuf = new(bytes.Buffer)1463 }

Show messages if deprecated flags are usedAlbert Nigmatzianovbefore adoption99b5d83

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

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

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

Show messages if deprecated flags are usedAlbert Nigmatzianovbefore adoption99b5d83

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

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

lines 1473–1474why, one step back
1473}
1474

Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b

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

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

lines 1476–1479why, one step back
1476func (c *Command) Parent() *Command {
1477 return c.parent1478}1479

Add only missing persistent flags of parents in mergePersistentFlagsAlbert Nigmatzianovbefore adoption3d89ed4

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

Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867

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

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

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

Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867

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

Add only missing persistent flags of parents in mergePersistentFlagsAlbert Nigmatzianovbefore adoption3d89ed4

lines 1486–1488why, one step back
1486}
14871488// updateParentsPflags updates c.parentsPflags by adding

Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867

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

Add only missing persistent flags of parents in mergePersistentFlagsAlbert Nigmatzianovbefore adoption3d89ed4

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

Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867

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

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

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

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

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

Add only missing persistent flags of parents in mergePersistentFlagsAlbert Nigmatzianovbefore adoption3d89ed4

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

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

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

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

line 1500why, one step back
1500 }

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

line 1501why, one step back
1501

Add merging of CommandLine flags to Root commandAlbert Nigmatzianovbefore adoption458d797

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

Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867

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

Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59

line 1506why, one step back
1506 })

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

line 1507why, one step back
1507}