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 fileTake 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 back1// 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.13Use pflag.ShorthandLookupAlbert Nigmatzianovbefore adoption4061f41
lines 14–15why, one step back14// 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 back16package cobra1718import (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 back24 "sort"Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 25–30why, one step back25 "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 back31// E.g. 'go run ...' - 'run' is the command. Cobra requiresAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 32–34why, one step back32// you to define the usage and description as part of your command33// definition to ensure usability.34type Command struct {Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
line 35why, one step back35 // Use is the one-line usage message.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 36why, one step back36 Use stringDivide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483
line 37why, one step back37Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
line 38why, one step back38 // 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 back39 Aliases []stringDivide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483
line 40why, one step back40Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
lines 41–42why, one step back41 // 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 back43 SuggestFor []stringDivide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483
line 44why, one step back44Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
line 45why, one step back45 // 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 back46 Short stringDivide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483
line 47why, one step back47Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
line 48why, one step back48 // 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 back49 Long stringDivide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483
line 50why, one step back50Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
line 51why, one step back51 // Example is examples of how to use the command.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 52why, one step back52 Example stringDivide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483
line 53why, one step back53Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
line 54why, one step back54 // ValidArgs is list of all valid non-flag arguments that are accepted in bash completionsAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 55why, one step back55 ValidArgs []stringDivide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483
line 56why, one step back56Update documentationAlbert Nigmatzianovbefore adoption1723331
lines 57–59why, one step back57 // Expected arguments58 Args PositionalArgs59Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
lines 60–62why, one step back60 // 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 back63 ArgAliases []stringDivide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483
line 64why, one step back64Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
line 65why, one step back65 // 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 back66 BashCompletionFunction stringDivide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483
line 67why, one step back67Group fields of CommandAlbert Nigmatzianovbefore adoption90d2fd8
line 68why, one step back68 // 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 back69 Deprecated stringDivide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483
line 70why, one step back70Group fields of CommandAlbert Nigmatzianovbefore adoption90d2fd8
line 71why, one step back71 // 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 back72 Hidden boolDivide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483
line 73why, one step back73Add a tags field for user defined tags on a Command. (#359)Daniel Nephinbefore adoption99600f6
line 74why, one step back74 // Annotations are key/value pairs that can be used by applications to identify orFix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
line 75why, one step back75 // group commands.Add a tags field for user defined tags on a Command. (#359)Daniel Nephinbefore adoption99600f6
line 76why, one step back76 Annotations map[string]stringDivide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483
line 77why, one step back77Add support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce
lines 78–82why, one step back78 // Version defines the version for this command. If this value is non-empty and the command does not79 // 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 string82Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 83–88why, one step back83 // 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 back89 // All functions get the same args, the arguments after the command name.Divide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483
line 90why, one step back90 //Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
line 91why, one step back91 // PersistentPreRun: children of this command will inherit and execute.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 92why, one step back92 PersistentPreRun func(cmd *Command, args []string)Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
line 93why, one step back93 // PersistentPreRunE: PersistentPreRun but returns an error.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 94–96why, one step back94 PersistentPreRunE func(cmd *Command, args []string) error95 // 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 back97 // PreRunE: PreRun but returns an error.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 98why, one step back98 PreRunE func(cmd *Command, args []string) errorFix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
line 99why, one step back99 // 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 back100 Run func(cmd *Command, args []string)Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
line 101why, one step back101 // RunE: Run but returns an error.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 102–104why, one step back102 RunE func(cmd *Command, args []string) error103 // PostRun: run after the Run command.104 PostRun func(cmd *Command, args []string)Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
line 105why, one step back105 // PostRunE: PostRun but returns an error.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 106why, one step back106 PostRunE func(cmd *Command, args []string) errorFix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
line 107why, one step back107 // 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 back108 PersistentPostRun func(cmd *Command, args []string)Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
line 109why, one step back109 // PersistentPostRunE: PersistentPostRun but returns an error.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 110why, one step back110 PersistentPostRunE func(cmd *Command, args []string) errorDivide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483
lines 111–117why, one step back111112 // 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 bool117Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
lines 118–120why, one step back118 // DisableFlagParsing disables the flag parsing.119 // If this is true all flags will be passed to the command as arguments.120 DisableFlagParsing boolDivide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483
line 121why, one step back121Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
lines 122–123why, one step back122 // 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 back124 DisableAutoGenTag boolFix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
line 125why, one step back125Disable flags in UseLine (#552)Daniel Nephinbefore adoption34ceca5
lines 126–129why, one step back126 // DisableFlagsInUseLine will disable the addition of [flags] to the usage127 // line of a command when printing help or generating docs128 DisableFlagsInUseLine bool129Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
lines 130–136why, one step back130 // DisableSuggestions disables the suggestions based on Levenshtein distance131 // that go along with 'unknown command' messages.132 DisableSuggestions bool133 // SuggestionsMinimumDistance defines minimum levenshtein distance to display suggestions.134 // Must be > 0.135 SuggestionsMinimumDistance int136Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03
lines 137–139why, one step back137 // TraverseChildren parses flags on all parents before executing child command.138 TraverseChildren bool139Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
line 140why, one step back140 // 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 back141 commands []*CommandFix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
line 142why, one step back142 // parent is a parent command for this command.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 143why, one step back143 parent *CommandDivide fields of Command for readabilityAlbert Nigmatzianovbefore adoptioneceb483
line 144why, one step back144 // 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 back145 commandsMaxUseLen int146 commandsMaxCommandPathLen int147 commandsMaxNameLen intGroup fields of CommandAlbert Nigmatzianovbefore adoption90d2fd8
line 148why, one step back148 // commandsAreSorted defines, if command slice are sorted or not.Merge pull request #293 from andreykurilin/sort_commandsEric Parisbefore adoptionbc81c21
line 149why, one step back149 commandsAreSorted boolAdd `CalledAs` method to cobra.Command (w/ tests) (#567)Tim Peoplesbefore adoptioneb58983
lines 150–154why, one step back150 // 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 back155Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
lines 156–157why, one step back156 // args is actual args parsed from flags.157 args []stringGroup fields of CommandAlbert Nigmatzianovbefore adoption90d2fd8
lines 158–173why, one step back158 // 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.NormalizedName173Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
lines 174–181why, one step back174 // 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 back182 flagErrorFunc func(*Command, error) errorFix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
lines 183–186why, one step back183 // 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 back187 // helpCommand is command with usage 'help'. If it's not defined by user,Fix docs for CommandAlbert Nigmatzianovbefore adoption4db577d
lines 188–189why, one step back188 // cobra uses default help command.189 helpCommand *CommandAdd support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce
lines 190–191why, one step back190 // versionTemplate is the version template defined by user.191 versionTemplate stringAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 192–193why, one step back192}193Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 194why, one step back194// SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overriddenAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 195–202why, one step back195// 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 back203 c.output = outputAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 204–205why, one step back204}205Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 206why, one step back206// 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 back207func (c *Command) SetUsageFunc(f func(*Command) error) {208 c.usageFunc = f209}210Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 211why, one step back211// 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 back212func (c *Command) SetUsageTemplate(s string) {213 c.usageTemplate = s214}215Merge pull request #283 from dnephin/add_flag_usage_funcEric Parisbefore adoptionb620a75
line 216why, one step back216// SetFlagErrorFunc sets a function to generate an error when flag parsingSome fixes related to help command (#394)Albert Nigmatzianovbefore adoptionfcd0c5a
line 217why, one step back217// fails.Merge pull request #283 from dnephin/add_flag_usage_funcEric Parisbefore adoptionb620a75
lines 218–221why, one step back218func (c *Command) SetFlagErrorFunc(f func(*Command, error) error) {219 c.flagErrorFunc = f220}221Some fixes related to help command (#394)Albert Nigmatzianovbefore adoptionfcd0c5a
line 222why, one step back222// 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 back223func (c *Command) SetHelpFunc(f func(*Command, []string)) {224 c.helpFunc = f225}226Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 227why, one step back227// SetHelpCommand sets help command.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 228–231why, one step back228func (c *Command) SetHelpCommand(cmd *Command) {229 c.helpCommand = cmd230}231Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 232why, one step back232// 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 back233func (c *Command) SetHelpTemplate(s string) {234 c.helpTemplate = s235}236Add support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce
lines 237–241why, one step back237// 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}241Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 242–253why, one step back242// 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}253Some fixes related to help command (#394)Albert Nigmatzianovbefore adoptionfcd0c5a
line 254why, one step back254// OutOrStdout returns output to stdout.Merge pull request #305 from fabianofranz/expose_outEric Parisbefore adoptionb24564e
line 255why, one step back255func (c *Command) OutOrStdout() io.Writer {Merge pull request #303 from fabianofranz/testing_help_exiting_1Eric Parisbefore adoptiona272c3c
lines 256–258why, one step back256 return c.getOut(os.Stdout)257}258Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 259why, one step back259// OutOrStderr returns output to stderrMerge pull request #305 from fabianofranz/expose_outEric Parisbefore adoptionb24564e
line 260why, one step back260func (c *Command) OutOrStderr() io.Writer {Merge pull request #303 from fabianofranz/testing_help_exiting_1Eric Parisbefore adoptiona272c3c
lines 261–265why, one step back261 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 back266 return c.outputMerge pull request #303 from fabianofranz/testing_help_exiting_1Eric Parisbefore adoptiona272c3c
lines 267–274why, one step back267 }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 commandMerge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 275why, one step back275// 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 back276func (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 back281 return c.Parent().UsageFunc()Merge pull request #260 from ianwalter/code-cleanupEric Parisbefore adoptionb0d571e
lines 282–283why, one step back282 }283 return func(c *Command) error {Merge pull request #305 from fabianofranz/expose_outEric Parisbefore adoptionb24564e
lines 284–285why, one step back284 c.mergePersistentFlags()285 err := tmpl(c.OutOrStderr(), c.UsageTemplate(), c)Merge pull request #260 from ianwalter/code-cleanupEric Parisbefore adoptionb0d571e
line 286why, one step back286 if err != nil {Merge pull request #303 from fabianofranz/testing_help_exiting_1Eric Parisbefore adoptiona272c3c
line 287why, one step back287 c.Println(err)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 288why, one step back288 }Merge pull request #260 from ianwalter/code-cleanupEric Parisbefore adoptionb0d571e
line 289why, one step back289 return errAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 290–292why, one step back290 }291}292Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
lines 293–295why, one step back293// 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 back296func (c *Command) Usage() error {297 return c.UsageFunc()(c)298}299Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 300why, one step back300// HelpFunc returns either the function set by SetHelpFunc for this commandMerge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 301why, one step back301// 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 back302func (c *Command) HelpFunc() func(*Command, []string) {Delete checkHelpFuncAlbert Nigmatzianovbefore adoption681a777
lines 303–304why, one step back303 if c.helpFunc != nil {304 return c.helpFuncAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 305why, one step back305 }Delete checkHelpFuncAlbert Nigmatzianovbefore adoption681a777
lines 306–309why, one step back306 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 back310 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 back312 if err != nil {313 c.Println(err)314 }315 }316}317Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
lines 318–320why, one step back318// 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 back321func (c *Command) Help() error {322 c.HelpFunc()(c, []string{})323 return nil324}325Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 326why, one step back326// UsageString return usage string.Merge pull request #303 from fabianofranz/testing_help_exiting_1Eric Parisbefore adoptiona272c3c
lines 327–330why, one step back327func (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 back331 c.Usage()Merge pull request #303 from fabianofranz/testing_help_exiting_1Eric Parisbefore adoptiona272c3c
lines 332–335why, one step back332 c.output = tmpOutput333 return bb.String()334}335Merge pull request #283 from dnephin/add_flag_usage_funcEric Parisbefore adoptionb620a75
lines 336–351why, one step back336// FlagErrorFunc returns either the function set by SetFlagErrorFunc for this337// 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}351Merge pull request #260 from ianwalter/code-cleanupEric Parisbefore adoptionb0d571e
line 352why, one step back352var minUsagePadding = 25Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 353why, one step back353Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 354why, one step back354// UsagePadding return padding for the usage.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 355–358why, one step back355func (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 back359 return c.parent.commandsMaxUseLenAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 360–361why, one step back360}361Merge pull request #260 from ianwalter/code-cleanupEric Parisbefore adoptionb0d571e
line 362why, one step back362var minCommandPathPadding = 11Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 363why, one step back363Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 364why, one step back364// CommandPathPadding return padding for the command path.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 365–368why, one step back365func (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 back369 return c.parent.commandsMaxCommandPathLenAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 370–371why, one step back370}371Merge pull request #260 from ianwalter/code-cleanupEric Parisbefore adoptionb0d571e
line 372why, one step back372var minNamePadding = 11Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 373why, one step back373Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 374why, one step back374// NamePadding returns padding for the name.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 375–378why, one step back375func (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 back379 return c.parent.commandsMaxNameLenAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 380–381why, one step back380}381Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 382why, one step back382// UsageTemplate returns usage template for the command.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 383–389why, one step back383func (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 back390 }391 return `Usage:{{if .Runnable}}Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af
lines 392–393why, one step back392 {{.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 back394395Aliases:Keep only one newline between Aliases and Flags in help (#410)Albert Nigmatzianovbefore adoption9350054
line 396why, one step back396 {{.NameAndAliases}}{{end}}{{if .HasExample}}Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 397–398why, one step back397398Examples:Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af
line 399why, one step back399{{.Example}}{{end}}{{if .HasAvailableSubCommands}}Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 400why, one step back400Some fixes related to help command (#394)Albert Nigmatzianovbefore adoptionfcd0c5a
lines 401–402why, one step back401Available 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 back403404Flags:Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af
line 405why, one step back405{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 406–407why, one step back406407Global Flags:Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af
line 408why, one step back408{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 409why, one step back409Rename IsHelpCommand to IsAdditionalHelpTopicCommand (#398)Albert Nigmatzianovbefore adoption0960ff7
line 410why, one step back410Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}Some fixes related to help command (#394)Albert Nigmatzianovbefore adoptionfcd0c5a
line 411why, one step back411 {{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 back412413Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}414`415}416Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 417why, one step back417// HelpTemplate return help template for the command.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 418–424why, one step back418func (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 back425 }Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af
line 426why, one step back426 return `{{with (or .Long .Short)}}{{. | trimTrailingWhitespaces}}Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 427–430why, one step back427428{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`429}430Add support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce
lines 431–443why, one step back431// 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}443Use pflag.ShorthandLookupAlbert Nigmatzianovbefore adoption4061f41
lines 444–445why, one step back444func 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 back446 if flag == nil {447 return false448 }Simplify stripFlagsAlbert Nigmatzianovbefore adoptionf4f10f6
line 449why, one step back449 return flag.NoOptDefVal != ""Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 450–451why, one step back450}451Fix subcommand locations with NoOptDefValue flags (#391)Eric Parisbefore adoption92ea23a
line 452why, one step back452func shortHasNoOptDefVal(name string, fs *flag.FlagSet) bool {Use pflag.ShorthandLookupAlbert Nigmatzianovbefore adoption4061f41
lines 453–461why, one step back453 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 back462}463464func stripFlags(args []string, c *Command) []string {Simplify stripFlags functionAlbert Nigmatzianovbefore adoption9890b7b
line 465why, one step back465 if len(args) == 0 {Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 466–470why, one step back466 return args467 }468 c.mergePersistentFlags()469470 commands := []string{}Simplify stripFlagsAlbert Nigmatzianovbefore adoptionf4f10f6
line 471why, one step back471 flags := c.Flags()Simplify stripFlags functionAlbert Nigmatzianovbefore adoption9890b7b
lines 472–475why, one step back472473Loop:474 for len(args) > 0 {475 s := args[0]Simplify stripFlagsAlbert Nigmatzianovbefore adoptionf4f10f6
line 476why, one step back476 args = args[1:]Fix tests so they give correct args (#445)Albert Nigmatzianovbefore adoption51b7cf5
line 477why, one step back477 switch {Terminate the stripping of flags when -- is found (#649)Di Xubefore adoptionc439c4f
lines 478–480why, one step back478 case s == "--":479 // "--" terminates the flags480 break LoopFix tests so they give correct args (#445)Albert Nigmatzianovbefore adoption51b7cf5
lines 481–492why, one step back481 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 continueAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 493why, one step back493 }Fix tests so they give correct args (#445)Albert Nigmatzianovbefore adoption51b7cf5
lines 494–495why, one step back494 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 back496 }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}515Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03
lines 516–520why, one step back516func isFlagArg(arg string) bool {517 return ((len(arg) >= 3 && arg[1] == '-') ||518 (len(arg) >= 2 && arg[0] == '-' && arg[1] != '-'))519}520Add a tags field for user defined tags on a Command. (#359)Daniel Nephinbefore adoption99600f6
line 521why, one step back521// Find the target command given the args and command treeAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 522–532why, one step back522// 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]532Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03
lines 533–535why, one step back533 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 back536 }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 back541 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 back543 }544 return commandFound, a, nilAllow commands to explicitly state if they do, or do not take arbitrary argumentsEric Parisbefore adoptiond89c499
lines 545–546why, one step back545}546Refactor TakesArgs to use an interface for arg validation.Daniel Nephinbefore adoptionf20b4e9
line 547why, one step back547func (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 back548 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 back555 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 back556 suggestionsString += "\n\nDid you mean this?\n"557 for _, s := range suggestions {558 suggestionsString += fmt.Sprintf("\t%v\n", s)559 }560 }561 return suggestionsStringAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 562–563why, one step back562}563Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03
lines 564–567why, one step back564func (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 back568 cmd.commandCalledAs.name = nextAdd a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03
lines 569–578why, one step back569 return cmd570 }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 back579Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03
lines 580–625why, one step back580 return nil581}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}625Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 626why, one step back626// SuggestionsFor provides suggestions for the typedName.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 627–646why, one step back627func (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}646Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 647why, one step back647// 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 back648func (c *Command) VisitParents(fn func(*Command)) {Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
lines 649–651why, one step back649 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 back652 }653}654Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 655why, one step back655// Root finds root command.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 656why, one step back656func (c *Command) Root() *Command {Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
lines 657–658why, one step back657 if c.HasParent() {658 return c.Parent().Root()Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 659why, one step back659 }Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
line 660why, one step back660 return cAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 661–662why, one step back661}662Improve testsAlbert Nigmatzianovbefore adoption65c8acb
lines 663–664why, one step back663// ArgsLenAtDash will return the length of c.Flags().Args at the moment664// when a -- was found during args parsing.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 665–677why, one step back665func (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 }677Add support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce
line 678why, one step back678 // initialize help and version flag at the last point possible to allow for userAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 679why, one step back679 // overridingMake initHelpFlag publicAlbert Nigmatzianovbefore adoptionfe69f2e
line 680why, one step back680 c.InitDefaultHelpFlag()Add support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce
line 681why, one step back681 c.InitDefaultVersionFlag()Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 682–684why, one step back682683 err = c.ParseFlags(a)684 if err != nil {Merge pull request #283 from dnephin/add_flag_usage_funcEric Parisbefore adoptionb620a75
line 685why, one step back685 return c.FlagErrorFunc()(c, err)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 686why, one step back686 }Make initHelpFlag publicAlbert Nigmatzianovbefore adoptionfe69f2e
lines 687–688why, one step back687688 // 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 back689 // 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 helpMake initHelpFlag publicAlbert Nigmatzianovbefore adoptionfe69f2e
line 693why, one step back693 // flag in InitDefaultHelpFlag()Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 694–696why, one step back694 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 back697Ensure that '--version' flag works properly for root command (#595)Nick Miyakebefore adoptionccaecb1
line 698why, one step back698 if helpVal {Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 699–701why, one step back699 return flag.ErrHelp700 }701Add support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce
lines 702–715why, one step back702 // for back-compat, only add version flag behavior if version is defined703 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 back716 }717718 if !c.Runnable() {719 return flag.ErrHelpAdd support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce
lines 720–721why, one step back720 }721Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 722why, one step back722 c.preRun()Merge pull request #289 from markbates/disable-flagsEric Parisbefore adoptionf447048
line 723why, one step back723Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 724why, one step back724 argWoFlags := c.Flags().Args()Merge pull request #289 from markbates/disable-flagsEric Parisbefore adoptionf447048
lines 725–727why, one step back725 if c.DisableFlagParsing {726 argWoFlags = a727 }Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 728why, one step back728Refactor TakesArgs to use an interface for arg validation.Daniel Nephinbefore adoptionf20b4e9
lines 729–732why, one step back729 if err := c.ValidateArgs(argWoFlags); err != nil {730 return err731 }732Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 733–751why, one step back733 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 }751enforce required flags (#502)Di Xubefore adoption4d6af28
lines 752–754why, one step back752 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 back755 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}789Fix typosAlbert Nigmatzianovbefore adoptione606913
line 790why, one step back790// 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 back791// and run through the command tree finding appropriate matches792// for commands and then corresponding flags.793func (c *Command) Execute() error {794 _, err := c.ExecuteC()795 return err796}797Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 798why, one step back798// ExecuteC executes the command.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 799–811why, one step back799func (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 // overridingMake initHelpCmd public (#436)Albert Nigmatzianovbefore adoption52ae6a1
line 812why, one step back812 c.InitDefaultHelpCmd()Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 813–822why, one step back813814 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 }822Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03
lines 823–828why, one step back823 var flags []string824 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 back829 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 back840Add `CalledAs` method to cobra.Command (w/ tests) (#567)Tim Peoplesbefore adoptioneb58983
lines 841–845why, one step back841 cmd.commandCalledAs.called = true842 if cmd.commandCalledAs.name == "" {843 cmd.commandCalledAs.name = cmd.Name()844 }845Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 846–866why, one step back846 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 back867 return cmd, errRefactor TakesArgs to use an interface for arg validation.Daniel Nephinbefore adoptionf20b4e9
lines 868–874why, one step back868}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 back875}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 back891 return fmt.Errorf(`required flag(s) "%s" not set`, strings.Join(missingFlagNames, `", "`))enforce required flags (#502)Di Xubefore adoption4d6af28
lines 892–893why, one step back892 }893 return nilAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 894–895why, one step back894}895Make initHelpFlag publicAlbert Nigmatzianovbefore adoptionfe69f2e
lines 896–897why, one step back896// 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 back898// If c already has help flag, it will do nothing.Make initHelpFlag publicAlbert Nigmatzianovbefore adoptionfe69f2e
line 899why, one step back899func (c *Command) InitDefaultHelpFlag() {Merge pull request #286 from dnephin/merge_flags_before_adding_helpEric Parisbefore adoptionec2fe78
line 900why, one step back900 c.mergePersistentFlags()Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 901why, one step back901 if c.Flags().Lookup("help") == nil {Initialize help flag in initHelpCmdAlbert Nigmatzianovbefore adoption867d414
lines 902–908why, one step back902 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 back909 }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 back930 }931}932Make initHelpCmd public (#436)Albert Nigmatzianovbefore adoption52ae6a1
lines 933–936why, one step back933// 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 back937 if !c.HasSubCommands() {Make initHelpCmd public (#436)Albert Nigmatzianovbefore adoption52ae6a1
lines 938–939why, one step back938 return939 }Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 940why, one step back940Fix InitDefaultHelpCmd when custom help command is setAlbert Nigmatzianovbefore adoption8c6fa02
lines 941–945why, one step back941 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 back946Simply 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 back947Fix InitDefaultHelpCmd when custom help command is setAlbert Nigmatzianovbefore adoption8c6fa02
lines 948–958why, one step back948 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 back959 }remove help command before adding it (#399)Tamir Dubersteinbefore adoption7be4bed
line 960why, one step back960 c.RemoveCommand(c.helpCommand)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 961–963why, one step back961 c.AddCommand(c.helpCommand)962}963Edit ResetFlags and ResetCommands descriptionsAlbert Nigmatzianovbefore adoptiond6a4305
line 964why, one step back964// ResetCommands delete parent, subcommand and help command from c.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 965why, one step back965func (c *Command) ResetCommands() {Improve consistency of flags when using SetGlobalNormalizationFunc (#522)Diego Becciolinibefore adoption0dacccf
line 966why, one step back966 c.parent = nilAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 967–968why, one step back967 c.commands = nil968 c.helpCommand = nilAdd only missing persistent flags of parents in mergePersistentFlagsAlbert Nigmatzianovbefore adoption3d89ed4
line 969why, one step back969 c.parentsPflags = nilAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 970–971why, one step back970}971Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 972why, one step back972// Sorts commands by their names.Merge pull request #293 from andreykurilin/sort_commandsEric Parisbefore adoptionbc81c21
lines 973–979why, one step back973type commandSorterByName []*Command974975func (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 back980func (c *Command) Commands() []*Command {Merge pull request #293 from andreykurilin/sort_commandsEric Parisbefore adoptionbc81c21
line 981why, one step back981 // do not sort commands if it already sorted or sorting was disabledMerge pull request #303 from fabianofranz/testing_help_exiting_1Eric Parisbefore adoptiona272c3c
line 982why, one step back982 if EnableCommandSorting && !c.commandsAreSorted {Merge pull request #293 from andreykurilin/sort_commandsEric Parisbefore adoptionbc81c21
lines 983–985why, one step back983 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 back986 return c.commands987}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 back1014 c.commandsAreSorted = falseAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1015–1017why, one step back1015 }1016}1017Merge pull request #278 from kevintpeng/patch-1Eric Parisbefore adoptionf368244
line 1018why, one step back1018// 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 back1019func (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}1051Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1052why, one step back1052// 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 back1053func (c *Command) Print(i ...interface{}) {Merge pull request #305 from fabianofranz/expose_outEric Parisbefore adoptionb24564e
line 1054why, one step back1054 fmt.Fprint(c.OutOrStderr(), i...)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1055–1056why, one step back1055}1056Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1057why, one step back1057// 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 back1058func (c *Command) Println(i ...interface{}) {Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af
line 1059why, one step back1059 c.Print(fmt.Sprintln(i...))Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1060–1061why, one step back1060}1061Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1062why, one step back1062// 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 back1063func (c *Command) Printf(format string, i ...interface{}) {Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af
line 1064why, one step back1064 c.Print(fmt.Sprintf(format, i...))Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1065–1068why, one step back1065}10661067// CommandPath returns the full path to this command.1068func (c *Command) CommandPath() string {Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af
lines 1069–1070why, one step back1069 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 back1071 }Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af
line 1072why, one step back1072 return c.Name()Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1073–1074why, one step back1073}1074Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1075why, one step back1075// 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 back1076func (c *Command) UseLine() string {Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af
line 1077why, one step back1077 var useline stringAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1078why, one step back1078 if c.HasParent() {Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af
lines 1079–1081why, one step back1079 useline = c.parent.CommandPath() + " " + c.Use1080 } else {1081 useline = c.UseDisable flags in UseLine (#552)Daniel Nephinbefore adoption34ceca5
lines 1082–1084why, one step back1082 }1083 if c.DisableFlagsInUseLine {1084 return uselineAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1085why, one step back1085 }Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af
lines 1086–1089why, one step back1086 if c.HasAvailableFlags() && !strings.Contains(useline, "[flags]") {1087 useline += " [flags]"1088 }1089 return uselineAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1090–1091why, one step back1090}1091Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1092why, one step back1092// DebugFlags used to determine which flags have been assigned to which commandsMerge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1093why, one step back1093// and which persist.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1094–1103why, one step back1094func (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 back1104 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 back1106 } 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 back1122 c.Println(x.flagErrorBuf)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1123–1134why, one step back1123 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 back1135 name := c.Use1136 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 back1139 }Delete name field from Command (#547)Albert Nigmatzianovbefore adoption40f1880
line 1140why, one step back1140 return nameAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1141–1142why, one step back1141}1142Merge pull request #260 from ianwalter/code-cleanupEric Parisbefore adoptionb0d571e
line 1143why, one step back1143// 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 back1144func (c *Command) HasAlias(s string) bool {1145 for _, a := range c.Aliases {1146 if a == s {1147 return true1148 }1149 }1150 return false1151}1152Add `CalledAs` method to cobra.Command (w/ tests) (#567)Tim Peoplesbefore adoptioneb58983
lines 1153–1161why, one step back1153// CalledAs returns the command name or alias that was used to invoke1154// 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}1161Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03
lines 1162–1165why, one step back1162// hasNameOrAliasPrefix returns true if the Name or any of aliases start1163// 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 back1166 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 back1167 return true1168 }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 back1171 c.commandCalledAs.name = aliasAdd a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03
lines 1172–1178why, one step back1172 return true1173 }1174 }1175 return false1176}11771178// NameAndAliases returns a list of the command name and all aliasesAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1179–1182why, one step back1179func (c *Command) NameAndAliases() string {1180 return strings.Join(append([]string{c.Name()}, c.Aliases...), ", ")1181}1182Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1183why, one step back1183// HasExample determines if the command has example.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1184–1187why, one step back1184func (c *Command) HasExample() bool {1185 return len(c.Example) > 01186}1187Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1188why, one step back1188// Runnable determines if the command is itself runnable.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1189–1192why, one step back1189func (c *Command) Runnable() bool {1190 return c.Run != nil || c.RunE != nil1191}1192Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1193why, one step back1193// HasSubCommands determines if the command has children commands.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1194–1198why, one step back1194func (c *Command) HasSubCommands() bool {1195 return len(c.commands) > 01196}11971198// IsAvailableCommand determines if a command is available as a non-help commandMerge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1199why, one step back1199// (this includes all non deprecated/hidden commands).Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1200–1215why, one step back1200func (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}1215Rename IsHelpCommand to IsAdditionalHelpTopicCommand (#398)Albert Nigmatzianovbefore adoption0960ff7
lines 1216–1221why, one step back1216// IsAdditionalHelpTopicCommand determines if a command is an additional1217// 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 back1222 // if a command is runnable, deprecated, or hidden it is not a 'help' command1223 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 back1229 if !sub.IsAdditionalHelpTopicCommand() {Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1230–1237why, one step back1230 return false1231 }1232 }12331234 // the command either has no sub commands, or no non-help sub commands1235 return true1236}1237Merge pull request #327 from bogem/fixesEric Parisbefore adoption1f47171
line 1238why, one step back1238// HasHelpSubCommands determines if a command has any available 'help' sub commandsAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1239why, one step back1239// that need to be shown in the usage/help default template under 'additional helpMerge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1240why, one step back1240// topics'.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1241–1243why, one step back1241func (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 back1244 if sub.IsAdditionalHelpTopicCommand() {Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1245–1253why, one step back1245 return true1246 }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 thatMerge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1254why, one step back1254// 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 back1255func (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 }1263Fix typo (#573)Pavol Loffaybefore adoptionf634327
line 1264why, one step back1264 // 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 back1265 // sub commands1266 return false1267}1268Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1269why, one step back1269// 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 back1270func (c *Command) HasParent() bool {1271 return c.parent != nil1272}1273fix grammatical error in command.go (#626)Shamus Taylorbefore adoption3a7eb14
line 1274why, one step back1274// 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 back1275func (c *Command) GlobalNormalizationFunc() func(f *flag.FlagSet, name string) flag.NormalizedName {1276 return c.globNormFunc1277}1278Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1279why, one step back1279// Flags returns the complete FlagSet that appliesMerge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1280why, one step back1280// 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 back1281func (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 back1284 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 back1288 }Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
line 1289why, one step back1289Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1290–1292why, one step back1290 return c.flags1291}1292Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1293why, one step back1293// 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 back1294func (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}1305Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1306why, one step back1306// 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 back1307func (c *Command) LocalFlags() *flag.FlagSet {1308 c.mergePersistentFlags()1309Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
lines 1310–1311why, one step back1310 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 back1312 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 back1316 }Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867
line 1317why, one step back1317 c.lflags.SortFlags = c.Flags().SortFlagsImprove consistency of flags when using SetGlobalNormalizationFunc (#522)Diego Becciolinibefore adoption0dacccf
lines 1318–1320why, one step back1318 if c.globNormFunc != nil {1319 c.lflags.SetNormalizeFunc(c.globNormFunc)1320 }Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
lines 1321–1326why, one step back13211322 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 back1327 c.Flags().VisitAll(addToLocal)Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
lines 1328–1329why, one step back1328 c.PersistentFlags().VisitAll(addToLocal)1329 return c.lflagsAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1330–1331why, one step back1330}1331Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1332why, one step back1332// 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 back1333func (c *Command) InheritedFlags() *flag.FlagSet {1334 c.mergePersistentFlags()1335Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
lines 1336–1337why, one step back1336 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 back1338 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 back1342 }1343Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1344why, one step back1344 local := c.LocalFlags()Improve consistency of flags when using SetGlobalNormalizationFunc (#522)Diego Becciolinibefore adoption0dacccf
lines 1345–1348why, one step back1345 if c.globNormFunc != nil {1346 c.iflags.SetNormalizeFunc(c.globNormFunc)1347 }1348Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
lines 1349–1351why, one step back1349 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 back1352 }Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
lines 1353–1354why, one step back1353 })1354 return c.iflagsAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1355–1356why, one step back1355}1356Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1357why, one step back1357// 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 back1358func (c *Command) NonInheritedFlags() *flag.FlagSet {1359 return c.LocalFlags()1360}1361Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1362why, one step back1362// 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 back1363func (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 back1366 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 back1370 }1371 return c.pflags1372}1373Edit ResetFlags and ResetCommands descriptionsAlbert Nigmatzianovbefore adoptiond6a4305
line 1374why, one step back1374// ResetFlags deletes all flags from command.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1375why, one step back1375func (c *Command) ResetFlags() {Revert "Get rid of flagErrorBuf (#417)"Albert Nigmatzianovbefore adoption3d7bff8
lines 1376–1377why, one step back1376 c.flagErrorBuf = new(bytes.Buffer)1377 c.flagErrorBuf.Reset()Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1378why, one step back1378 c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)Revert "Get rid of flagErrorBuf (#417)"Albert Nigmatzianovbefore adoption3d7bff8
line 1379why, one step back1379 c.flags.SetOutput(c.flagErrorBuf)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1380why, one step back1380 c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)Revert "Get rid of flagErrorBuf (#417)"Albert Nigmatzianovbefore adoption3d7bff8
line 1381why, one step back1381 c.pflags.SetOutput(c.flagErrorBuf)Improve consistency of flags when using SetGlobalNormalizationFunc (#522)Diego Becciolinibefore adoption0dacccf
lines 1382–1385why, one step back13821383 c.lflags = nil1384 c.iflags = nil1385 c.parentsPflags = nilAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1386–1387why, one step back1386}1387Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1388why, one step back1388// 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 back1389func (c *Command) HasFlags() bool {1390 return c.Flags().HasFlags()1391}1392Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1393why, one step back1393// HasPersistentFlags checks if the command contains persistent flags.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1394–1397why, one step back1394func (c *Command) HasPersistentFlags() bool {1395 return c.PersistentFlags().HasFlags()1396}1397Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1398why, one step back1398// 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 back1399func (c *Command) HasLocalFlags() bool {1400 return c.LocalFlags().HasFlags()1401}1402Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1403why, one step back1403// 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 back1404func (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 back1406}1407Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1408why, one step back1408// HasAvailableFlags checks if the command contains any flags (local plus persistent from the entireMerge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1409why, one step back1409// structure) which are not hidden or deprecated.Issue #267: Ensure hidden flags/subcommands do not change helpJohn Schnakebefore adoptione14e47b
lines 1410–1413why, one step back1410func (c *Command) HasAvailableFlags() bool {1411 return c.Flags().HasAvailableFlags()1412}1413Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1414why, one step back1414// 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 back1415func (c *Command) HasAvailablePersistentFlags() bool {1416 return c.PersistentFlags().HasAvailableFlags()1417}1418Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1419why, one step back1419// HasAvailableLocalFlags checks if the command has flags specifically declared locally which are not hiddenMerge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1420why, one step back1420// or deprecated.Issue #267: Ensure hidden flags/subcommands do not change helpJohn Schnakebefore adoptione14e47b
lines 1421–1424why, one step back1421func (c *Command) HasAvailableLocalFlags() bool {1422 return c.LocalFlags().HasAvailableFlags()1423}1424Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1425why, one step back1425// HasAvailableInheritedFlags checks if the command has flags inherited from its parent command which areMerge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1426why, one step back1426// not hidden or deprecated.Issue #267: Ensure hidden flags/subcommands do not change helpJohn Schnakebefore adoptione14e47b
lines 1427–1428why, one step back1427func (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 back1429}1430Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1431why, one step back1431// 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 back1432func (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}1441Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1442why, one step back1442// Recursively find matching persistent flag.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1443–1447why, one step back1443func (c *Command) persistentFlag(name string) (flag *flag.Flag) {1444 if c.HasPersistentFlags() {1445 flag = c.PersistentFlags().Lookup(name)1446 }1447Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867
lines 1448–1450why, one step back1448 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 back1451 }1452 return1453}1454Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1455why, one step back1455// ParseFlags parses persistent flag tree and local flags.Show messages if deprecated flags are usedAlbert Nigmatzianovbefore adoption99b5d83
line 1456why, one step back1456func (c *Command) ParseFlags(args []string) error {Merge pull request #289 from markbates/disable-flagsEric Parisbefore adoptionf447048
lines 1457–1459why, one step back1457 if c.DisableFlagParsing {1458 return nil1459 }Show messages if deprecated flags are usedAlbert Nigmatzianovbefore adoption99b5d83
line 1460why, one step back1460Create new buffer if not present yet (#549)Thomas Cyronbefore adoption7b2c5ac
lines 1461–1463why, one step back1461 if c.flagErrorBuf == nil {1462 c.flagErrorBuf = new(bytes.Buffer)1463 }Show messages if deprecated flags are usedAlbert Nigmatzianovbefore adoption99b5d83
line 1464why, one step back1464 beforeErrorBufLen := c.flagErrorBuf.Len()Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1465why, one step back1465 c.mergePersistentFlags()Show messages if deprecated flags are usedAlbert Nigmatzianovbefore adoption99b5d83
lines 1466–1472why, one step back1466 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 errAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1473–1474why, one step back1473}1474Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1475why, one step back1475// Parent returns a commands parent command.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1476–1479why, one step back1476func (c *Command) Parent() *Command {1477 return c.parent1478}1479Add only missing persistent flags of parents in mergePersistentFlagsAlbert Nigmatzianovbefore adoption3d89ed4
lines 1480–1482why, one step back1480// 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 back1483 c.updateParentsPflags()Fix incorrect merge of pflag.CommandLine to cmd.Flags()Albert Nigmatzianovbefore adoption7d22e77
line 1484why, one step back1484 c.Flags().AddFlagSet(c.PersistentFlags())Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867
line 1485why, one step back1485 c.Flags().AddFlagSet(c.parentsPflags)Add only missing persistent flags of parents in mergePersistentFlagsAlbert Nigmatzianovbefore adoption3d89ed4
lines 1486–1488why, one step back1486}14871488// updateParentsPflags updates c.parentsPflags by addingMake parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867
line 1489why, one step back1489// new persistent flags of all parents.Add only missing persistent flags of parents in mergePersistentFlagsAlbert Nigmatzianovbefore adoption3d89ed4
line 1490why, one step back1490// If c.parentsPflags == nil, it makes new.Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867
line 1491why, one step back1491func (c *Command) updateParentsPflags() {Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
lines 1492–1493why, one step back1492 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 back1494 c.parentsPflags.SetOutput(c.flagErrorBuf)Add only missing persistent flags of parents in mergePersistentFlagsAlbert Nigmatzianovbefore adoption3d89ed4
line 1495why, one step back1495 c.parentsPflags.SortFlags = falseImprove consistency of flags when using SetGlobalNormalizationFunc (#522)Diego Becciolinibefore adoption0dacccf
lines 1496–1499why, one step back1496 }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 back1500 }Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
line 1501why, one step back1501Add merging of CommandLine flags to Root commandAlbert Nigmatzianovbefore adoption458d797
lines 1502–1503why, one step back1502 c.Root().PersistentFlags().AddFlagSet(flag.CommandLine)1503Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867
lines 1504–1505why, one step back1504 c.VisitParents(func(parent *Command) {1505 c.parentsPflags.AddFlagSet(parent.PersistentFlags())Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
line 1506why, one step back1506 })Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1507why, one step back1507}