command.go
This file
Path
command.go
1,504 lines · 41.4 KiB · Go
Last changed by
Add `CalledAs` method to cobra.Command (w/ tests) (#567)Tim Peoplesbefore adoptioneb58983
History of this 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
lines 477–489why, one step back477 switch {478 case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], flags):479 // If '--flag arg' then480 // delete arg from args.481 fallthrough // (do the same as below)482 case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags):483 // If '-f arg' then484 // delete 'arg' from args or break the loop if len(args) <= 1.485 if len(args) <= 1 {486 break Loop487 } else {488 args = args[1:]489 continueAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 490why, one step back490 }Fix tests so they give correct args (#445)Albert Nigmatzianovbefore adoption51b7cf5
lines 491–492why, one step back491 case s != "" && !strings.HasPrefix(s, "-"):492 commands = append(commands, s)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 493–512why, one step back493 }494 }495496 return commands497}498499// argsMinusFirstX removes only the first x from args. Otherwise, commands that look like500// openshift admin policy add-role-to-user admin my-user, lose the admin argument (arg[4]).501func argsMinusFirstX(args []string, x string) []string {502 for i, y := range args {503 if x == y {504 ret := []string{}505 ret = append(ret, args[:i]...)506 ret = append(ret, args[i+1:]...)507 return ret508 }509 }510 return args511}512Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03
lines 513–517why, one step back513func isFlagArg(arg string) bool {514 return ((len(arg) >= 3 && arg[1] == '-') ||515 (len(arg) >= 2 && arg[0] == '-' && arg[1] != '-'))516}517Add a tags field for user defined tags on a Command. (#359)Daniel Nephinbefore adoption99600f6
line 518why, one step back518// Find the target command given the args and command treeAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 519–529why, one step back519// Meant to be run on the highest node. Only searches down.520func (c *Command) Find(args []string) (*Command, []string, error) {521 var innerfind func(*Command, []string) (*Command, []string)522523 innerfind = func(c *Command, innerArgs []string) (*Command, []string) {524 argsWOflags := stripFlags(innerArgs, c)525 if len(argsWOflags) == 0 {526 return c, innerArgs527 }528 nextSubCmd := argsWOflags[0]529Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03
lines 530–532why, one step back530 cmd := c.findNext(nextSubCmd)531 if cmd != nil {532 return innerfind(cmd, argsMinusFirstX(innerArgs, nextSubCmd))Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 533–537why, one step back533 }534 return c, innerArgs535 }536537 commandFound, a := innerfind(c, args)Refactor TakesArgs to use an interface for arg validation.Daniel Nephinbefore adoptionf20b4e9
lines 538–539why, one step back538 if commandFound.Args == nil {539 return commandFound, a, legacyArgs(commandFound, stripFlags(a, commandFound))Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 540–541why, one step back540 }541 return commandFound, a, nilAllow commands to explicitly state if they do, or do not take arbitrary argumentsEric Parisbefore adoptiond89c499
lines 542–543why, one step back542}543Refactor TakesArgs to use an interface for arg validation.Daniel Nephinbefore adoptionf20b4e9
line 544why, one step back544func (c *Command) findSuggestions(arg string) string {Allow commands to explicitly state if they do, or do not take arbitrary argumentsEric Parisbefore adoptiond89c499
lines 545–551why, one step back545 if c.DisableSuggestions {546 return ""547 }548 if c.SuggestionsMinimumDistance <= 0 {549 c.SuggestionsMinimumDistance = 2550 }551 suggestionsString := ""Refactor TakesArgs to use an interface for arg validation.Daniel Nephinbefore adoptionf20b4e9
line 552why, one step back552 if suggestions := c.SuggestionsFor(arg); len(suggestions) > 0 {Allow commands to explicitly state if they do, or do not take arbitrary argumentsEric Parisbefore adoptiond89c499
lines 553–558why, one step back553 suggestionsString += "\n\nDid you mean this?\n"554 for _, s := range suggestions {555 suggestionsString += fmt.Sprintf("\t%v\n", s)556 }557 }558 return suggestionsStringAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 559–560why, one step back559}560Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03
lines 561–564why, one step back561func (c *Command) findNext(next string) *Command {562 matches := make([]*Command, 0)563 for _, cmd := range c.commands {564 if cmd.Name() == next || cmd.HasAlias(next) {Add `CalledAs` method to cobra.Command (w/ tests) (#567)Tim Peoplesbefore adoptioneb58983
line 565why, one step back565 cmd.commandCalledAs.name = nextAdd a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03
lines 566–575why, one step back566 return cmd567 }568 if EnablePrefixMatching && cmd.hasNameOrAliasPrefix(next) {569 matches = append(matches, cmd)570 }571 }572573 if len(matches) == 1 {574 return matches[0]575 }Add `CalledAs` method to cobra.Command (w/ tests) (#567)Tim Peoplesbefore adoptioneb58983
line 576why, one step back576Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03
lines 577–622why, one step back577 return nil578}579580// Traverse the command tree to find the command, and parse args for581// each parent.582func (c *Command) Traverse(args []string) (*Command, []string, error) {583 flags := []string{}584 inFlag := false585586 for i, arg := range args {587 switch {588 // A long flag with a space separated value589 case strings.HasPrefix(arg, "--") && !strings.Contains(arg, "="):590 // TODO: this isn't quite right, we should really check ahead for 'true' or 'false'591 inFlag = !hasNoOptDefVal(arg[2:], c.Flags())592 flags = append(flags, arg)593 continue594 // A short flag with a space separated value595 case strings.HasPrefix(arg, "-") && !strings.Contains(arg, "=") && len(arg) == 2 && !shortHasNoOptDefVal(arg[1:], c.Flags()):596 inFlag = true597 flags = append(flags, arg)598 continue599 // The value for a flag600 case inFlag:601 inFlag = false602 flags = append(flags, arg)603 continue604 // A flag without a value, or with an `=` separated value605 case isFlagArg(arg):606 flags = append(flags, arg)607 continue608 }609610 cmd := c.findNext(arg)611 if cmd == nil {612 return c, args, nil613 }614615 if err := c.ParseFlags(flags); err != nil {616 return nil, args, err617 }618 return cmd.Traverse(args[i+1:])619 }620 return c, args, nil621}622Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 623why, one step back623// SuggestionsFor provides suggestions for the typedName.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 624–643why, one step back624func (c *Command) SuggestionsFor(typedName string) []string {625 suggestions := []string{}626 for _, cmd := range c.commands {627 if cmd.IsAvailableCommand() {628 levenshteinDistance := ld(typedName, cmd.Name(), true)629 suggestByLevenshtein := levenshteinDistance <= c.SuggestionsMinimumDistance630 suggestByPrefix := strings.HasPrefix(strings.ToLower(cmd.Name()), strings.ToLower(typedName))631 if suggestByLevenshtein || suggestByPrefix {632 suggestions = append(suggestions, cmd.Name())633 }634 for _, explicitSuggestion := range cmd.SuggestFor {635 if strings.EqualFold(typedName, explicitSuggestion) {636 suggestions = append(suggestions, cmd.Name())637 }638 }639 }640 }641 return suggestions642}643Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 644why, one step back644// VisitParents visits all parents of the command and invokes fn on each parent.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 645why, one step back645func (c *Command) VisitParents(fn func(*Command)) {Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
lines 646–648why, one step back646 if c.HasParent() {647 fn(c.Parent())648 c.Parent().VisitParents(fn)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 649–651why, one step back649 }650}651Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 652why, one step back652// Root finds root command.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 653why, one step back653func (c *Command) Root() *Command {Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
lines 654–655why, one step back654 if c.HasParent() {655 return c.Parent().Root()Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 656why, one step back656 }Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
line 657why, one step back657 return cAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 658–659why, one step back658}659Improve testsAlbert Nigmatzianovbefore adoption65c8acb
lines 660–661why, one step back660// ArgsLenAtDash will return the length of c.Flags().Args at the moment661// when a -- was found during args parsing.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 662–674why, one step back662func (c *Command) ArgsLenAtDash() int {663 return c.Flags().ArgsLenAtDash()664}665666func (c *Command) execute(a []string) (err error) {667 if c == nil {668 return fmt.Errorf("Called Execute() on a nil Command")669 }670671 if len(c.Deprecated) > 0 {672 c.Printf("Command %q is deprecated, %s\n", c.Name(), c.Deprecated)673 }674Add support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce
line 675why, one step back675 // 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 676why, one step back676 // overridingMake initHelpFlag publicAlbert Nigmatzianovbefore adoptionfe69f2e
line 677why, one step back677 c.InitDefaultHelpFlag()Add support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce
line 678why, one step back678 c.InitDefaultVersionFlag()Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 679–681why, one step back679680 err = c.ParseFlags(a)681 if err != nil {Merge pull request #283 from dnephin/add_flag_usage_funcEric Parisbefore adoptionb620a75
line 682why, one step back682 return c.FlagErrorFunc()(c, err)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 683why, one step back683 }Make initHelpFlag publicAlbert Nigmatzianovbefore adoptionfe69f2e
lines 684–685why, one step back684685 // If help is called, regardless of other flags, return we want help.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 686–689why, one step back686 // Also say we need help if the command isn't runnable.687 helpVal, err := c.Flags().GetBool("help")688 if err != nil {689 // should be impossible to get here as we always declare a helpMake initHelpFlag publicAlbert Nigmatzianovbefore adoptionfe69f2e
line 690why, one step back690 // flag in InitDefaultHelpFlag()Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 691–693why, one step back691 c.Println("\"help\" flag declared as non-bool. Please correct your code")692 return err693 }Merge pull request #303 from fabianofranz/testing_help_exiting_1Eric Parisbefore adoptiona272c3c
line 694why, one step back694Ensure that '--version' flag works properly for root command (#595)Nick Miyakebefore adoptionccaecb1
line 695why, one step back695 if helpVal {Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 696–698why, one step back696 return flag.ErrHelp697 }698Add support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce
lines 699–712why, one step back699 // for back-compat, only add version flag behavior if version is defined700 if c.Version != "" {701 versionVal, err := c.Flags().GetBool("version")702 if err != nil {703 c.Println("\"version\" flag declared as non-bool. Please correct your code")704 return err705 }706 if versionVal {707 err := tmpl(c.OutOrStdout(), c.VersionTemplate(), c)708 if err != nil {709 c.Println(err)710 }711 return err712 }Ensure that '--version' flag works properly for root command (#595)Nick Miyakebefore adoptionccaecb1
lines 713–716why, one step back713 }714715 if !c.Runnable() {716 return flag.ErrHelpAdd support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce
lines 717–718why, one step back717 }718Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 719why, one step back719 c.preRun()Merge pull request #289 from markbates/disable-flagsEric Parisbefore adoptionf447048
line 720why, one step back720Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 721why, one step back721 argWoFlags := c.Flags().Args()Merge pull request #289 from markbates/disable-flagsEric Parisbefore adoptionf447048
lines 722–724why, one step back722 if c.DisableFlagParsing {723 argWoFlags = a724 }Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 725why, one step back725Refactor TakesArgs to use an interface for arg validation.Daniel Nephinbefore adoptionf20b4e9
lines 726–729why, one step back726 if err := c.ValidateArgs(argWoFlags); err != nil {727 return err728 }729Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 730–748why, one step back730 for p := c; p != nil; p = p.Parent() {731 if p.PersistentPreRunE != nil {732 if err := p.PersistentPreRunE(c, argWoFlags); err != nil {733 return err734 }735 break736 } else if p.PersistentPreRun != nil {737 p.PersistentPreRun(c, argWoFlags)738 break739 }740 }741 if c.PreRunE != nil {742 if err := c.PreRunE(c, argWoFlags); err != nil {743 return err744 }745 } else if c.PreRun != nil {746 c.PreRun(c, argWoFlags)747 }748enforce required flags (#502)Di Xubefore adoption4d6af28
lines 749–751why, one step back749 if err := c.validateRequiredFlags(); err != nil {750 return err751 }Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 752–786why, one step back752 if c.RunE != nil {753 if err := c.RunE(c, argWoFlags); err != nil {754 return err755 }756 } else {757 c.Run(c, argWoFlags)758 }759 if c.PostRunE != nil {760 if err := c.PostRunE(c, argWoFlags); err != nil {761 return err762 }763 } else if c.PostRun != nil {764 c.PostRun(c, argWoFlags)765 }766 for p := c; p != nil; p = p.Parent() {767 if p.PersistentPostRunE != nil {768 if err := p.PersistentPostRunE(c, argWoFlags); err != nil {769 return err770 }771 break772 } else if p.PersistentPostRun != nil {773 p.PersistentPostRun(c, argWoFlags)774 break775 }776 }777778 return nil779}780781func (c *Command) preRun() {782 for _, x := range initializers {783 x()784 }785}786Fix typosAlbert Nigmatzianovbefore adoptione606913
line 787why, one step back787// Execute uses the args (os.Args[1:] by default)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 788–794why, one step back788// and run through the command tree finding appropriate matches789// for commands and then corresponding flags.790func (c *Command) Execute() error {791 _, err := c.ExecuteC()792 return err793}794Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 795why, one step back795// ExecuteC executes the command.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 796–808why, one step back796func (c *Command) ExecuteC() (cmd *Command, err error) {797 // Regardless of what command execute is called on, run on Root only798 if c.HasParent() {799 return c.Root().ExecuteC()800 }801802 // windows hook803 if preExecHookFn != nil {804 preExecHookFn(c)805 }806807 // initialize help as the last point possible to allow for user808 // overridingMake initHelpCmd public (#436)Albert Nigmatzianovbefore adoption52ae6a1
line 809why, one step back809 c.InitDefaultHelpCmd()Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 810–819why, one step back810811 var args []string812813 // Workaround FAIL with "go test -v" or "cobra.test -test.v", see #155814 if c.args == nil && filepath.Base(os.Args[0]) != "cobra.test" {815 args = os.Args[1:]816 } else {817 args = c.args818 }819Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03
lines 820–825why, one step back820 var flags []string821 if c.TraverseChildren {822 cmd, flags, err = c.Traverse(args)823 } else {824 cmd, flags, err = c.Find(args)825 }Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 826–836why, one step back826 if err != nil {827 // If found parse to a subcommand and then failed, talk about the subcommand828 if cmd != nil {829 c = cmd830 }831 if !c.SilenceErrors {832 c.Println("Error:", err.Error())833 c.Printf("Run '%v --help' for usage.\n", c.CommandPath())834 }835 return c, err836 }Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03
line 837why, one step back837Add `CalledAs` method to cobra.Command (w/ tests) (#567)Tim Peoplesbefore adoptioneb58983
lines 838–842why, one step back838 cmd.commandCalledAs.called = true839 if cmd.commandCalledAs.name == "" {840 cmd.commandCalledAs.name = cmd.Name()841 }842Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 843–863why, one step back843 err = cmd.execute(flags)844 if err != nil {845 // Always show help if requested, even if SilenceErrors is in846 // effect847 if err == flag.ErrHelp {848 cmd.HelpFunc()(cmd, args)849 return cmd, nil850 }851852 // If root command has SilentErrors flagged,853 // all subcommands should respect it854 if !cmd.SilenceErrors && !c.SilenceErrors {855 c.Println("Error:", err.Error())856 }857858 // If root command has SilentUsage flagged,859 // all subcommands should respect it860 if !cmd.SilenceUsage && !c.SilenceUsage {861 c.Println(cmd.UsageString())862 }863 }Fix typosAlbert Nigmatzianovbefore adoptione606913
line 864why, one step back864 return cmd, errRefactor TakesArgs to use an interface for arg validation.Daniel Nephinbefore adoptionf20b4e9
lines 865–871why, one step back865}866867func (c *Command) ValidateArgs(args []string) error {868 if c.Args == nil {869 return nil870 }871 return c.Args(c, args)enforce required flags (#502)Di Xubefore adoption4d6af28
lines 872–887why, one step back872}873874func (c *Command) validateRequiredFlags() error {875 flags := c.Flags()876 missingFlagNames := []string{}877 flags.VisitAll(func(pflag *flag.Flag) {878 requiredAnnotation, found := pflag.Annotations[BashCompOneRequiredFlag]879 if !found {880 return881 }882 if (requiredAnnotation[0] == "true") && !pflag.Changed {883 missingFlagNames = append(missingFlagNames, pflag.Name)884 }885 })886887 if len(missingFlagNames) > 0 {Update error message for missing required flags (#580)Nick Miyakebefore adoption19e54c4
line 888why, one step back888 return fmt.Errorf(`required flag(s) "%s" not set`, strings.Join(missingFlagNames, `", "`))enforce required flags (#502)Di Xubefore adoption4d6af28
lines 889–890why, one step back889 }890 return nilAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 891–892why, one step back891}892Make initHelpFlag publicAlbert Nigmatzianovbefore adoptionfe69f2e
lines 893–894why, one step back893// InitDefaultHelpFlag adds default help flag to c.894// It is called automatically by executing the c or by calling help and usage.Add comment for InitDefaultHelpFlagAlbert Nigmatzianovbefore adoptiond83a1d7
line 895why, one step back895// If c already has help flag, it will do nothing.Make initHelpFlag publicAlbert Nigmatzianovbefore adoptionfe69f2e
line 896why, one step back896func (c *Command) InitDefaultHelpFlag() {Merge pull request #286 from dnephin/merge_flags_before_adding_helpEric Parisbefore adoptionec2fe78
line 897why, one step back897 c.mergePersistentFlags()Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 898why, one step back898 if c.Flags().Lookup("help") == nil {Initialize help flag in initHelpCmdAlbert Nigmatzianovbefore adoption867d414
lines 899–905why, one step back899 usage := "help for "900 if c.Name() == "" {901 usage += "this command"902 } else {903 usage += c.Name()904 }905 c.Flags().BoolP("help", "h", false, usage)Add support for --version flag (#584)Nick Miyakebefore adoptionb1ec2ce
lines 906–926why, one step back906 }907}908909// InitDefaultVersionFlag adds default version flag to c.910// It is called automatically by executing the c.911// If c already has a version flag, it will do nothing.912// If c.Version is empty, it will do nothing.913func (c *Command) InitDefaultVersionFlag() {914 if c.Version == "" {915 return916 }917918 c.mergePersistentFlags()919 if c.Flags().Lookup("version") == nil {920 usage := "version for "921 if c.Name() == "" {922 usage += "this command"923 } else {924 usage += c.Name()925 }926 c.Flags().Bool("version", false, usage)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 927–929why, one step back927 }928}929Make initHelpCmd public (#436)Albert Nigmatzianovbefore adoption52ae6a1
lines 930–933why, one step back930// InitDefaultHelpCmd adds default help command to c.931// It is called automatically by executing the c or by calling help and usage.932// If c already has help command or c has no subcommands, it will do nothing.933func (c *Command) InitDefaultHelpCmd() {Fix InitDefaultHelpCmd when custom help command is setAlbert Nigmatzianovbefore adoption8c6fa02
line 934why, one step back934 if !c.HasSubCommands() {Make initHelpCmd public (#436)Albert Nigmatzianovbefore adoption52ae6a1
lines 935–936why, one step back935 return936 }Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 937why, one step back937Fix InitDefaultHelpCmd when custom help command is setAlbert Nigmatzianovbefore adoption8c6fa02
lines 938–942why, one step back938 if c.helpCommand == nil {939 c.helpCommand = &Command{940 Use: "help [command]",941 Short: "Help about any command",942 Long: `Help provides help for any command in the application.Delete tab in the long description of helpAlbert Nigmatzianovbefore adoptiond994347
line 943why, one step back943Simply type ` + c.Name() + ` help [path to command] for full details.`,Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 944why, one step back944Fix InitDefaultHelpCmd when custom help command is setAlbert Nigmatzianovbefore adoption8c6fa02
lines 945–955why, one step back945 Run: func(c *Command, args []string) {946 cmd, _, e := c.Root().Find(args)947 if cmd == nil || e != nil {948 c.Printf("Unknown help topic %#q\n", args)949 c.Root().Usage()950 } else {951 cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown952 cmd.Help()953 }954 },955 }Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 956why, one step back956 }remove help command before adding it (#399)Tamir Dubersteinbefore adoption7be4bed
line 957why, one step back957 c.RemoveCommand(c.helpCommand)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 958–960why, one step back958 c.AddCommand(c.helpCommand)959}960Edit ResetFlags and ResetCommands descriptionsAlbert Nigmatzianovbefore adoptiond6a4305
line 961why, one step back961// ResetCommands delete parent, subcommand and help command from c.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 962why, one step back962func (c *Command) ResetCommands() {Improve consistency of flags when using SetGlobalNormalizationFunc (#522)Diego Becciolinibefore adoption0dacccf
line 963why, one step back963 c.parent = nilAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 964–965why, one step back964 c.commands = nil965 c.helpCommand = nilAdd only missing persistent flags of parents in mergePersistentFlagsAlbert Nigmatzianovbefore adoption3d89ed4
line 966why, one step back966 c.parentsPflags = nilAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 967–968why, one step back967}968Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 969why, one step back969// Sorts commands by their names.Merge pull request #293 from andreykurilin/sort_commandsEric Parisbefore adoptionbc81c21
lines 970–976why, one step back970type commandSorterByName []*Command971972func (c commandSorterByName) Len() int { return len(c) }973func (c commandSorterByName) Swap(i, j int) { c[i], c[j] = c[j], c[i] }974func (c commandSorterByName) Less(i, j int) bool { return c[i].Name() < c[j].Name() }975976// Commands returns a sorted slice of child commands.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 977why, one step back977func (c *Command) Commands() []*Command {Merge pull request #293 from andreykurilin/sort_commandsEric Parisbefore adoptionbc81c21
line 978why, one step back978 // do not sort commands if it already sorted or sorting was disabledMerge pull request #303 from fabianofranz/testing_help_exiting_1Eric Parisbefore adoptiona272c3c
line 979why, one step back979 if EnableCommandSorting && !c.commandsAreSorted {Merge pull request #293 from andreykurilin/sort_commandsEric Parisbefore adoptionbc81c21
lines 980–982why, one step back980 sort.Sort(commandSorterByName(c.commands))981 c.commandsAreSorted = true982 }Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 983–1010why, one step back983 return c.commands984}985986// AddCommand adds one or more commands to this parent command.987func (c *Command) AddCommand(cmds ...*Command) {988 for i, x := range cmds {989 if cmds[i] == c {990 panic("Command can't be a child of itself")991 }992 cmds[i].parent = c993 // update max lengths994 usageLen := len(x.Use)995 if usageLen > c.commandsMaxUseLen {996 c.commandsMaxUseLen = usageLen997 }998 commandPathLen := len(x.CommandPath())999 if commandPathLen > c.commandsMaxCommandPathLen {1000 c.commandsMaxCommandPathLen = commandPathLen1001 }1002 nameLen := len(x.Name())1003 if nameLen > c.commandsMaxNameLen {1004 c.commandsMaxNameLen = nameLen1005 }1006 // If global normalization function exists, update all children1007 if c.globNormFunc != nil {1008 x.SetGlobalNormalizationFunc(c.globNormFunc)1009 }1010 c.commands = append(c.commands, x)Merge pull request #293 from andreykurilin/sort_commandsEric Parisbefore adoptionbc81c21
line 1011why, one step back1011 c.commandsAreSorted = falseAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1012–1014why, one step back1012 }1013}1014Merge pull request #278 from kevintpeng/patch-1Eric Parisbefore adoptionf368244
line 1015why, one step back1015// RemoveCommand removes one or more commands from a parent command.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1016–1048why, one step back1016func (c *Command) RemoveCommand(cmds ...*Command) {1017 commands := []*Command{}1018main:1019 for _, command := range c.commands {1020 for _, cmd := range cmds {1021 if command == cmd {1022 command.parent = nil1023 continue main1024 }1025 }1026 commands = append(commands, command)1027 }1028 c.commands = commands1029 // recompute all lengths1030 c.commandsMaxUseLen = 01031 c.commandsMaxCommandPathLen = 01032 c.commandsMaxNameLen = 01033 for _, command := range c.commands {1034 usageLen := len(command.Use)1035 if usageLen > c.commandsMaxUseLen {1036 c.commandsMaxUseLen = usageLen1037 }1038 commandPathLen := len(command.CommandPath())1039 if commandPathLen > c.commandsMaxCommandPathLen {1040 c.commandsMaxCommandPathLen = commandPathLen1041 }1042 nameLen := len(command.Name())1043 if nameLen > c.commandsMaxNameLen {1044 c.commandsMaxNameLen = nameLen1045 }1046 }1047}1048Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1049why, one step back1049// Print is a convenience method to Print to the defined output, fallback to Stderr if not set.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1050why, one step back1050func (c *Command) Print(i ...interface{}) {Merge pull request #305 from fabianofranz/expose_outEric Parisbefore adoptionb24564e
line 1051why, one step back1051 fmt.Fprint(c.OutOrStderr(), i...)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1052–1053why, one step back1052}1053Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1054why, one step back1054// Println is a convenience method to Println to the defined output, fallback to Stderr if not set.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1055why, one step back1055func (c *Command) Println(i ...interface{}) {Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af
line 1056why, one step back1056 c.Print(fmt.Sprintln(i...))Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1057–1058why, one step back1057}1058Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1059why, one step back1059// Printf is a convenience method to Printf to the defined output, fallback to Stderr if not set.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1060why, one step back1060func (c *Command) Printf(format string, i ...interface{}) {Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af
line 1061why, one step back1061 c.Print(fmt.Sprintf(format, i...))Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1062–1065why, one step back1062}10631064// CommandPath returns the full path to this command.1065func (c *Command) CommandPath() string {Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af
lines 1066–1067why, one step back1066 if c.HasParent() {1067 return c.Parent().CommandPath() + " " + c.Name()Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1068why, one step back1068 }Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af
line 1069why, one step back1069 return c.Name()Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1070–1071why, one step back1070}1071Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1072why, one step back1072// UseLine puts out the full usage for a given command (including parents).Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1073why, one step back1073func (c *Command) UseLine() string {Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af
line 1074why, one step back1074 var useline stringAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1075why, one step back1075 if c.HasParent() {Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af
lines 1076–1078why, one step back1076 useline = c.parent.CommandPath() + " " + c.Use1077 } else {1078 useline = c.UseDisable flags in UseLine (#552)Daniel Nephinbefore adoption34ceca5
lines 1079–1081why, one step back1079 }1080 if c.DisableFlagsInUseLine {1081 return uselineAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1082why, one step back1082 }Improve template mechanismsAlbert Nigmatzianovbefore adoption4d2c4af
lines 1083–1086why, one step back1083 if c.HasAvailableFlags() && !strings.Contains(useline, "[flags]") {1084 useline += " [flags]"1085 }1086 return uselineAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1087–1088why, one step back1087}1088Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1089why, one step back1089// DebugFlags used to determine which flags have been assigned to which commandsMerge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1090why, one step back1090// and which persist.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1091–1100why, one step back1091func (c *Command) DebugFlags() {1092 c.Println("DebugFlags called on", c.Name())1093 var debugflags func(*Command)10941095 debugflags = func(x *Command) {1096 if x.HasFlags() || x.HasPersistentFlags() {1097 c.Println(x.Name())1098 }1099 if x.HasFlags() {1100 x.flags.VisitAll(func(f *flag.Flag) {Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
lines 1101–1102why, one step back1101 if x.HasPersistentFlags() && x.persistentFlag(f.Name) != nil {1102 c.Println(" -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, " [LP]")Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1103–1118why, one step back1103 } else {1104 c.Println(" -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, " [L]")1105 }1106 })1107 }1108 if x.HasPersistentFlags() {1109 x.pflags.VisitAll(func(f *flag.Flag) {1110 if x.HasFlags() {1111 if x.flags.Lookup(f.Name) == nil {1112 c.Println(" -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, " [P]")1113 }1114 } else {1115 c.Println(" -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, " [P]")1116 }1117 })1118 }Revert "Get rid of flagErrorBuf (#417)"Albert Nigmatzianovbefore adoption3d7bff8
line 1119why, one step back1119 c.Println(x.flagErrorBuf)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1120–1131why, one step back1120 if x.HasSubCommands() {1121 for _, y := range x.commands {1122 debugflags(y)1123 }1124 }1125 }11261127 debugflags(c)1128}11291130// Name returns the command's name: the first word in the use line.1131func (c *Command) Name() string {Delete name field from Command (#547)Albert Nigmatzianovbefore adoption40f1880
lines 1132–1135why, one step back1132 name := c.Use1133 i := strings.Index(name, " ")1134 if i >= 0 {1135 name = name[:i]Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1136why, one step back1136 }Delete name field from Command (#547)Albert Nigmatzianovbefore adoption40f1880
line 1137why, one step back1137 return nameAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1138–1139why, one step back1138}1139Merge pull request #260 from ianwalter/code-cleanupEric Parisbefore adoptionb0d571e
line 1140why, one step back1140// HasAlias determines if a given string is an alias of the command.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1141–1149why, one step back1141func (c *Command) HasAlias(s string) bool {1142 for _, a := range c.Aliases {1143 if a == s {1144 return true1145 }1146 }1147 return false1148}1149Add `CalledAs` method to cobra.Command (w/ tests) (#567)Tim Peoplesbefore adoptioneb58983
lines 1150–1158why, one step back1150// CalledAs returns the command name or alias that was used to invoke1151// this command or an empty string if the command has not been called.1152func (c *Command) CalledAs() string {1153 if c.commandCalledAs.called {1154 return c.commandCalledAs.name1155 }1156 return ""1157}1158Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03
lines 1159–1162why, one step back1159// hasNameOrAliasPrefix returns true if the Name or any of aliases start1160// with prefix1161func (c *Command) hasNameOrAliasPrefix(prefix string) bool {1162 if strings.HasPrefix(c.Name(), prefix) {Add `CalledAs` method to cobra.Command (w/ tests) (#567)Tim Peoplesbefore adoptioneb58983
line 1163why, one step back1163 c.commandCalledAs.name = c.Name()Add a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03
lines 1164–1167why, one step back1164 return true1165 }1166 for _, alias := range c.Aliases {1167 if strings.HasPrefix(alias, prefix) {Add `CalledAs` method to cobra.Command (w/ tests) (#567)Tim Peoplesbefore adoptioneb58983
line 1168why, one step back1168 c.commandCalledAs.name = aliasAdd a TraverseChildren option to allow for flags on each command in the hierarchy (#299)Daniel Nephinbefore adoption83b1f03
lines 1169–1175why, one step back1169 return true1170 }1171 }1172 return false1173}11741175// NameAndAliases returns a list of the command name and all aliasesAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1176–1179why, one step back1176func (c *Command) NameAndAliases() string {1177 return strings.Join(append([]string{c.Name()}, c.Aliases...), ", ")1178}1179Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1180why, one step back1180// HasExample determines if the command has example.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1181–1184why, one step back1181func (c *Command) HasExample() bool {1182 return len(c.Example) > 01183}1184Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1185why, one step back1185// Runnable determines if the command is itself runnable.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1186–1189why, one step back1186func (c *Command) Runnable() bool {1187 return c.Run != nil || c.RunE != nil1188}1189Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1190why, one step back1190// HasSubCommands determines if the command has children commands.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1191–1195why, one step back1191func (c *Command) HasSubCommands() bool {1192 return len(c.commands) > 01193}11941195// IsAvailableCommand determines if a command is available as a non-help commandMerge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1196why, one step back1196// (this includes all non deprecated/hidden commands).Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1197–1212why, one step back1197func (c *Command) IsAvailableCommand() bool {1198 if len(c.Deprecated) != 0 || c.Hidden {1199 return false1200 }12011202 if c.HasParent() && c.Parent().helpCommand == c {1203 return false1204 }12051206 if c.Runnable() || c.HasAvailableSubCommands() {1207 return true1208 }12091210 return false1211}1212Rename IsHelpCommand to IsAdditionalHelpTopicCommand (#398)Albert Nigmatzianovbefore adoption0960ff7
lines 1213–1218why, one step back1213// IsAdditionalHelpTopicCommand determines if a command is an additional1214// help topic command; additional help topic command is determined by the1215// fact that it is NOT runnable/hidden/deprecated, and has no sub commands that1216// are runnable/hidden/deprecated.1217// Concrete example: https://github.com/spf13/cobra/issues/393#issuecomment-282741924.1218func (c *Command) IsAdditionalHelpTopicCommand() bool {Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1219–1225why, one step back1219 // if a command is runnable, deprecated, or hidden it is not a 'help' command1220 if c.Runnable() || len(c.Deprecated) != 0 || c.Hidden {1221 return false1222 }12231224 // if any non-help sub commands are found, the command is not a 'help' command1225 for _, sub := range c.commands {Rename IsHelpCommand to IsAdditionalHelpTopicCommand (#398)Albert Nigmatzianovbefore adoption0960ff7
line 1226why, one step back1226 if !sub.IsAdditionalHelpTopicCommand() {Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1227–1234why, one step back1227 return false1228 }1229 }12301231 // the command either has no sub commands, or no non-help sub commands1232 return true1233}1234Merge pull request #327 from bogem/fixesEric Parisbefore adoption1f47171
line 1235why, one step back1235// HasHelpSubCommands determines if a command has any available 'help' sub commandsAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1236why, one step back1236// that need to be shown in the usage/help default template under 'additional helpMerge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1237why, one step back1237// topics'.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1238–1240why, one step back1238func (c *Command) HasHelpSubCommands() bool {1239 // return true on the first found available 'help' sub command1240 for _, sub := range c.commands {Rename IsHelpCommand to IsAdditionalHelpTopicCommand (#398)Albert Nigmatzianovbefore adoption0960ff7
line 1241why, one step back1241 if sub.IsAdditionalHelpTopicCommand() {Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1242–1250why, one step back1242 return true1243 }1244 }12451246 // the command either has no sub commands, or no available 'help' sub commands1247 return false1248}12491250// HasAvailableSubCommands determines if a command has available sub commands thatMerge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1251why, one step back1251// need to be shown in the usage/help default template under 'available commands'.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1252–1260why, one step back1252func (c *Command) HasAvailableSubCommands() bool {1253 // return true on the first found available (non deprecated/help/hidden)1254 // sub command1255 for _, sub := range c.commands {1256 if sub.IsAvailableCommand() {1257 return true1258 }1259 }1260Fix typo (#573)Pavol Loffaybefore adoptionf634327
line 1261why, one step back1261 // the command either has no sub commands, or no available (non deprecated/help/hidden)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1262–1265why, one step back1262 // sub commands1263 return false1264}1265Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1266why, one step back1266// HasParent determines if the command is a child command.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1267–1270why, one step back1267func (c *Command) HasParent() bool {1268 return c.parent != nil1269}1270fix grammatical error in command.go (#626)Shamus Taylorbefore adoption3a7eb14
line 1271why, one step back1271// GlobalNormalizationFunc returns the global normalization function or nil if it doesn't exist.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1272–1275why, one step back1272func (c *Command) GlobalNormalizationFunc() func(f *flag.FlagSet, name string) flag.NormalizedName {1273 return c.globNormFunc1274}1275Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1276why, one step back1276// Flags returns the complete FlagSet that appliesMerge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1277why, one step back1277// to this command (local and persistent declared here and by all parents).Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1278–1280why, one step back1278func (c *Command) Flags() *flag.FlagSet {1279 if c.flags == nil {1280 c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)Revert "Get rid of flagErrorBuf (#417)"Albert Nigmatzianovbefore adoption3d7bff8
lines 1281–1284why, one step back1281 if c.flagErrorBuf == nil {1282 c.flagErrorBuf = new(bytes.Buffer)1283 }1284 c.flags.SetOutput(c.flagErrorBuf)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1285why, one step back1285 }Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
line 1286why, one step back1286Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1287–1289why, one step back1287 return c.flags1288}1289Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1290why, one step back1290// LocalNonPersistentFlags are flags specific to this command which will NOT persist to subcommands.Merge pull request #288 from eparis/flags-vs-commandsEric Parisbefore adoptionc21ee9d
lines 1291–1302why, one step back1291func (c *Command) LocalNonPersistentFlags() *flag.FlagSet {1292 persistentFlags := c.PersistentFlags()12931294 out := flag.NewFlagSet(c.Name(), flag.ContinueOnError)1295 c.LocalFlags().VisitAll(func(f *flag.Flag) {1296 if persistentFlags.Lookup(f.Name) == nil {1297 out.AddFlag(f)1298 }1299 })1300 return out1301}1302Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1303why, one step back1303// LocalFlags returns the local FlagSet specifically set in the current command.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1304–1306why, one step back1304func (c *Command) LocalFlags() *flag.FlagSet {1305 c.mergePersistentFlags()1306Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
lines 1307–1308why, one step back1307 if c.lflags == nil {1308 c.lflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)Revert "Get rid of flagErrorBuf (#417)"Albert Nigmatzianovbefore adoption3d7bff8
lines 1309–1312why, one step back1309 if c.flagErrorBuf == nil {1310 c.flagErrorBuf = new(bytes.Buffer)1311 }1312 c.lflags.SetOutput(c.flagErrorBuf)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1313why, one step back1313 }Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867
line 1314why, one step back1314 c.lflags.SortFlags = c.Flags().SortFlagsImprove consistency of flags when using SetGlobalNormalizationFunc (#522)Diego Becciolinibefore adoption0dacccf
lines 1315–1317why, one step back1315 if c.globNormFunc != nil {1316 c.lflags.SetNormalizeFunc(c.globNormFunc)1317 }Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
lines 1318–1323why, one step back13181319 addToLocal := func(f *flag.Flag) {1320 if c.lflags.Lookup(f.Name) == nil && c.parentsPflags.Lookup(f.Name) == nil {1321 c.lflags.AddFlag(f)1322 }1323 }Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867
line 1324why, one step back1324 c.Flags().VisitAll(addToLocal)Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
lines 1325–1326why, one step back1325 c.PersistentFlags().VisitAll(addToLocal)1326 return c.lflagsAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1327–1328why, one step back1327}1328Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1329why, one step back1329// InheritedFlags returns all flags which were inherited from parents commands.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1330–1332why, one step back1330func (c *Command) InheritedFlags() *flag.FlagSet {1331 c.mergePersistentFlags()1332Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
lines 1333–1334why, one step back1333 if c.iflags == nil {1334 c.iflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)Revert "Get rid of flagErrorBuf (#417)"Albert Nigmatzianovbefore adoption3d7bff8
lines 1335–1338why, one step back1335 if c.flagErrorBuf == nil {1336 c.flagErrorBuf = new(bytes.Buffer)1337 }1338 c.iflags.SetOutput(c.flagErrorBuf)Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
lines 1339–1340why, one step back1339 }1340Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1341why, one step back1341 local := c.LocalFlags()Improve consistency of flags when using SetGlobalNormalizationFunc (#522)Diego Becciolinibefore adoption0dacccf
lines 1342–1345why, one step back1342 if c.globNormFunc != nil {1343 c.iflags.SetNormalizeFunc(c.globNormFunc)1344 }1345Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
lines 1346–1348why, one step back1346 c.parentsPflags.VisitAll(func(f *flag.Flag) {1347 if c.iflags.Lookup(f.Name) == nil && local.Lookup(f.Name) == nil {1348 c.iflags.AddFlag(f)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1349why, one step back1349 }Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
lines 1350–1351why, one step back1350 })1351 return c.iflagsAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1352–1353why, one step back1352}1353Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1354why, one step back1354// NonInheritedFlags returns all flags which were not inherited from parent commands.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1355–1358why, one step back1355func (c *Command) NonInheritedFlags() *flag.FlagSet {1356 return c.LocalFlags()1357}1358Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1359why, one step back1359// PersistentFlags returns the persistent FlagSet specifically set in the current command.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1360–1362why, one step back1360func (c *Command) PersistentFlags() *flag.FlagSet {1361 if c.pflags == nil {1362 c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)Revert "Get rid of flagErrorBuf (#417)"Albert Nigmatzianovbefore adoption3d7bff8
lines 1363–1366why, one step back1363 if c.flagErrorBuf == nil {1364 c.flagErrorBuf = new(bytes.Buffer)1365 }1366 c.pflags.SetOutput(c.flagErrorBuf)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1367–1370why, one step back1367 }1368 return c.pflags1369}1370Edit ResetFlags and ResetCommands descriptionsAlbert Nigmatzianovbefore adoptiond6a4305
line 1371why, one step back1371// ResetFlags deletes all flags from command.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1372why, one step back1372func (c *Command) ResetFlags() {Revert "Get rid of flagErrorBuf (#417)"Albert Nigmatzianovbefore adoption3d7bff8
lines 1373–1374why, one step back1373 c.flagErrorBuf = new(bytes.Buffer)1374 c.flagErrorBuf.Reset()Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1375why, one step back1375 c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)Revert "Get rid of flagErrorBuf (#417)"Albert Nigmatzianovbefore adoption3d7bff8
line 1376why, one step back1376 c.flags.SetOutput(c.flagErrorBuf)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1377why, one step back1377 c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)Revert "Get rid of flagErrorBuf (#417)"Albert Nigmatzianovbefore adoption3d7bff8
line 1378why, one step back1378 c.pflags.SetOutput(c.flagErrorBuf)Improve consistency of flags when using SetGlobalNormalizationFunc (#522)Diego Becciolinibefore adoption0dacccf
lines 1379–1382why, one step back13791380 c.lflags = nil1381 c.iflags = nil1382 c.parentsPflags = nilAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1383–1384why, one step back1383}1384Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1385why, one step back1385// HasFlags checks if the command contains any flags (local plus persistent from the entire structure).Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1386–1389why, one step back1386func (c *Command) HasFlags() bool {1387 return c.Flags().HasFlags()1388}1389Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1390why, one step back1390// HasPersistentFlags checks if the command contains persistent flags.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1391–1394why, one step back1391func (c *Command) HasPersistentFlags() bool {1392 return c.PersistentFlags().HasFlags()1393}1394Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1395why, one step back1395// HasLocalFlags checks if the command has flags specifically declared locally.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1396–1399why, one step back1396func (c *Command) HasLocalFlags() bool {1397 return c.LocalFlags().HasFlags()1398}1399Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1400why, one step back1400// HasInheritedFlags checks if the command has flags inherited from its parent command.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1401–1402why, one step back1401func (c *Command) HasInheritedFlags() bool {1402 return c.InheritedFlags().HasFlags()Issue #267: Ensure hidden flags/subcommands do not change helpJohn Schnakebefore adoptione14e47b
lines 1403–1404why, one step back1403}1404Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1405why, one step back1405// HasAvailableFlags checks if the command contains any flags (local plus persistent from the entireMerge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1406why, one step back1406// structure) which are not hidden or deprecated.Issue #267: Ensure hidden flags/subcommands do not change helpJohn Schnakebefore adoptione14e47b
lines 1407–1410why, one step back1407func (c *Command) HasAvailableFlags() bool {1408 return c.Flags().HasAvailableFlags()1409}1410Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1411why, one step back1411// HasAvailablePersistentFlags checks if the command contains persistent flags which are not hidden or deprecated.Issue #267: Ensure hidden flags/subcommands do not change helpJohn Schnakebefore adoptione14e47b
lines 1412–1415why, one step back1412func (c *Command) HasAvailablePersistentFlags() bool {1413 return c.PersistentFlags().HasAvailableFlags()1414}1415Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1416why, one step back1416// HasAvailableLocalFlags checks if the command has flags specifically declared locally which are not hiddenMerge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1417why, one step back1417// or deprecated.Issue #267: Ensure hidden flags/subcommands do not change helpJohn Schnakebefore adoptione14e47b
lines 1418–1421why, one step back1418func (c *Command) HasAvailableLocalFlags() bool {1419 return c.LocalFlags().HasAvailableFlags()1420}1421Added godocs to public methods. (#386)Oleg Atamanenkobefore adoption0f056af
line 1422why, one step back1422// HasAvailableInheritedFlags checks if the command has flags inherited from its parent command which areMerge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1423why, one step back1423// not hidden or deprecated.Issue #267: Ensure hidden flags/subcommands do not change helpJohn Schnakebefore adoptione14e47b
lines 1424–1425why, one step back1424func (c *Command) HasAvailableInheritedFlags() bool {1425 return c.InheritedFlags().HasAvailableFlags()Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1426–1427why, one step back1426}1427Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1428why, one step back1428// Flag climbs up the command tree looking for matching flag.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1429–1438why, one step back1429func (c *Command) Flag(name string) (flag *flag.Flag) {1430 flag = c.Flags().Lookup(name)14311432 if flag == nil {1433 flag = c.persistentFlag(name)1434 }14351436 return1437}1438Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1439why, one step back1439// Recursively find matching persistent flag.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1440–1444why, one step back1440func (c *Command) persistentFlag(name string) (flag *flag.Flag) {1441 if c.HasPersistentFlags() {1442 flag = c.PersistentFlags().Lookup(name)1443 }1444Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867
lines 1445–1447why, one step back1445 if flag == nil {1446 c.updateParentsPflags()1447 flag = c.parentsPflags.Lookup(name)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1448–1451why, one step back1448 }1449 return1450}1451Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1452why, one step back1452// ParseFlags parses persistent flag tree and local flags.Show messages if deprecated flags are usedAlbert Nigmatzianovbefore adoption99b5d83
line 1453why, one step back1453func (c *Command) ParseFlags(args []string) error {Merge pull request #289 from markbates/disable-flagsEric Parisbefore adoptionf447048
lines 1454–1456why, one step back1454 if c.DisableFlagParsing {1455 return nil1456 }Show messages if deprecated flags are usedAlbert Nigmatzianovbefore adoption99b5d83
line 1457why, one step back1457Create new buffer if not present yet (#549)Thomas Cyronbefore adoption7b2c5ac
lines 1458–1460why, one step back1458 if c.flagErrorBuf == nil {1459 c.flagErrorBuf = new(bytes.Buffer)1460 }Show messages if deprecated flags are usedAlbert Nigmatzianovbefore adoption99b5d83
line 1461why, one step back1461 beforeErrorBufLen := c.flagErrorBuf.Len()Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1462why, one step back1462 c.mergePersistentFlags()Show messages if deprecated flags are usedAlbert Nigmatzianovbefore adoption99b5d83
lines 1463–1469why, one step back1463 err := c.Flags().Parse(args)1464 // Print warnings if they occurred (e.g. deprecated flag messages).1465 if c.flagErrorBuf.Len()-beforeErrorBufLen > 0 && err == nil {1466 c.Print(c.flagErrorBuf.String())1467 }14681469 return errAllow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1470–1471why, one step back1470}1471Merge pull request #339 from bogem/fixesEric Parisbefore adoption9c28e4b
line 1472why, one step back1472// Parent returns a commands parent command.Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
lines 1473–1476why, one step back1473func (c *Command) Parent() *Command {1474 return c.parent1475}1476Add only missing persistent flags of parents in mergePersistentFlagsAlbert Nigmatzianovbefore adoption3d89ed4
lines 1477–1479why, one step back1477// mergePersistentFlags merges c.PersistentFlags() to c.Flags()1478// and adds missing persistent flags of all parents.1479func (c *Command) mergePersistentFlags() {Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867
line 1480why, one step back1480 c.updateParentsPflags()Fix incorrect merge of pflag.CommandLine to cmd.Flags()Albert Nigmatzianovbefore adoption7d22e77
line 1481why, one step back1481 c.Flags().AddFlagSet(c.PersistentFlags())Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867
line 1482why, one step back1482 c.Flags().AddFlagSet(c.parentsPflags)Add only missing persistent flags of parents in mergePersistentFlagsAlbert Nigmatzianovbefore adoption3d89ed4
lines 1483–1485why, one step back1483}14841485// updateParentsPflags updates c.parentsPflags by addingMake parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867
line 1486why, one step back1486// new persistent flags of all parents.Add only missing persistent flags of parents in mergePersistentFlagsAlbert Nigmatzianovbefore adoption3d89ed4
line 1487why, one step back1487// If c.parentsPflags == nil, it makes new.Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867
line 1488why, one step back1488func (c *Command) updateParentsPflags() {Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
lines 1489–1490why, one step back1489 if c.parentsPflags == nil {1490 c.parentsPflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)Revert "Get rid of flagErrorBuf (#417)"Albert Nigmatzianovbefore adoption3d7bff8
line 1491why, one step back1491 c.parentsPflags.SetOutput(c.flagErrorBuf)Add only missing persistent flags of parents in mergePersistentFlagsAlbert Nigmatzianovbefore adoption3d89ed4
line 1492why, one step back1492 c.parentsPflags.SortFlags = falseImprove consistency of flags when using SetGlobalNormalizationFunc (#522)Diego Becciolinibefore adoption0dacccf
lines 1493–1496why, one step back1493 }14941495 if c.globNormFunc != nil {1496 c.parentsPflags.SetNormalizeFunc(c.globNormFunc)Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1497why, one step back1497 }Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
line 1498why, one step back1498Add merging of CommandLine flags to Root commandAlbert Nigmatzianovbefore adoption458d797
lines 1499–1500why, one step back1499 c.Root().PersistentFlags().AddFlagSet(flag.CommandLine)1500Make parentsPflags more flexibleAlbert Nigmatzianovbefore adoptione135867
lines 1501–1502why, one step back1501 c.VisitParents(func(parent *Command) {1502 c.parentsPflags.AddFlagSet(parent.PersistentFlags())Refactor flags mechanismsAlbert Nigmatzianovbefore adoption6202b59
line 1503why, one step back1503 })Allow bash-completion to work with full-path executableAnthony Fokbefore adoption65a708c
line 1504why, one step back1504}