2013-09-24 20:47:29 +04:00
|
|
|
// Copyright © 2013 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2014-12-13 08:05:43 +03:00
|
|
|
//Package cobra is a commander providing a simple interface to create powerful modern CLI interfaces.
|
|
|
|
//In addition to providing an interface, Cobra simultaneously provides a controller to organize your application code.
|
2013-09-24 20:47:29 +04:00
|
|
|
package cobra
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2013-11-02 02:36:24 +04:00
|
|
|
"os"
|
2015-11-08 09:21:50 +03:00
|
|
|
"path/filepath"
|
2016-06-14 17:04:53 +03:00
|
|
|
"sort"
|
2013-09-24 20:47:29 +04:00
|
|
|
"strings"
|
2015-05-13 04:38:01 +03:00
|
|
|
|
|
|
|
flag "github.com/spf13/pflag"
|
2013-09-24 20:47:29 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Command is just that, a command for your application.
|
|
|
|
// eg. 'go run' ... 'run' is the command. Cobra requires
|
|
|
|
// you to define the usage and description as part of your command
|
|
|
|
// definition to ensure usability.
|
|
|
|
type Command struct {
|
|
|
|
// Name is the command name, usually the executable's name.
|
|
|
|
name string
|
|
|
|
// The one-line usage message.
|
|
|
|
Use string
|
2014-09-26 03:03:34 +04:00
|
|
|
// An array of aliases that can be used instead of the first word in Use.
|
|
|
|
Aliases []string
|
2015-09-25 18:04:28 +03:00
|
|
|
// An array of command names for which this command will be suggested - similar to aliases but only suggests.
|
|
|
|
SuggestFor []string
|
2013-09-24 20:47:29 +04:00
|
|
|
// The short description shown in the 'help' output.
|
|
|
|
Short string
|
|
|
|
// The long message shown in the 'help <this-command>' output.
|
|
|
|
Long string
|
2015-02-13 02:12:07 +03:00
|
|
|
// Examples of how to use the command
|
|
|
|
Example string
|
2016-03-25 18:05:56 +03:00
|
|
|
// List of all valid non-flag arguments that are accepted in bash completions
|
2015-03-16 22:31:03 +03:00
|
|
|
ValidArgs []string
|
2016-03-25 18:05:56 +03:00
|
|
|
// List of aliases for ValidArgs. These are not suggested to the user in the bash
|
|
|
|
// completion, but accepted if entered manually.
|
|
|
|
ArgAliases []string
|
2015-03-16 22:31:03 +03:00
|
|
|
// Custom functions used by the bash autocompletion generator
|
|
|
|
BashCompletionFunction string
|
2015-04-08 00:38:22 +03:00
|
|
|
// Is this command deprecated and should print this string when used?
|
|
|
|
Deprecated string
|
2015-09-04 23:34:51 +03:00
|
|
|
// Is this command hidden and should NOT show up in the list of available commands?
|
|
|
|
Hidden bool
|
2017-02-08 00:20:48 +03:00
|
|
|
// Annotations are key/value pairs that can be used by applications to identify or
|
|
|
|
// group commands
|
|
|
|
Annotations map[string]string
|
2015-01-29 23:55:29 +03:00
|
|
|
// Full set of flags
|
2013-09-24 20:47:29 +04:00
|
|
|
flags *flag.FlagSet
|
2015-01-29 23:55:29 +03:00
|
|
|
// Set of flags childrens of this command will inherit
|
2013-09-24 20:47:29 +04:00
|
|
|
pflags *flag.FlagSet
|
2015-02-28 03:26:56 +03:00
|
|
|
// Flags that are declared specifically by this command (not inherited).
|
|
|
|
lflags *flag.FlagSet
|
2017-04-05 19:44:50 +03:00
|
|
|
// Inherited flags.
|
|
|
|
iflags *flag.FlagSet
|
|
|
|
// All persistent flags of cmd's parents.
|
|
|
|
parentsPflags *flag.FlagSet
|
2015-10-26 04:58:53 +03:00
|
|
|
// SilenceErrors is an option to quiet errors down stream
|
|
|
|
SilenceErrors bool
|
2015-10-27 02:38:08 +03:00
|
|
|
// Silence Usage is an option to silence usage when an error occurs.
|
|
|
|
SilenceUsage bool
|
2015-04-30 20:58:29 +03:00
|
|
|
// The *Run functions are executed in the following order:
|
|
|
|
// * PersistentPreRun()
|
|
|
|
// * PreRun()
|
|
|
|
// * Run()
|
|
|
|
// * PostRun()
|
|
|
|
// * PersistentPostRun()
|
|
|
|
// All functions get the same args, the arguments after the command name
|
|
|
|
// PersistentPreRun: children of this command will inherit and execute
|
|
|
|
PersistentPreRun func(cmd *Command, args []string)
|
2015-08-31 04:03:16 +03:00
|
|
|
// PersistentPreRunE: PersistentPreRun but returns an error
|
|
|
|
PersistentPreRunE func(cmd *Command, args []string) error
|
2015-04-30 20:58:29 +03:00
|
|
|
// PreRun: children of this command will not inherit.
|
2015-02-10 01:44:14 +03:00
|
|
|
PreRun func(cmd *Command, args []string)
|
2015-08-31 04:03:16 +03:00
|
|
|
// PreRunE: PreRun but returns an error
|
|
|
|
PreRunE func(cmd *Command, args []string) error
|
2015-04-30 20:58:29 +03:00
|
|
|
// Run: Typically the actual work function. Most commands will only implement this
|
|
|
|
Run func(cmd *Command, args []string)
|
2015-08-31 04:03:16 +03:00
|
|
|
// RunE: Run but returns an error
|
|
|
|
RunE func(cmd *Command, args []string) error
|
2015-04-30 20:58:29 +03:00
|
|
|
// PostRun: run after the Run command.
|
2015-02-10 01:44:14 +03:00
|
|
|
PostRun func(cmd *Command, args []string)
|
2015-08-31 04:03:16 +03:00
|
|
|
// PostRunE: PostRun but returns an error
|
|
|
|
PostRunE func(cmd *Command, args []string) error
|
2015-04-30 20:58:29 +03:00
|
|
|
// PersistentPostRun: children of this command will inherit and execute after PostRun
|
2015-02-18 00:09:49 +03:00
|
|
|
PersistentPostRun func(cmd *Command, args []string)
|
2015-08-31 04:03:16 +03:00
|
|
|
// PersistentPostRunE: PersistentPostRun but returns an error
|
|
|
|
PersistentPostRunE func(cmd *Command, args []string) error
|
2015-11-08 04:21:25 +03:00
|
|
|
// DisableAutoGenTag remove
|
|
|
|
DisableAutoGenTag bool
|
2013-11-02 02:36:24 +04:00
|
|
|
// Commands is the list of commands supported by this program.
|
2013-09-24 20:47:29 +04:00
|
|
|
commands []*Command
|
|
|
|
// Parent Command for this command
|
2013-11-02 02:36:24 +04:00
|
|
|
parent *Command
|
2014-01-17 16:36:57 +04:00
|
|
|
// max lengths of commands' string lengths for use in padding
|
|
|
|
commandsMaxUseLen int
|
|
|
|
commandsMaxCommandPathLen int
|
2015-02-11 12:20:29 +03:00
|
|
|
commandsMaxNameLen int
|
2016-06-14 17:04:53 +03:00
|
|
|
// is commands slice are sorted or not
|
|
|
|
commandsAreSorted bool
|
2013-11-02 02:36:24 +04:00
|
|
|
|
2016-05-26 00:27:02 +03:00
|
|
|
args []string // actual args parsed from flags
|
2017-04-02 17:14:34 +03:00
|
|
|
output io.Writer // out writer if set in SetOutput(w)
|
2016-05-26 00:27:02 +03:00
|
|
|
usageFunc func(*Command) error // Usage can be defined by application
|
|
|
|
usageTemplate string // Can be defined by Application
|
|
|
|
flagErrorFunc func(*Command, error) error
|
2013-11-02 02:36:24 +04:00
|
|
|
helpTemplate string // Can be defined by Application
|
|
|
|
helpFunc func(*Command, []string) // Help can be defined by application
|
|
|
|
helpCommand *Command // The help command
|
2015-05-14 20:38:15 +03:00
|
|
|
// The global normalization function that we can use on every pFlag set and children commands
|
|
|
|
globNormFunc func(f *flag.FlagSet, name string) flag.NormalizedName
|
2015-09-11 23:04:58 +03:00
|
|
|
|
|
|
|
// Disable the suggestions based on Levenshtein distance that go along with 'unknown command' messages
|
|
|
|
DisableSuggestions bool
|
|
|
|
// If displaying suggestions, allows to set the minimum levenshtein distance to display, must be > 0
|
|
|
|
SuggestionsMinimumDistance int
|
2016-06-04 03:25:52 +03:00
|
|
|
|
|
|
|
// Disable the flag parsing. If this is true all flags will be passed to the command as arguments.
|
|
|
|
DisableFlagParsing bool
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden
|
2013-11-02 02:36:24 +04:00
|
|
|
// particularly useful when testing.
|
|
|
|
func (c *Command) SetArgs(a []string) {
|
|
|
|
c.args = a
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOutput sets the destination for usage and error messages.
|
|
|
|
// If output is nil, os.Stderr is used.
|
|
|
|
func (c *Command) SetOutput(output io.Writer) {
|
2017-04-02 17:14:34 +03:00
|
|
|
c.output = output
|
2013-11-02 02:36:24 +04:00
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// SetUsageFunc sets usage function. Usage can be defined by application.
|
2013-11-02 02:36:24 +04:00
|
|
|
func (c *Command) SetUsageFunc(f func(*Command) error) {
|
|
|
|
c.usageFunc = f
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// SetUsageTemplate sets usage template. Can be defined by Application.
|
2013-11-02 02:36:24 +04:00
|
|
|
func (c *Command) SetUsageTemplate(s string) {
|
|
|
|
c.usageTemplate = s
|
|
|
|
}
|
|
|
|
|
2016-05-26 00:27:02 +03:00
|
|
|
// SetFlagErrorFunc sets a function to generate an error when flag parsing
|
2017-02-28 22:17:48 +03:00
|
|
|
// fails.
|
2016-05-26 00:27:02 +03:00
|
|
|
func (c *Command) SetFlagErrorFunc(f func(*Command, error) error) {
|
|
|
|
c.flagErrorFunc = f
|
|
|
|
}
|
|
|
|
|
2017-02-28 22:17:48 +03:00
|
|
|
// SetHelpFunc sets help function. Can be defined by Application.
|
2013-11-02 02:36:24 +04:00
|
|
|
func (c *Command) SetHelpFunc(f func(*Command, []string)) {
|
|
|
|
c.helpFunc = f
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// SetHelpCommand sets help command.
|
2013-11-02 02:36:24 +04:00
|
|
|
func (c *Command) SetHelpCommand(cmd *Command) {
|
|
|
|
c.helpCommand = cmd
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// SetHelpTemplate sets help template to be used. Application can use it to set custom template.
|
2013-11-02 02:36:24 +04:00
|
|
|
func (c *Command) SetHelpTemplate(s string) {
|
|
|
|
c.helpTemplate = s
|
|
|
|
}
|
|
|
|
|
2015-05-14 20:38:15 +03:00
|
|
|
// SetGlobalNormalizationFunc sets a normalization function to all flag sets and also to child commands.
|
|
|
|
// The user should not have a cyclic dependency on commands.
|
|
|
|
func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string) flag.NormalizedName) {
|
|
|
|
c.Flags().SetNormalizeFunc(n)
|
|
|
|
c.PersistentFlags().SetNormalizeFunc(n)
|
|
|
|
c.globNormFunc = n
|
|
|
|
|
|
|
|
for _, command := range c.commands {
|
|
|
|
command.SetGlobalNormalizationFunc(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-28 22:17:48 +03:00
|
|
|
// OutOrStdout returns output to stdout.
|
2016-07-15 23:12:07 +03:00
|
|
|
func (c *Command) OutOrStdout() io.Writer {
|
2016-07-06 18:51:57 +03:00
|
|
|
return c.getOut(os.Stdout)
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// OutOrStderr returns output to stderr
|
2016-07-15 23:12:07 +03:00
|
|
|
func (c *Command) OutOrStderr() io.Writer {
|
2016-07-06 18:51:57 +03:00
|
|
|
return c.getOut(os.Stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) getOut(def io.Writer) io.Writer {
|
|
|
|
if c.output != nil {
|
2017-04-02 17:14:34 +03:00
|
|
|
return c.output
|
2016-07-06 18:51:57 +03:00
|
|
|
}
|
|
|
|
if c.HasParent() {
|
|
|
|
return c.parent.getOut(def)
|
|
|
|
}
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
|
|
|
|
// UsageFunc returns either the function set by SetUsageFunc for this command
|
2016-08-30 20:14:27 +03:00
|
|
|
// or a parent, or it returns a default usage function.
|
2013-11-02 02:36:24 +04:00
|
|
|
func (c *Command) UsageFunc() (f func(*Command) error) {
|
|
|
|
if c.usageFunc != nil {
|
|
|
|
return c.usageFunc
|
|
|
|
}
|
|
|
|
if c.HasParent() {
|
2017-04-23 10:17:44 +03:00
|
|
|
return c.Parent().UsageFunc()
|
2016-03-31 16:53:34 +03:00
|
|
|
}
|
|
|
|
return func(c *Command) error {
|
2016-07-15 23:17:29 +03:00
|
|
|
c.mergePersistentFlags()
|
|
|
|
err := tmpl(c.OutOrStderr(), c.UsageTemplate(), c)
|
2016-03-31 16:53:34 +03:00
|
|
|
if err != nil {
|
2016-07-06 18:51:57 +03:00
|
|
|
c.Println(err)
|
2013-11-02 02:36:24 +04:00
|
|
|
}
|
2016-03-31 16:53:34 +03:00
|
|
|
return err
|
2013-11-02 02:36:24 +04:00
|
|
|
}
|
|
|
|
}
|
2015-09-01 20:58:55 +03:00
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// Usage puts out the usage for the command.
|
|
|
|
// Used when a user provides invalid input.
|
|
|
|
// Can be defined by user by overriding UsageFunc.
|
2016-07-20 17:39:46 +03:00
|
|
|
func (c *Command) Usage() error {
|
|
|
|
return c.UsageFunc()(c)
|
|
|
|
}
|
|
|
|
|
2015-09-01 20:58:55 +03:00
|
|
|
// HelpFunc returns either the function set by SetHelpFunc for this command
|
2016-08-30 20:14:27 +03:00
|
|
|
// or a parent, or it returns a function with default help behavior.
|
2013-11-02 02:36:24 +04:00
|
|
|
func (c *Command) HelpFunc() func(*Command, []string) {
|
2017-04-23 10:17:44 +03:00
|
|
|
if c.helpFunc != nil {
|
|
|
|
return c.helpFunc
|
2013-11-02 02:36:24 +04:00
|
|
|
}
|
2017-04-23 10:17:44 +03:00
|
|
|
if c.HasParent() {
|
|
|
|
return c.Parent().HelpFunc()
|
|
|
|
}
|
|
|
|
return func(c *Command, a []string) {
|
2016-07-15 23:17:29 +03:00
|
|
|
c.mergePersistentFlags()
|
|
|
|
err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c)
|
2015-09-01 20:58:55 +03:00
|
|
|
if err != nil {
|
|
|
|
c.Println(err)
|
2013-11-02 02:36:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// Help puts out the help for the command.
|
|
|
|
// Used when a user calls help [command].
|
|
|
|
// Can be defined by user by overriding HelpFunc.
|
2016-07-20 17:39:46 +03:00
|
|
|
func (c *Command) Help() error {
|
|
|
|
c.HelpFunc()(c, []string{})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// UsageString return usage string.
|
2016-07-06 18:51:57 +03:00
|
|
|
func (c *Command) UsageString() string {
|
|
|
|
tmpOutput := c.output
|
|
|
|
bb := new(bytes.Buffer)
|
|
|
|
c.SetOutput(bb)
|
2016-07-20 17:39:46 +03:00
|
|
|
c.Usage()
|
2016-07-06 18:51:57 +03:00
|
|
|
c.output = tmpOutput
|
|
|
|
return bb.String()
|
|
|
|
}
|
|
|
|
|
2016-05-26 00:27:02 +03:00
|
|
|
// FlagErrorFunc returns either the function set by SetFlagErrorFunc for this
|
|
|
|
// command or a parent, or it returns a function which returns the original
|
|
|
|
// error.
|
|
|
|
func (c *Command) FlagErrorFunc() (f func(*Command, error) error) {
|
|
|
|
if c.flagErrorFunc != nil {
|
|
|
|
return c.flagErrorFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HasParent() {
|
|
|
|
return c.parent.FlagErrorFunc()
|
|
|
|
}
|
|
|
|
return func(c *Command, err error) error {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 16:53:34 +03:00
|
|
|
var minUsagePadding = 25
|
2014-01-17 16:36:57 +04:00
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// UsagePadding return padding for the usage.
|
2014-01-17 16:36:57 +04:00
|
|
|
func (c *Command) UsagePadding() int {
|
|
|
|
if c.parent == nil || minUsagePadding > c.parent.commandsMaxUseLen {
|
|
|
|
return minUsagePadding
|
|
|
|
}
|
2016-03-31 16:53:34 +03:00
|
|
|
return c.parent.commandsMaxUseLen
|
2014-01-17 16:36:57 +04:00
|
|
|
}
|
|
|
|
|
2016-03-31 16:53:34 +03:00
|
|
|
var minCommandPathPadding = 11
|
2014-01-17 16:36:57 +04:00
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// CommandPathPadding return padding for the command path.
|
2014-01-17 16:36:57 +04:00
|
|
|
func (c *Command) CommandPathPadding() int {
|
|
|
|
if c.parent == nil || minCommandPathPadding > c.parent.commandsMaxCommandPathLen {
|
|
|
|
return minCommandPathPadding
|
|
|
|
}
|
2016-03-31 16:53:34 +03:00
|
|
|
return c.parent.commandsMaxCommandPathLen
|
2014-01-17 16:36:57 +04:00
|
|
|
}
|
|
|
|
|
2016-03-31 16:53:34 +03:00
|
|
|
var minNamePadding = 11
|
2015-02-11 12:20:29 +03:00
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// NamePadding returns padding for the name.
|
2015-02-11 12:20:29 +03:00
|
|
|
func (c *Command) NamePadding() int {
|
|
|
|
if c.parent == nil || minNamePadding > c.parent.commandsMaxNameLen {
|
|
|
|
return minNamePadding
|
|
|
|
}
|
2016-03-31 16:53:34 +03:00
|
|
|
return c.parent.commandsMaxNameLen
|
2015-02-11 12:20:29 +03:00
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// UsageTemplate returns usage template for the command.
|
2013-11-02 02:36:24 +04:00
|
|
|
func (c *Command) UsageTemplate() string {
|
|
|
|
if c.usageTemplate != "" {
|
|
|
|
return c.usageTemplate
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HasParent() {
|
|
|
|
return c.parent.UsageTemplate()
|
2016-03-31 16:53:34 +03:00
|
|
|
}
|
|
|
|
return `Usage:{{if .Runnable}}
|
2016-04-13 21:27:05 +03:00
|
|
|
{{if .HasAvailableFlags}}{{appendIfNotPresent .UseLine "[flags]"}}{{else}}{{.UseLine}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
|
2014-09-26 03:03:34 +04:00
|
|
|
{{ .CommandPath}} [command]{{end}}{{if gt .Aliases 0}}
|
|
|
|
|
|
|
|
Aliases:
|
2017-04-17 19:55:55 +03:00
|
|
|
{{.NameAndAliases}}{{end}}{{if .HasExample}}
|
2015-03-14 22:44:58 +03:00
|
|
|
|
2015-02-13 02:12:07 +03:00
|
|
|
Examples:
|
2017-02-28 22:17:48 +03:00
|
|
|
{{ .Example }}{{end}}{{if .HasAvailableSubCommands}}
|
2015-06-30 03:06:04 +03:00
|
|
|
|
2017-02-28 22:17:48 +03:00
|
|
|
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
|
|
|
|
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
|
2015-06-30 03:06:04 +03:00
|
|
|
|
|
|
|
Flags:
|
2017-02-28 22:17:48 +03:00
|
|
|
{{.LocalFlags.FlagUsages | trimRightSpace}}{{end}}{{if .HasAvailableInheritedFlags}}
|
2015-06-30 03:06:04 +03:00
|
|
|
|
|
|
|
Global Flags:
|
2015-09-12 01:44:03 +03:00
|
|
|
{{.InheritedFlags.FlagUsages | trimRightSpace}}{{end}}{{if .HasHelpSubCommands}}
|
2015-06-30 03:06:04 +03:00
|
|
|
|
2017-03-09 18:37:15 +03:00
|
|
|
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
|
2017-02-28 22:17:48 +03:00
|
|
|
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
|
2015-06-30 03:06:04 +03:00
|
|
|
|
2015-09-12 07:21:31 +03:00
|
|
|
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
|
|
|
|
`
|
2013-11-02 02:36:24 +04:00
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// HelpTemplate return help template for the command.
|
2013-11-02 02:36:24 +04:00
|
|
|
func (c *Command) HelpTemplate() string {
|
|
|
|
if c.helpTemplate != "" {
|
|
|
|
return c.helpTemplate
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HasParent() {
|
|
|
|
return c.parent.HelpTemplate()
|
2016-03-31 16:53:34 +03:00
|
|
|
}
|
|
|
|
return `{{with or .Long .Short }}{{. | trim}}
|
2015-09-12 01:27:37 +03:00
|
|
|
|
|
|
|
{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
|
2013-09-29 09:57:50 +04:00
|
|
|
}
|
|
|
|
|
2017-02-23 16:12:51 +03:00
|
|
|
func hasNoOptDefVal(name string, f *flag.FlagSet) bool {
|
2015-03-11 21:52:35 +03:00
|
|
|
flag := f.Lookup(name)
|
|
|
|
if flag == nil {
|
|
|
|
return false
|
|
|
|
}
|
2017-04-24 16:20:01 +03:00
|
|
|
return flag.NoOptDefVal != ""
|
2015-03-11 21:52:35 +03:00
|
|
|
}
|
|
|
|
|
2017-02-23 16:12:51 +03:00
|
|
|
func shortHasNoOptDefVal(name string, fs *flag.FlagSet) bool {
|
2015-03-11 20:22:30 +03:00
|
|
|
result := false
|
2017-02-23 16:12:51 +03:00
|
|
|
fs.VisitAll(func(flag *flag.Flag) {
|
2017-04-24 16:20:01 +03:00
|
|
|
if flag.Shorthand == name && flag.NoOptDefVal != "" {
|
2015-03-11 20:22:30 +03:00
|
|
|
result = true
|
2017-04-24 16:20:01 +03:00
|
|
|
return
|
2015-03-11 20:22:30 +03:00
|
|
|
}
|
|
|
|
})
|
2015-03-11 21:52:35 +03:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func stripFlags(args []string, c *Command) []string {
|
2017-04-23 23:38:30 +03:00
|
|
|
if len(args) == 0 {
|
2014-06-17 20:32:27 +04:00
|
|
|
return args
|
|
|
|
}
|
2015-03-16 22:47:32 +03:00
|
|
|
c.mergePersistentFlags()
|
2014-06-17 20:32:27 +04:00
|
|
|
|
|
|
|
commands := []string{}
|
|
|
|
inQuote := false
|
2017-04-24 16:20:01 +03:00
|
|
|
flags := c.Flags()
|
2017-04-23 23:38:30 +03:00
|
|
|
|
|
|
|
Loop:
|
|
|
|
for len(args) > 0 {
|
|
|
|
s := args[0]
|
2017-04-24 16:20:01 +03:00
|
|
|
args = args[1:]
|
2014-06-17 20:32:27 +04:00
|
|
|
if !inQuote {
|
|
|
|
switch {
|
2017-04-23 23:38:30 +03:00
|
|
|
case strings.HasPrefix(s, "\"") || strings.Contains(s, "=\""):
|
2014-06-17 20:32:27 +04:00
|
|
|
inQuote = true
|
2017-04-24 16:20:01 +03:00
|
|
|
case strings.HasPrefix(s, "--") && !strings.Contains(s, "=") && !hasNoOptDefVal(s[2:], flags):
|
2017-04-23 23:38:30 +03:00
|
|
|
// If '--flag arg' then
|
2017-04-24 16:20:01 +03:00
|
|
|
// delete arg from args.
|
|
|
|
fallthrough // (do the same as below)
|
|
|
|
case strings.HasPrefix(s, "-") && !strings.Contains(s, "=") && len(s) == 2 && !shortHasNoOptDefVal(s[1:], flags):
|
2017-04-23 23:38:30 +03:00
|
|
|
// If '-f arg' then
|
2017-04-24 16:20:01 +03:00
|
|
|
// delete 'arg' from args or break the loop if len(args) <= 1.
|
|
|
|
if len(args) <= 1 {
|
2017-04-23 23:38:30 +03:00
|
|
|
break Loop
|
|
|
|
} else {
|
2017-04-24 16:20:01 +03:00
|
|
|
args = args[1:]
|
2017-04-23 23:38:30 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
case s != "" && !strings.HasPrefix(s, "-"):
|
|
|
|
commands = append(commands, s)
|
2014-06-17 20:32:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-23 23:38:30 +03:00
|
|
|
if strings.HasSuffix(s, "\"") && !strings.HasSuffix(s, "\\\"") {
|
2014-06-17 20:32:27 +04:00
|
|
|
inQuote = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return commands
|
|
|
|
}
|
|
|
|
|
2015-04-24 18:39:11 +03:00
|
|
|
// argsMinusFirstX removes only the first x from args. Otherwise, commands that look like
|
|
|
|
// openshift admin policy add-role-to-user admin my-user, lose the admin argument (arg[4]).
|
|
|
|
func argsMinusFirstX(args []string, x string) []string {
|
|
|
|
for i, y := range args {
|
|
|
|
if x == y {
|
|
|
|
ret := []string{}
|
|
|
|
ret = append(ret, args[:i]...)
|
|
|
|
ret = append(ret, args[i+1:]...)
|
|
|
|
return ret
|
2014-06-17 20:32:27 +04:00
|
|
|
}
|
|
|
|
}
|
2015-04-24 18:39:11 +03:00
|
|
|
return args
|
2014-06-17 20:32:27 +04:00
|
|
|
}
|
|
|
|
|
2017-02-08 00:20:48 +03:00
|
|
|
// Find the target command given the args and command tree
|
2013-09-25 00:03:22 +04:00
|
|
|
// Meant to be run on the highest node. Only searches down.
|
2015-05-13 04:38:01 +03:00
|
|
|
func (c *Command) Find(args []string) (*Command, []string, error) {
|
2013-09-24 20:47:29 +04:00
|
|
|
if c == nil {
|
|
|
|
return nil, nil, fmt.Errorf("Called find() on a nil Command")
|
|
|
|
}
|
|
|
|
|
2013-09-29 09:59:13 +04:00
|
|
|
var innerfind func(*Command, []string) (*Command, []string)
|
|
|
|
|
2015-05-13 04:38:01 +03:00
|
|
|
innerfind = func(c *Command, innerArgs []string) (*Command, []string) {
|
2015-06-09 20:52:43 +03:00
|
|
|
argsWOflags := stripFlags(innerArgs, c)
|
|
|
|
if len(argsWOflags) == 0 {
|
|
|
|
return c, innerArgs
|
|
|
|
}
|
|
|
|
nextSubCmd := argsWOflags[0]
|
|
|
|
matches := make([]*Command, 0)
|
|
|
|
for _, cmd := range c.commands {
|
|
|
|
if cmd.Name() == nextSubCmd || cmd.HasAlias(nextSubCmd) { // exact name or alias match
|
|
|
|
return innerfind(cmd, argsMinusFirstX(innerArgs, nextSubCmd))
|
|
|
|
}
|
|
|
|
if EnablePrefixMatching {
|
|
|
|
if strings.HasPrefix(cmd.Name(), nextSubCmd) { // prefix match
|
|
|
|
matches = append(matches, cmd)
|
2013-09-29 09:59:13 +04:00
|
|
|
}
|
2015-06-09 20:52:43 +03:00
|
|
|
for _, x := range cmd.Aliases {
|
|
|
|
if strings.HasPrefix(x, nextSubCmd) {
|
|
|
|
matches = append(matches, cmd)
|
|
|
|
}
|
2014-10-07 23:41:19 +04:00
|
|
|
}
|
2014-03-26 12:53:00 +04:00
|
|
|
}
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
2013-09-29 09:59:13 +04:00
|
|
|
|
2015-06-09 20:52:43 +03:00
|
|
|
// only accept a single prefix match - multiple matches would be ambiguous
|
|
|
|
if len(matches) == 1 {
|
|
|
|
return innerfind(matches[0], argsMinusFirstX(innerArgs, argsWOflags[0]))
|
|
|
|
}
|
|
|
|
|
2015-05-13 04:38:01 +03:00
|
|
|
return c, innerArgs
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
2013-09-29 09:59:13 +04:00
|
|
|
|
2015-05-13 04:38:01 +03:00
|
|
|
commandFound, a := innerfind(c, args)
|
2015-06-23 00:26:11 +03:00
|
|
|
argsWOflags := stripFlags(a, commandFound)
|
2015-06-09 20:52:43 +03:00
|
|
|
|
|
|
|
// no subcommand, always take args
|
|
|
|
if !commandFound.HasSubCommands() {
|
|
|
|
return commandFound, a, nil
|
|
|
|
}
|
2015-09-11 23:04:58 +03:00
|
|
|
|
2015-06-09 20:52:43 +03:00
|
|
|
// root command with subcommands, do subcommand checking
|
2015-06-23 00:26:11 +03:00
|
|
|
if commandFound == c && len(argsWOflags) > 0 {
|
2015-09-25 17:41:47 +03:00
|
|
|
suggestionsString := ""
|
2015-09-11 23:04:58 +03:00
|
|
|
if !c.DisableSuggestions {
|
|
|
|
if c.SuggestionsMinimumDistance <= 0 {
|
|
|
|
c.SuggestionsMinimumDistance = 2
|
|
|
|
}
|
2015-09-25 17:41:47 +03:00
|
|
|
if suggestions := c.SuggestionsFor(argsWOflags[0]); len(suggestions) > 0 {
|
|
|
|
suggestionsString += "\n\nDid you mean this?\n"
|
|
|
|
for _, s := range suggestions {
|
|
|
|
suggestionsString += fmt.Sprintf("\t%v\n", s)
|
2015-09-11 23:04:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 17:41:47 +03:00
|
|
|
return commandFound, a, fmt.Errorf("unknown command %q for %q%s", argsWOflags[0], commandFound.CommandPath(), suggestionsString)
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
|
|
|
|
2013-09-29 09:59:13 +04:00
|
|
|
return commandFound, a, nil
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// SuggestionsFor provides suggestions for the typedName.
|
2015-09-25 18:04:28 +03:00
|
|
|
func (c *Command) SuggestionsFor(typedName string) []string {
|
2015-09-25 17:41:47 +03:00
|
|
|
suggestions := []string{}
|
|
|
|
for _, cmd := range c.commands {
|
|
|
|
if cmd.IsAvailableCommand() {
|
2015-09-25 18:04:28 +03:00
|
|
|
levenshteinDistance := ld(typedName, cmd.Name(), true)
|
|
|
|
suggestByLevenshtein := levenshteinDistance <= c.SuggestionsMinimumDistance
|
|
|
|
suggestByPrefix := strings.HasPrefix(strings.ToLower(cmd.Name()), strings.ToLower(typedName))
|
|
|
|
if suggestByLevenshtein || suggestByPrefix {
|
2015-09-25 17:41:47 +03:00
|
|
|
suggestions = append(suggestions, cmd.Name())
|
|
|
|
}
|
2015-09-25 18:04:28 +03:00
|
|
|
for _, explicitSuggestion := range cmd.SuggestFor {
|
|
|
|
if strings.EqualFold(typedName, explicitSuggestion) {
|
|
|
|
suggestions = append(suggestions, cmd.Name())
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 17:41:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return suggestions
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// VisitParents visits all parents of the command and invokes fn on each parent.
|
2015-11-08 04:21:25 +03:00
|
|
|
func (c *Command) VisitParents(fn func(*Command)) {
|
2017-04-05 19:44:50 +03:00
|
|
|
if c.HasParent() {
|
|
|
|
fn(c.Parent())
|
|
|
|
c.Parent().VisitParents(fn)
|
2015-11-08 04:21:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// Root finds root command.
|
2013-09-25 00:03:22 +04:00
|
|
|
func (c *Command) Root() *Command {
|
2017-04-05 19:44:50 +03:00
|
|
|
if c.HasParent() {
|
|
|
|
return c.Parent().Root()
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
2017-04-05 19:44:50 +03:00
|
|
|
return c
|
2013-09-25 00:03:22 +04:00
|
|
|
}
|
|
|
|
|
2015-10-14 01:37:14 +03:00
|
|
|
// ArgsLenAtDash will return the length of f.Args at the moment when a -- was
|
|
|
|
// found during arg parsing. This allows your program to know which args were
|
|
|
|
// before the -- and which came after. (Description from
|
|
|
|
// https://godoc.org/github.com/spf13/pflag#FlagSet.ArgsLenAtDash).
|
2015-09-16 19:31:41 +03:00
|
|
|
func (c *Command) ArgsLenAtDash() int {
|
|
|
|
return c.Flags().ArgsLenAtDash()
|
|
|
|
}
|
|
|
|
|
2013-10-01 05:54:46 +04:00
|
|
|
func (c *Command) execute(a []string) (err error) {
|
2013-09-24 20:47:29 +04:00
|
|
|
if c == nil {
|
|
|
|
return fmt.Errorf("Called Execute() on a nil Command")
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:38:22 +03:00
|
|
|
if len(c.Deprecated) > 0 {
|
|
|
|
c.Printf("Command %q is deprecated, %s\n", c.Name(), c.Deprecated)
|
|
|
|
}
|
|
|
|
|
2015-09-01 07:16:41 +03:00
|
|
|
// initialize help flag as the last point possible to allow for user
|
|
|
|
// overriding
|
|
|
|
c.initHelpFlag()
|
|
|
|
|
2013-10-01 05:54:46 +04:00
|
|
|
err = c.ParseFlags(a)
|
|
|
|
if err != nil {
|
2016-05-26 00:27:02 +03:00
|
|
|
return c.FlagErrorFunc()(c, err)
|
2015-04-06 21:54:26 +03:00
|
|
|
}
|
2015-06-10 01:26:25 +03:00
|
|
|
// If help is called, regardless of other flags, return we want help
|
2015-09-02 20:59:31 +03:00
|
|
|
// Also say we need help if the command isn't runnable.
|
2015-09-01 07:16:41 +03:00
|
|
|
helpVal, err := c.Flags().GetBool("help")
|
|
|
|
if err != nil {
|
|
|
|
// should be impossible to get here as we always declare a help
|
|
|
|
// flag in initHelpFlag()
|
|
|
|
c.Println("\"help\" flag declared as non-bool. Please correct your code")
|
|
|
|
return err
|
|
|
|
}
|
2016-07-06 18:51:57 +03:00
|
|
|
|
2015-09-01 07:16:41 +03:00
|
|
|
if helpVal || !c.Runnable() {
|
2015-06-10 01:26:25 +03:00
|
|
|
return flag.ErrHelp
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
2015-04-06 21:54:26 +03:00
|
|
|
|
|
|
|
c.preRun()
|
2016-06-04 03:25:52 +03:00
|
|
|
|
2015-04-06 21:54:26 +03:00
|
|
|
argWoFlags := c.Flags().Args()
|
2016-06-04 03:25:52 +03:00
|
|
|
if c.DisableFlagParsing {
|
|
|
|
argWoFlags = a
|
|
|
|
}
|
2015-02-10 01:44:14 +03:00
|
|
|
|
2015-05-04 04:36:05 +03:00
|
|
|
for p := c; p != nil; p = p.Parent() {
|
2015-08-31 04:03:16 +03:00
|
|
|
if p.PersistentPreRunE != nil {
|
2015-09-30 10:09:17 +03:00
|
|
|
if err := p.PersistentPreRunE(c, argWoFlags); err != nil {
|
2015-08-31 04:03:16 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
} else if p.PersistentPreRun != nil {
|
2015-05-04 04:36:05 +03:00
|
|
|
p.PersistentPreRun(c, argWoFlags)
|
|
|
|
break
|
|
|
|
}
|
2015-02-18 00:09:49 +03:00
|
|
|
}
|
2015-08-31 04:03:16 +03:00
|
|
|
if c.PreRunE != nil {
|
|
|
|
if err := c.PreRunE(c, argWoFlags); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if c.PreRun != nil {
|
2015-02-10 01:44:14 +03:00
|
|
|
c.PreRun(c, argWoFlags)
|
|
|
|
}
|
|
|
|
|
2015-08-31 04:03:16 +03:00
|
|
|
if c.RunE != nil {
|
|
|
|
if err := c.RunE(c, argWoFlags); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.Run(c, argWoFlags)
|
|
|
|
}
|
|
|
|
if c.PostRunE != nil {
|
|
|
|
if err := c.PostRunE(c, argWoFlags); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if c.PostRun != nil {
|
2015-02-10 01:44:14 +03:00
|
|
|
c.PostRun(c, argWoFlags)
|
|
|
|
}
|
2015-05-04 04:36:05 +03:00
|
|
|
for p := c; p != nil; p = p.Parent() {
|
2015-08-31 04:03:16 +03:00
|
|
|
if p.PersistentPostRunE != nil {
|
|
|
|
if err := p.PersistentPostRunE(c, argWoFlags); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
} else if p.PersistentPostRun != nil {
|
2015-05-04 04:36:05 +03:00
|
|
|
p.PersistentPostRun(c, argWoFlags)
|
|
|
|
break
|
|
|
|
}
|
2015-02-18 00:09:49 +03:00
|
|
|
}
|
2015-02-10 01:44:14 +03:00
|
|
|
|
2015-04-06 21:54:26 +03:00
|
|
|
return nil
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 20:27:46 +04:00
|
|
|
func (c *Command) preRun() {
|
2014-07-11 18:57:53 +04:00
|
|
|
for _, x := range initializers {
|
|
|
|
x()
|
|
|
|
}
|
2014-06-27 20:27:46 +04:00
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// Execute Call execute to use the args (os.Args[1:] by default)
|
2013-11-02 02:36:24 +04:00
|
|
|
// and run through the command tree finding appropriate matches
|
|
|
|
// for commands and then corresponding flags.
|
2015-11-17 21:41:21 +03:00
|
|
|
func (c *Command) Execute() error {
|
|
|
|
_, err := c.ExecuteC()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// ExecuteC executes the command.
|
2015-11-17 21:41:21 +03:00
|
|
|
func (c *Command) ExecuteC() (cmd *Command, err error) {
|
2013-11-02 02:36:24 +04:00
|
|
|
// Regardless of what command execute is called on, run on Root only
|
|
|
|
if c.HasParent() {
|
2015-11-17 21:41:21 +03:00
|
|
|
return c.Root().ExecuteC()
|
2013-11-02 02:36:24 +04:00
|
|
|
}
|
|
|
|
|
2015-11-24 02:19:32 +03:00
|
|
|
// windows hook
|
|
|
|
if preExecHookFn != nil {
|
|
|
|
preExecHookFn(c)
|
2015-04-01 22:14:40 +03:00
|
|
|
}
|
|
|
|
|
2013-11-02 02:36:24 +04:00
|
|
|
// initialize help as the last point possible to allow for user
|
|
|
|
// overriding
|
2015-09-01 07:16:41 +03:00
|
|
|
c.initHelpCmd()
|
2013-11-02 02:36:24 +04:00
|
|
|
|
|
|
|
var args []string
|
|
|
|
|
2015-11-08 09:21:50 +03:00
|
|
|
// Workaround FAIL with "go test -v" or "cobra.test -test.v", see #155
|
2016-01-14 07:55:11 +03:00
|
|
|
if c.args == nil && filepath.Base(os.Args[0]) != "cobra.test" {
|
2013-11-02 02:36:24 +04:00
|
|
|
args = os.Args[1:]
|
|
|
|
} else {
|
|
|
|
args = c.args
|
|
|
|
}
|
|
|
|
|
2015-04-06 22:24:36 +03:00
|
|
|
cmd, flags, err := c.Find(args)
|
2015-06-10 01:26:25 +03:00
|
|
|
if err != nil {
|
2015-06-30 00:05:49 +03:00
|
|
|
// If found parse to a subcommand and then failed, talk about the subcommand
|
|
|
|
if cmd != nil {
|
|
|
|
c = cmd
|
|
|
|
}
|
2015-10-26 04:58:53 +03:00
|
|
|
if !c.SilenceErrors {
|
|
|
|
c.Println("Error:", err.Error())
|
|
|
|
c.Printf("Run '%v --help' for usage.\n", c.CommandPath())
|
|
|
|
}
|
2015-11-17 21:41:21 +03:00
|
|
|
return c, err
|
2013-11-02 02:36:24 +04:00
|
|
|
}
|
2015-06-10 01:26:25 +03:00
|
|
|
err = cmd.execute(flags)
|
2013-11-02 02:36:24 +04:00
|
|
|
if err != nil {
|
2015-12-03 16:58:31 +03:00
|
|
|
// Always show help if requested, even if SilenceErrors is in
|
|
|
|
// effect
|
|
|
|
if err == flag.ErrHelp {
|
|
|
|
cmd.HelpFunc()(cmd, args)
|
|
|
|
return cmd, nil
|
|
|
|
}
|
|
|
|
|
2015-10-27 02:42:06 +03:00
|
|
|
// If root command has SilentErrors flagged,
|
|
|
|
// all subcommands should respect it
|
2015-10-26 06:17:39 +03:00
|
|
|
if !cmd.SilenceErrors && !c.SilenceErrors {
|
2015-10-26 04:58:53 +03:00
|
|
|
c.Println("Error:", err.Error())
|
2015-06-10 01:26:25 +03:00
|
|
|
}
|
2015-10-31 16:22:19 +03:00
|
|
|
|
|
|
|
// If root command has SilentUsage flagged,
|
|
|
|
// all subcommands should respect it
|
|
|
|
if !cmd.SilenceUsage && !c.SilenceUsage {
|
|
|
|
c.Println(cmd.UsageString())
|
|
|
|
}
|
2015-11-17 21:41:21 +03:00
|
|
|
return cmd, err
|
2013-11-02 02:36:24 +04:00
|
|
|
}
|
2015-11-17 21:41:21 +03:00
|
|
|
return cmd, nil
|
2013-11-02 02:36:24 +04:00
|
|
|
}
|
|
|
|
|
2015-09-01 07:16:41 +03:00
|
|
|
func (c *Command) initHelpFlag() {
|
2016-06-01 01:38:12 +03:00
|
|
|
c.mergePersistentFlags()
|
2015-08-30 19:10:14 +03:00
|
|
|
if c.Flags().Lookup("help") == nil {
|
2015-09-01 07:16:41 +03:00
|
|
|
c.Flags().BoolP("help", "h", false, "help for "+c.Name())
|
2015-08-30 19:10:14 +03:00
|
|
|
}
|
2015-09-01 07:16:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) initHelpCmd() {
|
2013-11-02 02:36:24 +04:00
|
|
|
if c.helpCommand == nil {
|
2014-11-12 07:32:54 +03:00
|
|
|
if !c.HasSubCommands() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-11-02 02:36:24 +04:00
|
|
|
c.helpCommand = &Command{
|
|
|
|
Use: "help [command]",
|
|
|
|
Short: "Help about any command",
|
|
|
|
Long: `Help provides help for any command in the application.
|
|
|
|
Simply type ` + c.Name() + ` help [path to command] for full details.`,
|
2015-02-18 00:09:49 +03:00
|
|
|
PersistentPreRun: func(cmd *Command, args []string) {},
|
|
|
|
PersistentPostRun: func(cmd *Command, args []string) {},
|
2015-09-01 20:58:55 +03:00
|
|
|
|
|
|
|
Run: func(c *Command, args []string) {
|
|
|
|
cmd, _, e := c.Root().Find(args)
|
|
|
|
if cmd == nil || e != nil {
|
2017-02-28 22:17:48 +03:00
|
|
|
c.Printf("Unknown help topic %#q\n", args)
|
2016-07-20 17:39:46 +03:00
|
|
|
c.Root().Usage()
|
2015-09-01 20:58:55 +03:00
|
|
|
} else {
|
2016-07-20 17:39:46 +03:00
|
|
|
cmd.Help()
|
2015-09-01 20:58:55 +03:00
|
|
|
}
|
|
|
|
},
|
2013-11-02 02:36:24 +04:00
|
|
|
}
|
|
|
|
}
|
2017-03-14 20:12:53 +03:00
|
|
|
c.RemoveCommand(c.helpCommand)
|
2013-11-02 02:36:24 +04:00
|
|
|
c.AddCommand(c.helpCommand)
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// ResetCommands used for testing.
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) ResetCommands() {
|
|
|
|
c.commands = nil
|
2014-11-12 07:32:54 +03:00
|
|
|
c.helpCommand = nil
|
2017-04-06 16:28:17 +03:00
|
|
|
c.parentsPflags = nil
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// Sorts commands by their names.
|
2016-06-14 17:04:53 +03:00
|
|
|
type commandSorterByName []*Command
|
|
|
|
|
|
|
|
func (c commandSorterByName) Len() int { return len(c) }
|
|
|
|
func (c commandSorterByName) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
|
|
|
|
func (c commandSorterByName) Less(i, j int) bool { return c[i].Name() < c[j].Name() }
|
|
|
|
|
|
|
|
// Commands returns a sorted slice of child commands.
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) Commands() []*Command {
|
2016-06-14 17:04:53 +03:00
|
|
|
// do not sort commands if it already sorted or sorting was disabled
|
2016-07-06 18:51:57 +03:00
|
|
|
if EnableCommandSorting && !c.commandsAreSorted {
|
2016-06-14 17:04:53 +03:00
|
|
|
sort.Sort(commandSorterByName(c.commands))
|
|
|
|
c.commandsAreSorted = true
|
|
|
|
}
|
2013-09-24 20:47:29 +04:00
|
|
|
return c.commands
|
|
|
|
}
|
|
|
|
|
2014-12-13 08:05:43 +03:00
|
|
|
// AddCommand adds one or more commands to this parent command.
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) AddCommand(cmds ...*Command) {
|
|
|
|
for i, x := range cmds {
|
|
|
|
if cmds[i] == c {
|
|
|
|
panic("Command can't be a child of itself")
|
|
|
|
}
|
|
|
|
cmds[i].parent = c
|
2014-01-17 16:36:57 +04:00
|
|
|
// update max lengths
|
|
|
|
usageLen := len(x.Use)
|
|
|
|
if usageLen > c.commandsMaxUseLen {
|
|
|
|
c.commandsMaxUseLen = usageLen
|
|
|
|
}
|
|
|
|
commandPathLen := len(x.CommandPath())
|
|
|
|
if commandPathLen > c.commandsMaxCommandPathLen {
|
|
|
|
c.commandsMaxCommandPathLen = commandPathLen
|
|
|
|
}
|
2015-02-11 12:20:29 +03:00
|
|
|
nameLen := len(x.Name())
|
|
|
|
if nameLen > c.commandsMaxNameLen {
|
|
|
|
c.commandsMaxNameLen = nameLen
|
|
|
|
}
|
2015-12-16 11:14:10 +03:00
|
|
|
// If global normalization function exists, update all children
|
2015-05-14 20:38:15 +03:00
|
|
|
if c.globNormFunc != nil {
|
|
|
|
x.SetGlobalNormalizationFunc(c.globNormFunc)
|
|
|
|
}
|
2013-09-24 20:47:29 +04:00
|
|
|
c.commands = append(c.commands, x)
|
2016-06-14 17:04:53 +03:00
|
|
|
c.commandsAreSorted = false
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-17 20:17:01 +03:00
|
|
|
// RemoveCommand removes one or more commands from a parent command.
|
2015-03-03 09:50:53 +03:00
|
|
|
func (c *Command) RemoveCommand(cmds ...*Command) {
|
|
|
|
commands := []*Command{}
|
|
|
|
main:
|
|
|
|
for _, command := range c.commands {
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
if command == cmd {
|
|
|
|
command.parent = nil
|
|
|
|
continue main
|
|
|
|
}
|
|
|
|
}
|
|
|
|
commands = append(commands, command)
|
|
|
|
}
|
|
|
|
c.commands = commands
|
|
|
|
// recompute all lengths
|
|
|
|
c.commandsMaxUseLen = 0
|
|
|
|
c.commandsMaxCommandPathLen = 0
|
|
|
|
c.commandsMaxNameLen = 0
|
|
|
|
for _, command := range c.commands {
|
|
|
|
usageLen := len(command.Use)
|
|
|
|
if usageLen > c.commandsMaxUseLen {
|
|
|
|
c.commandsMaxUseLen = usageLen
|
|
|
|
}
|
|
|
|
commandPathLen := len(command.CommandPath())
|
|
|
|
if commandPathLen > c.commandsMaxCommandPathLen {
|
|
|
|
c.commandsMaxCommandPathLen = commandPathLen
|
|
|
|
}
|
|
|
|
nameLen := len(command.Name())
|
|
|
|
if nameLen > c.commandsMaxNameLen {
|
|
|
|
c.commandsMaxNameLen = nameLen
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// Print is a convenience method to Print to the defined output, fallback to Stderr if not set.
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) Print(i ...interface{}) {
|
2016-07-15 23:12:07 +03:00
|
|
|
fmt.Fprint(c.OutOrStderr(), i...)
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// Println is a convenience method to Println to the defined output, fallback to Stderr if not set.
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) Println(i ...interface{}) {
|
|
|
|
str := fmt.Sprintln(i...)
|
|
|
|
c.Print(str)
|
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// Printf is a convenience method to Printf to the defined output, fallback to Stderr if not set.
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) Printf(format string, i ...interface{}) {
|
|
|
|
str := fmt.Sprintf(format, i...)
|
|
|
|
c.Print(str)
|
|
|
|
}
|
|
|
|
|
2014-12-13 08:05:43 +03:00
|
|
|
// CommandPath returns the full path to this command.
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) CommandPath() string {
|
|
|
|
str := c.Name()
|
|
|
|
x := c
|
|
|
|
for x.HasParent() {
|
|
|
|
str = x.parent.Name() + " " + str
|
|
|
|
x = x.parent
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// UseLine puts out the full usage for a given command (including parents).
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) UseLine() string {
|
|
|
|
str := ""
|
|
|
|
if c.HasParent() {
|
|
|
|
str = c.parent.CommandPath() + " "
|
|
|
|
}
|
|
|
|
return str + c.Use
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// DebugFlags used to determine which flags have been assigned to which commands
|
2016-08-30 20:14:27 +03:00
|
|
|
// and which persist.
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) DebugFlags() {
|
|
|
|
c.Println("DebugFlags called on", c.Name())
|
|
|
|
var debugflags func(*Command)
|
|
|
|
|
|
|
|
debugflags = func(x *Command) {
|
|
|
|
if x.HasFlags() || x.HasPersistentFlags() {
|
|
|
|
c.Println(x.Name())
|
|
|
|
}
|
|
|
|
if x.HasFlags() {
|
|
|
|
x.flags.VisitAll(func(f *flag.Flag) {
|
2017-04-05 19:44:50 +03:00
|
|
|
if x.HasPersistentFlags() && x.persistentFlag(f.Name) != nil {
|
|
|
|
c.Println(" -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, " [LP]")
|
2013-09-24 20:47:29 +04:00
|
|
|
} else {
|
|
|
|
c.Println(" -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, " [L]")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if x.HasPersistentFlags() {
|
|
|
|
x.pflags.VisitAll(func(f *flag.Flag) {
|
|
|
|
if x.HasFlags() {
|
|
|
|
if x.flags.Lookup(f.Name) == nil {
|
|
|
|
c.Println(" -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, " [P]")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.Println(" -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, " [P]")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if x.HasSubCommands() {
|
|
|
|
for _, y := range x.commands {
|
|
|
|
debugflags(y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debugflags(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the command's name: the first word in the use line.
|
|
|
|
func (c *Command) Name() string {
|
|
|
|
if c.name != "" {
|
|
|
|
return c.name
|
|
|
|
}
|
|
|
|
name := c.Use
|
|
|
|
i := strings.Index(name, " ")
|
|
|
|
if i >= 0 {
|
|
|
|
name = name[:i]
|
|
|
|
}
|
2017-02-09 19:54:17 +03:00
|
|
|
c.name = name
|
|
|
|
return c.name
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
|
|
|
|
2016-03-31 16:53:34 +03:00
|
|
|
// HasAlias determines if a given string is an alias of the command.
|
2014-09-26 03:03:34 +04:00
|
|
|
func (c *Command) HasAlias(s string) bool {
|
|
|
|
for _, a := range c.Aliases {
|
|
|
|
if a == s {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// NameAndAliases returns string containing name and all aliases
|
2014-09-26 03:03:34 +04:00
|
|
|
func (c *Command) NameAndAliases() string {
|
|
|
|
return strings.Join(append([]string{c.Name()}, c.Aliases...), ", ")
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// HasExample determines if the command has example.
|
2015-02-13 02:12:07 +03:00
|
|
|
func (c *Command) HasExample() bool {
|
|
|
|
return len(c.Example) > 0
|
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// Runnable determines if the command is itself runnable.
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) Runnable() bool {
|
2015-09-02 20:59:31 +03:00
|
|
|
return c.Run != nil || c.RunE != nil
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// HasSubCommands determines if the command has children commands.
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) HasSubCommands() bool {
|
|
|
|
return len(c.commands) > 0
|
|
|
|
}
|
|
|
|
|
2015-09-04 23:34:51 +03:00
|
|
|
// IsAvailableCommand determines if a command is available as a non-help command
|
2016-08-30 20:14:27 +03:00
|
|
|
// (this includes all non deprecated/hidden commands).
|
2015-09-04 23:34:51 +03:00
|
|
|
func (c *Command) IsAvailableCommand() bool {
|
2015-09-09 05:43:49 +03:00
|
|
|
if len(c.Deprecated) != 0 || c.Hidden {
|
|
|
|
return false
|
|
|
|
}
|
2015-09-04 23:34:51 +03:00
|
|
|
|
2015-09-12 01:18:36 +03:00
|
|
|
if c.HasParent() && c.Parent().helpCommand == c {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-09-09 05:43:49 +03:00
|
|
|
if c.Runnable() || c.HasAvailableSubCommands() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2015-09-04 23:34:51 +03:00
|
|
|
}
|
|
|
|
|
2017-03-09 18:37:15 +03:00
|
|
|
// IsAdditionalHelpTopicCommand determines if a command is an additional
|
|
|
|
// help topic command; additional help topic command is determined by the
|
|
|
|
// fact that it is NOT runnable/hidden/deprecated, and has no sub commands that
|
|
|
|
// are runnable/hidden/deprecated.
|
|
|
|
// Concrete example: https://github.com/spf13/cobra/issues/393#issuecomment-282741924.
|
|
|
|
func (c *Command) IsAdditionalHelpTopicCommand() bool {
|
2015-09-04 23:34:51 +03:00
|
|
|
// if a command is runnable, deprecated, or hidden it is not a 'help' command
|
|
|
|
if c.Runnable() || len(c.Deprecated) != 0 || c.Hidden {
|
2015-03-20 21:47:56 +03:00
|
|
|
return false
|
|
|
|
}
|
2015-09-04 23:34:51 +03:00
|
|
|
|
|
|
|
// if any non-help sub commands are found, the command is not a 'help' command
|
2015-06-30 03:06:04 +03:00
|
|
|
for _, sub := range c.commands {
|
2017-03-09 18:37:15 +03:00
|
|
|
if !sub.IsAdditionalHelpTopicCommand() {
|
2015-06-30 03:06:04 +03:00
|
|
|
return false
|
2015-03-20 21:47:56 +03:00
|
|
|
}
|
|
|
|
}
|
2015-09-04 23:34:51 +03:00
|
|
|
|
|
|
|
// the command either has no sub commands, or no non-help sub commands
|
2015-06-30 03:06:04 +03:00
|
|
|
return true
|
2015-03-20 21:47:56 +03:00
|
|
|
}
|
|
|
|
|
2016-08-20 10:04:53 +03:00
|
|
|
// HasHelpSubCommands determines if a command has any available 'help' sub commands
|
2015-09-04 23:34:51 +03:00
|
|
|
// that need to be shown in the usage/help default template under 'additional help
|
2016-08-30 20:14:27 +03:00
|
|
|
// topics'.
|
2015-03-20 21:47:56 +03:00
|
|
|
func (c *Command) HasHelpSubCommands() bool {
|
2015-09-04 23:34:51 +03:00
|
|
|
// return true on the first found available 'help' sub command
|
2015-03-20 21:47:56 +03:00
|
|
|
for _, sub := range c.commands {
|
2017-03-09 18:37:15 +03:00
|
|
|
if sub.IsAdditionalHelpTopicCommand() {
|
2015-03-20 21:47:56 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2015-09-04 23:34:51 +03:00
|
|
|
|
|
|
|
// the command either has no sub commands, or no available 'help' sub commands
|
2015-03-20 21:47:56 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-09-04 23:34:51 +03:00
|
|
|
// HasAvailableSubCommands determines if a command has available sub commands that
|
2016-08-30 20:14:27 +03:00
|
|
|
// need to be shown in the usage/help default template under 'available commands'.
|
2015-09-04 23:34:51 +03:00
|
|
|
func (c *Command) HasAvailableSubCommands() bool {
|
|
|
|
// return true on the first found available (non deprecated/help/hidden)
|
|
|
|
// sub command
|
2015-03-20 21:47:56 +03:00
|
|
|
for _, sub := range c.commands {
|
2015-09-04 23:34:51 +03:00
|
|
|
if sub.IsAvailableCommand() {
|
2015-03-20 21:47:56 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2015-09-04 23:34:51 +03:00
|
|
|
|
|
|
|
// the command either has no sub comamnds, or no available (non deprecated/help/hidden)
|
|
|
|
// sub commands
|
2015-03-20 21:47:56 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// HasParent determines if the command is a child command.
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) HasParent() bool {
|
|
|
|
return c.parent != nil
|
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// GlobalNormalizationFunc returns the global normalization function or nil if doesn't exists.
|
2015-05-14 20:38:15 +03:00
|
|
|
func (c *Command) GlobalNormalizationFunc() func(f *flag.FlagSet, name string) flag.NormalizedName {
|
|
|
|
return c.globNormFunc
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// Flags returns the complete FlagSet that applies
|
2016-08-30 20:14:27 +03:00
|
|
|
// to this command (local and persistent declared here and by all parents).
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) Flags() *flag.FlagSet {
|
|
|
|
if c.flags == nil {
|
|
|
|
c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
2017-04-19 16:23:43 +03:00
|
|
|
c.flags.SetOutput(c.OutOrStderr())
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
2017-04-05 19:44:50 +03:00
|
|
|
|
2013-09-24 20:47:29 +04:00
|
|
|
return c.flags
|
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// LocalNonPersistentFlags are flags specific to this command which will NOT persist to subcommands.
|
2016-06-03 19:37:40 +03:00
|
|
|
func (c *Command) LocalNonPersistentFlags() *flag.FlagSet {
|
|
|
|
persistentFlags := c.PersistentFlags()
|
|
|
|
|
|
|
|
out := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
|
|
|
c.LocalFlags().VisitAll(func(f *flag.Flag) {
|
|
|
|
if persistentFlags.Lookup(f.Name) == nil {
|
|
|
|
out.AddFlag(f)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// LocalFlags returns the local FlagSet specifically set in the current command.
|
2015-01-29 23:55:29 +03:00
|
|
|
func (c *Command) LocalFlags() *flag.FlagSet {
|
|
|
|
c.mergePersistentFlags()
|
|
|
|
|
2017-04-05 19:44:50 +03:00
|
|
|
if c.lflags == nil {
|
|
|
|
c.lflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
|
|
|
c.lflags.SetOutput(c.OutOrStderr())
|
|
|
|
}
|
2017-04-19 12:17:48 +03:00
|
|
|
c.lflags.SortFlags = c.Flags().SortFlags
|
2017-04-05 19:44:50 +03:00
|
|
|
|
|
|
|
addToLocal := func(f *flag.Flag) {
|
|
|
|
if c.lflags.Lookup(f.Name) == nil && c.parentsPflags.Lookup(f.Name) == nil {
|
|
|
|
c.lflags.AddFlag(f)
|
|
|
|
}
|
2015-08-17 08:14:43 +03:00
|
|
|
}
|
2017-04-19 12:17:48 +03:00
|
|
|
c.Flags().VisitAll(addToLocal)
|
2017-04-05 19:44:50 +03:00
|
|
|
c.PersistentFlags().VisitAll(addToLocal)
|
|
|
|
return c.lflags
|
2015-01-29 23:55:29 +03:00
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// InheritedFlags returns all flags which were inherited from parents commands.
|
2015-02-14 03:44:12 +03:00
|
|
|
func (c *Command) InheritedFlags() *flag.FlagSet {
|
|
|
|
c.mergePersistentFlags()
|
|
|
|
|
2017-04-05 19:44:50 +03:00
|
|
|
if c.iflags == nil {
|
|
|
|
c.iflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
|
|
|
}
|
2015-02-28 03:26:56 +03:00
|
|
|
|
2017-04-05 19:44:50 +03:00
|
|
|
local := c.LocalFlags()
|
|
|
|
c.parentsPflags.VisitAll(func(f *flag.Flag) {
|
|
|
|
if c.iflags.Lookup(f.Name) == nil && local.Lookup(f.Name) == nil {
|
|
|
|
c.iflags.AddFlag(f)
|
2015-02-28 03:26:56 +03:00
|
|
|
}
|
2017-04-05 19:44:50 +03:00
|
|
|
})
|
|
|
|
return c.iflags
|
2015-02-14 03:44:12 +03:00
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// NonInheritedFlags returns all flags which were not inherited from parent commands.
|
2015-02-14 03:45:00 +03:00
|
|
|
func (c *Command) NonInheritedFlags() *flag.FlagSet {
|
2015-02-28 03:26:56 +03:00
|
|
|
return c.LocalFlags()
|
2015-02-14 03:45:00 +03:00
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// PersistentFlags returns the persistent FlagSet specifically set in the current command.
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) PersistentFlags() *flag.FlagSet {
|
|
|
|
if c.pflags == nil {
|
|
|
|
c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
2017-04-19 16:23:43 +03:00
|
|
|
c.pflags.SetOutput(c.OutOrStderr())
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
|
|
|
return c.pflags
|
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// ResetFlags is used in testing.
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) ResetFlags() {
|
|
|
|
c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
2017-04-19 16:23:43 +03:00
|
|
|
c.flags.SetOutput(c.OutOrStderr())
|
2013-09-24 20:47:29 +04:00
|
|
|
c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
2017-04-19 16:23:43 +03:00
|
|
|
c.pflags.SetOutput(c.OutOrStderr())
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// HasFlags checks if the command contains any flags (local plus persistent from the entire structure).
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) HasFlags() bool {
|
|
|
|
return c.Flags().HasFlags()
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// HasPersistentFlags checks if the command contains persistent flags.
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) HasPersistentFlags() bool {
|
|
|
|
return c.PersistentFlags().HasFlags()
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// HasLocalFlags checks if the command has flags specifically declared locally.
|
2015-01-29 23:55:29 +03:00
|
|
|
func (c *Command) HasLocalFlags() bool {
|
|
|
|
return c.LocalFlags().HasFlags()
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// HasInheritedFlags checks if the command has flags inherited from its parent command.
|
2015-02-28 03:26:56 +03:00
|
|
|
func (c *Command) HasInheritedFlags() bool {
|
|
|
|
return c.InheritedFlags().HasFlags()
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// HasAvailableFlags checks if the command contains any flags (local plus persistent from the entire
|
2016-08-30 20:14:27 +03:00
|
|
|
// structure) which are not hidden or deprecated.
|
2016-04-13 21:27:05 +03:00
|
|
|
func (c *Command) HasAvailableFlags() bool {
|
|
|
|
return c.Flags().HasAvailableFlags()
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// HasAvailablePersistentFlags checks if the command contains persistent flags which are not hidden or deprecated.
|
2016-04-13 21:27:05 +03:00
|
|
|
func (c *Command) HasAvailablePersistentFlags() bool {
|
|
|
|
return c.PersistentFlags().HasAvailableFlags()
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// HasAvailableLocalFlags checks if the command has flags specifically declared locally which are not hidden
|
2016-08-30 20:14:27 +03:00
|
|
|
// or deprecated.
|
2016-04-13 21:27:05 +03:00
|
|
|
func (c *Command) HasAvailableLocalFlags() bool {
|
|
|
|
return c.LocalFlags().HasAvailableFlags()
|
|
|
|
}
|
|
|
|
|
2017-01-24 19:30:45 +03:00
|
|
|
// HasAvailableInheritedFlags checks if the command has flags inherited from its parent command which are
|
2016-08-30 20:14:27 +03:00
|
|
|
// not hidden or deprecated.
|
2016-04-13 21:27:05 +03:00
|
|
|
func (c *Command) HasAvailableInheritedFlags() bool {
|
|
|
|
return c.InheritedFlags().HasAvailableFlags()
|
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// Flag climbs up the command tree looking for matching flag.
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) Flag(name string) (flag *flag.Flag) {
|
|
|
|
flag = c.Flags().Lookup(name)
|
|
|
|
|
|
|
|
if flag == nil {
|
|
|
|
flag = c.persistentFlag(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// Recursively find matching persistent flag.
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) persistentFlag(name string) (flag *flag.Flag) {
|
|
|
|
if c.HasPersistentFlags() {
|
|
|
|
flag = c.PersistentFlags().Lookup(name)
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:17:48 +03:00
|
|
|
if flag == nil {
|
|
|
|
c.updateParentsPflags()
|
|
|
|
flag = c.parentsPflags.Lookup(name)
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// ParseFlags parses persistent flag tree and local flags.
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) ParseFlags(args []string) (err error) {
|
2016-06-04 03:25:52 +03:00
|
|
|
if c.DisableFlagParsing {
|
|
|
|
return nil
|
|
|
|
}
|
2013-09-24 20:47:29 +04:00
|
|
|
c.mergePersistentFlags()
|
|
|
|
err = c.Flags().Parse(args)
|
2015-03-11 20:22:30 +03:00
|
|
|
return
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
|
|
|
|
2016-08-30 20:14:27 +03:00
|
|
|
// Parent returns a commands parent command.
|
2015-01-29 23:55:29 +03:00
|
|
|
func (c *Command) Parent() *Command {
|
|
|
|
return c.parent
|
|
|
|
}
|
|
|
|
|
2017-04-06 16:28:17 +03:00
|
|
|
// mergePersistentFlags merges c.PersistentFlags() to c.Flags()
|
|
|
|
// and adds missing persistent flags of all parents.
|
|
|
|
func (c *Command) mergePersistentFlags() {
|
2017-04-19 11:50:00 +03:00
|
|
|
c.Flags().AddFlagSet(c.PersistentFlags())
|
2017-04-19 12:17:48 +03:00
|
|
|
c.updateParentsPflags()
|
|
|
|
c.Flags().AddFlagSet(c.parentsPflags)
|
2017-04-06 16:28:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// updateParentsPflags updates c.parentsPflags by adding
|
2017-04-19 12:17:48 +03:00
|
|
|
// new persistent flags of all parents.
|
2017-04-06 16:28:17 +03:00
|
|
|
// If c.parentsPflags == nil, it makes new.
|
2017-04-19 12:17:48 +03:00
|
|
|
func (c *Command) updateParentsPflags() {
|
2017-04-05 19:44:50 +03:00
|
|
|
if c.parentsPflags == nil {
|
|
|
|
c.parentsPflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
|
|
|
c.parentsPflags.SetOutput(c.OutOrStderr())
|
2017-04-06 16:28:17 +03:00
|
|
|
c.parentsPflags.SortFlags = false
|
2017-04-05 19:44:50 +03:00
|
|
|
}
|
2013-09-24 20:47:29 +04:00
|
|
|
|
2017-04-19 11:49:22 +03:00
|
|
|
c.Root().PersistentFlags().AddFlagSet(flag.CommandLine)
|
|
|
|
|
2017-04-19 12:17:48 +03:00
|
|
|
c.VisitParents(func(parent *Command) {
|
|
|
|
c.parentsPflags.AddFlagSet(parent.PersistentFlags())
|
2017-04-05 19:44:50 +03:00
|
|
|
})
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|