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"
|
2013-09-24 20:47:29 +04:00
|
|
|
"strings"
|
2014-02-03 07:20:55 +04: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
|
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
|
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
|
2013-09-24 20:47:29 +04:00
|
|
|
// Run runs the command.
|
|
|
|
// The args are the arguments after the command name.
|
|
|
|
Run func(cmd *Command, args []string)
|
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
|
2013-11-02 02:36:24 +04:00
|
|
|
|
2013-09-24 20:47:29 +04:00
|
|
|
flagErrorBuf *bytes.Buffer
|
2014-12-09 16:42:53 +03:00
|
|
|
cmdErrorBuf *bytes.Buffer
|
2013-11-02 02:36:24 +04:00
|
|
|
|
2014-12-13 08:05:43 +03:00
|
|
|
args []string // actual args parsed from flags
|
2014-04-11 18:11:09 +04:00
|
|
|
output *io.Writer // nil means stderr; use Out() method instead
|
2013-11-02 02:36:24 +04:00
|
|
|
usageFunc func(*Command) error // Usage can be defined by application
|
|
|
|
usageTemplate string // Can be defined by Application
|
|
|
|
helpTemplate string // Can be defined by Application
|
|
|
|
helpFunc func(*Command, []string) // Help can be defined by application
|
|
|
|
helpCommand *Command // The help command
|
2014-06-17 20:28:42 +04:00
|
|
|
helpFlagVal bool
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
|
|
|
|
2013-11-02 02:36:24 +04:00
|
|
|
// os.Args[1:] by default, if desired, can be overridden
|
|
|
|
// particularly useful when testing.
|
|
|
|
func (c *Command) SetArgs(a []string) {
|
|
|
|
c.args = a
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) Out() io.Writer {
|
|
|
|
if c.output != nil {
|
|
|
|
return *c.output
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HasParent() {
|
|
|
|
return c.parent.Out()
|
|
|
|
} else {
|
|
|
|
return os.Stderr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOutput sets the destination for usage and error messages.
|
|
|
|
// If output is nil, os.Stderr is used.
|
|
|
|
func (c *Command) SetOutput(output io.Writer) {
|
|
|
|
c.output = &output
|
|
|
|
}
|
|
|
|
|
|
|
|
// Usage can be defined by application
|
|
|
|
func (c *Command) SetUsageFunc(f func(*Command) error) {
|
|
|
|
c.usageFunc = f
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can be defined by Application
|
|
|
|
func (c *Command) SetUsageTemplate(s string) {
|
|
|
|
c.usageTemplate = s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can be defined by Application
|
|
|
|
func (c *Command) SetHelpFunc(f func(*Command, []string)) {
|
|
|
|
c.helpFunc = f
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) SetHelpCommand(cmd *Command) {
|
|
|
|
c.helpCommand = cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can be defined by Application
|
|
|
|
func (c *Command) SetHelpTemplate(s string) {
|
|
|
|
c.helpTemplate = s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) UsageFunc() (f func(*Command) error) {
|
|
|
|
if c.usageFunc != nil {
|
|
|
|
return c.usageFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HasParent() {
|
|
|
|
return c.parent.UsageFunc()
|
|
|
|
} else {
|
|
|
|
return func(c *Command) error {
|
|
|
|
err := tmpl(c.Out(), c.UsageTemplate(), c)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (c *Command) HelpFunc() func(*Command, []string) {
|
|
|
|
if c.helpFunc != nil {
|
|
|
|
return c.helpFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HasParent() {
|
|
|
|
return c.parent.HelpFunc()
|
|
|
|
} else {
|
|
|
|
return func(c *Command, args []string) {
|
|
|
|
if len(args) == 0 {
|
|
|
|
// Help called without any topic, calling on root
|
2014-09-03 12:24:07 +04:00
|
|
|
c.Root().Help()
|
2013-11-02 02:36:24 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd, _, e := c.Root().Find(args)
|
|
|
|
if cmd == nil || e != nil {
|
|
|
|
c.Printf("Unknown help topic %#q.", args)
|
|
|
|
|
|
|
|
c.Root().Usage()
|
|
|
|
} else {
|
|
|
|
err := cmd.Help()
|
|
|
|
if err != nil {
|
|
|
|
c.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-17 16:36:57 +04:00
|
|
|
var minUsagePadding int = 25
|
|
|
|
|
|
|
|
func (c *Command) UsagePadding() int {
|
|
|
|
if c.parent == nil || minUsagePadding > c.parent.commandsMaxUseLen {
|
|
|
|
return minUsagePadding
|
|
|
|
} else {
|
|
|
|
return c.parent.commandsMaxUseLen
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var minCommandPathPadding int = 11
|
|
|
|
|
2014-12-13 08:05:43 +03:00
|
|
|
//
|
2014-01-17 16:36:57 +04:00
|
|
|
func (c *Command) CommandPathPadding() int {
|
|
|
|
if c.parent == nil || minCommandPathPadding > c.parent.commandsMaxCommandPathLen {
|
|
|
|
return minCommandPathPadding
|
|
|
|
} else {
|
|
|
|
return c.parent.commandsMaxCommandPathLen
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-11 12:20:29 +03:00
|
|
|
var minNamePadding int = 11
|
|
|
|
|
|
|
|
func (c *Command) NamePadding() int {
|
|
|
|
if c.parent == nil || minNamePadding > c.parent.commandsMaxNameLen {
|
|
|
|
return minNamePadding
|
|
|
|
} else {
|
|
|
|
return c.parent.commandsMaxNameLen
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
} else {
|
|
|
|
return `{{ $cmd := . }}
|
|
|
|
Usage: {{if .Runnable}}
|
|
|
|
{{.UseLine}}{{if .HasFlags}} [flags]{{end}}{{end}}{{if .HasSubCommands}}
|
2014-09-26 03:03:34 +04:00
|
|
|
{{ .CommandPath}} [command]{{end}}{{if gt .Aliases 0}}
|
|
|
|
|
|
|
|
Aliases:
|
2015-02-13 02:12:07 +03:00
|
|
|
{{.NameAndAliases}}
|
|
|
|
{{end}}{{if .HasExample}}
|
|
|
|
Examples:
|
|
|
|
{{ .Example }}
|
|
|
|
{{end}}{{ if .HasSubCommands}}
|
2013-11-02 02:36:24 +04:00
|
|
|
Available Commands: {{range .Commands}}{{if .Runnable}}
|
2015-02-11 12:20:29 +03:00
|
|
|
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}
|
2013-11-02 02:36:24 +04:00
|
|
|
{{end}}
|
2015-01-29 23:55:29 +03:00
|
|
|
{{ if .HasLocalFlags}}Flags:
|
|
|
|
{{.LocalFlags.FlagUsages}}{{end}}
|
2015-02-28 03:26:56 +03:00
|
|
|
{{ if .HasInheritedFlags}}Global Flags:
|
|
|
|
{{.InheritedFlags.FlagUsages}}{{end}}{{if .HasParent}}{{if and (gt .Commands 0) (gt .Parent.Commands 1) }}
|
2014-06-14 04:06:09 +04:00
|
|
|
Additional help topics: {{if gt .Commands 0 }}{{range .Commands}}{{if not .Runnable}} {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if gt .Parent.Commands 1 }}{{range .Parent.Commands}}{{if .Runnable}}{{if not (eq .Name $cmd.Name) }}{{end}}
|
|
|
|
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{end}}
|
2014-11-12 07:32:54 +03:00
|
|
|
{{end}}{{ if .HasSubCommands }}
|
2015-01-31 19:03:26 +03:00
|
|
|
Use "{{.Root.Name}} help [command]" for more information about a command.
|
2014-11-12 07:32:54 +03:00
|
|
|
{{end}}`
|
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()
|
|
|
|
} else {
|
|
|
|
return `{{.Long | trim}}
|
2014-09-03 12:24:07 +04:00
|
|
|
{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}
|
2013-11-02 02:36:24 +04:00
|
|
|
`
|
|
|
|
}
|
2013-09-29 09:57:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Really only used when casting a command to a commander
|
|
|
|
func (c *Command) resetChildrensParents() {
|
|
|
|
for _, x := range c.commands {
|
|
|
|
x.parent = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 21:52:35 +03:00
|
|
|
// Test if the named flag is a boolean flag.
|
|
|
|
func isBooleanFlag(name string, f *flag.FlagSet) bool {
|
|
|
|
flag := f.Lookup(name)
|
|
|
|
if flag == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return flag.Value.Type() == "bool"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test if the named flag is a boolean flag.
|
|
|
|
func isBooleanShortFlag(name string, f *flag.FlagSet) bool {
|
2015-03-11 20:22:30 +03:00
|
|
|
result := false
|
|
|
|
f.VisitAll(func(f *flag.Flag) {
|
|
|
|
if f.Shorthand == name && f.Value.Type() == "bool" {
|
|
|
|
result = true
|
|
|
|
}
|
|
|
|
})
|
2015-03-11 21:52:35 +03:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func stripFlags(args []string, c *Command) []string {
|
2014-06-17 20:32:27 +04:00
|
|
|
if len(args) < 1 {
|
|
|
|
return args
|
|
|
|
}
|
2015-03-16 22:47:32 +03:00
|
|
|
c.mergePersistentFlags()
|
2014-06-17 20:32:27 +04:00
|
|
|
|
|
|
|
commands := []string{}
|
|
|
|
|
|
|
|
inQuote := false
|
2015-03-11 21:52:35 +03:00
|
|
|
inFlag := false
|
2014-06-17 20:32:27 +04:00
|
|
|
for _, y := range args {
|
|
|
|
if !inQuote {
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(y, "\""):
|
|
|
|
inQuote = true
|
|
|
|
case strings.Contains(y, "=\""):
|
|
|
|
inQuote = true
|
2015-03-11 21:52:35 +03:00
|
|
|
case strings.HasPrefix(y, "--") && !strings.Contains(y, "="):
|
|
|
|
// TODO: this isn't quite right, we should really check ahead for 'true' or 'false'
|
|
|
|
inFlag = !isBooleanFlag(y[2:], c.Flags())
|
|
|
|
case strings.HasPrefix(y, "-") && !strings.Contains(y, "=") && len(y) == 2 && !isBooleanShortFlag(y[1:], c.Flags()):
|
|
|
|
inFlag = true
|
|
|
|
case inFlag:
|
|
|
|
inFlag = false
|
2014-06-17 20:32:27 +04:00
|
|
|
case !strings.HasPrefix(y, "-"):
|
|
|
|
commands = append(commands, y)
|
2015-03-11 21:52:35 +03:00
|
|
|
inFlag = false
|
2014-06-17 20:32:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(y, "\"") && !strings.HasSuffix(y, "\\\"") {
|
|
|
|
inQuote = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return commands
|
|
|
|
}
|
|
|
|
|
|
|
|
func argsMinusX(args []string, x string) []string {
|
|
|
|
newargs := []string{}
|
|
|
|
|
|
|
|
for _, y := range args {
|
|
|
|
if x != y {
|
|
|
|
newargs = append(newargs, y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newargs
|
|
|
|
}
|
|
|
|
|
2013-09-24 20:47:29 +04: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.
|
2013-09-29 09:59:13 +04:00
|
|
|
func (c *Command) Find(arrs []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
|
|
|
if len(arrs) == 0 {
|
2013-11-02 02:36:24 +04:00
|
|
|
return c.Root(), arrs, nil
|
2013-09-29 09:59:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
var innerfind func(*Command, []string) (*Command, []string)
|
|
|
|
|
|
|
|
innerfind = func(c *Command, args []string) (*Command, []string) {
|
|
|
|
if len(args) > 0 && c.HasSubCommands() {
|
2015-03-11 21:52:35 +03:00
|
|
|
argsWOflags := stripFlags(args, c)
|
2014-06-17 20:32:27 +04:00
|
|
|
if len(argsWOflags) > 0 {
|
2014-10-07 23:41:19 +04:00
|
|
|
matches := make([]*Command, 0)
|
2014-06-17 20:32:27 +04:00
|
|
|
for _, cmd := range c.commands {
|
2014-09-26 03:03:34 +04:00
|
|
|
if cmd.Name() == argsWOflags[0] || cmd.HasAlias(argsWOflags[0]) { // exact name or alias match
|
|
|
|
return innerfind(cmd, argsMinusX(args, argsWOflags[0]))
|
2014-10-08 00:15:19 +04:00
|
|
|
} else if EnablePrefixMatching {
|
2014-10-07 23:41:19 +04:00
|
|
|
if strings.HasPrefix(cmd.Name(), argsWOflags[0]) { // prefix match
|
|
|
|
matches = append(matches, cmd)
|
|
|
|
}
|
|
|
|
for _, x := range cmd.Aliases {
|
|
|
|
if strings.HasPrefix(x, argsWOflags[0]) {
|
|
|
|
matches = append(matches, cmd)
|
|
|
|
}
|
|
|
|
}
|
2014-06-17 20:32:27 +04:00
|
|
|
}
|
2013-09-29 09:59:13 +04:00
|
|
|
}
|
2014-10-07 23:41:19 +04:00
|
|
|
|
|
|
|
// only accept a single prefix match - multiple matches would be ambiguous
|
|
|
|
if len(matches) == 1 {
|
|
|
|
return innerfind(matches[0], argsMinusX(args, argsWOflags[0]))
|
|
|
|
}
|
2014-03-26 12:53:00 +04:00
|
|
|
}
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
2013-09-29 09:59:13 +04:00
|
|
|
|
|
|
|
return c, args
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
2013-09-29 09:59:13 +04:00
|
|
|
|
|
|
|
commandFound, a := innerfind(c, arrs)
|
|
|
|
|
2014-09-26 03:03:34 +04:00
|
|
|
// if commander returned and the first argument (if it exists) doesn't
|
|
|
|
// match the command name, return nil & error
|
|
|
|
if commandFound.Name() == c.Name() && len(arrs[0]) > 0 && commandFound.Name() != arrs[0] {
|
2013-10-01 05:54:46 +04:00
|
|
|
return nil, a, fmt.Errorf("unknown command %q\nRun 'help' for usage.\n", a[0])
|
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
|
|
|
}
|
|
|
|
|
2013-09-25 00:03:22 +04:00
|
|
|
func (c *Command) Root() *Command {
|
2013-09-24 20:47:29 +04:00
|
|
|
var findRoot func(*Command) *Command
|
|
|
|
|
|
|
|
findRoot = func(x *Command) *Command {
|
|
|
|
if x.HasParent() {
|
|
|
|
return findRoot(x.parent)
|
|
|
|
} else {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
}
|
2013-09-25 00:03:22 +04:00
|
|
|
|
|
|
|
return findRoot(c)
|
|
|
|
}
|
|
|
|
|
2013-09-24 20:47:29 +04:00
|
|
|
// execute the command determined by args and the command tree
|
2013-10-01 05:54:46 +04:00
|
|
|
func (c *Command) findAndExecute(args []string) (err error) {
|
2013-09-24 20:47:29 +04:00
|
|
|
|
2013-10-01 05:54:46 +04:00
|
|
|
cmd, a, e := c.Find(args)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return cmd.execute(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2013-10-01 05:54:46 +04:00
|
|
|
err = c.ParseFlags(a)
|
2015-03-11 20:22:30 +03:00
|
|
|
if err == flag.ErrHelp {
|
|
|
|
c.Help()
|
|
|
|
return nil
|
|
|
|
}
|
2013-10-01 05:54:46 +04:00
|
|
|
if err != nil {
|
2014-12-09 16:42:53 +03:00
|
|
|
// We're writing subcommand usage to root command's error buffer to have it displayed to the user
|
2014-12-09 16:42:53 +03:00
|
|
|
r := c.Root()
|
|
|
|
if r.cmdErrorBuf == nil {
|
|
|
|
r.cmdErrorBuf = new(bytes.Buffer)
|
2014-12-09 16:42:53 +03:00
|
|
|
}
|
|
|
|
// for writing the usage to the buffer we need to switch the output temporarily
|
2014-12-09 16:42:53 +03:00
|
|
|
// since Out() returns root output, you also need to revert that on root
|
|
|
|
out := r.Out()
|
|
|
|
r.SetOutput(r.cmdErrorBuf)
|
2014-12-09 16:42:53 +03:00
|
|
|
c.Usage()
|
2014-12-09 16:42:53 +03:00
|
|
|
r.SetOutput(out)
|
2013-10-01 05:54:46 +04:00
|
|
|
return err
|
|
|
|
} else {
|
2014-12-11 20:06:11 +03:00
|
|
|
// If help is called, regardless of other flags, we print that.
|
|
|
|
// Print help also if c.Run is nil.
|
|
|
|
if c.helpFlagVal || !c.Runnable() {
|
2014-06-17 20:28:42 +04:00
|
|
|
c.Help()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-27 20:27:46 +04:00
|
|
|
c.preRun()
|
2013-10-01 05:54:46 +04:00
|
|
|
argWoFlags := c.Flags().Args()
|
|
|
|
c.Run(c, argWoFlags)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-06-17 20:32:27 +04:00
|
|
|
func (c *Command) errorMsgFromParse() string {
|
|
|
|
s := c.flagErrorBuf.String()
|
|
|
|
|
|
|
|
x := strings.Split(s, "\n")
|
|
|
|
|
|
|
|
if len(x) > 0 {
|
|
|
|
return x[0]
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-02 02:36:24 +04:00
|
|
|
// Call execute to use the args (os.Args[1:] by default)
|
|
|
|
// and run through the command tree finding appropriate matches
|
|
|
|
// for commands and then corresponding flags.
|
|
|
|
func (c *Command) Execute() (err error) {
|
|
|
|
|
|
|
|
// Regardless of what command execute is called on, run on Root only
|
|
|
|
if c.HasParent() {
|
|
|
|
return c.Root().Execute()
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize help as the last point possible to allow for user
|
|
|
|
// overriding
|
|
|
|
c.initHelp()
|
|
|
|
|
|
|
|
var args []string
|
|
|
|
|
|
|
|
if len(c.args) == 0 {
|
|
|
|
args = os.Args[1:]
|
|
|
|
} else {
|
|
|
|
args = c.args
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
// Only the executable is called and the root is runnable, run it
|
|
|
|
if c.Runnable() {
|
|
|
|
err = c.execute([]string(nil))
|
|
|
|
} else {
|
2014-09-03 12:24:07 +04:00
|
|
|
c.Help()
|
2013-11-02 02:36:24 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = c.findAndExecute(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now handle the case where the root is runnable and only flags are provided
|
|
|
|
if err != nil && c.Runnable() {
|
2014-06-27 20:27:46 +04:00
|
|
|
// This is pretty much a custom version of the *Command.execute method
|
|
|
|
// with a few differences because it's the final command (no fall back)
|
2013-11-02 02:36:24 +04:00
|
|
|
e := c.ParseFlags(args)
|
|
|
|
if e != nil {
|
2014-06-14 04:00:56 +04:00
|
|
|
// Flags parsing had an error.
|
2014-06-27 20:27:46 +04:00
|
|
|
// If an error happens here, we have to report it to the user
|
2015-03-11 20:22:30 +03:00
|
|
|
c.Println(e.Error())
|
2014-12-09 16:42:53 +03:00
|
|
|
// If an error happens search also for subcommand info about that
|
|
|
|
if c.cmdErrorBuf != nil && c.cmdErrorBuf.Len() > 0 {
|
|
|
|
c.Println(c.cmdErrorBuf.String())
|
|
|
|
} else {
|
|
|
|
c.Usage()
|
|
|
|
}
|
2015-03-11 20:22:30 +03:00
|
|
|
err = e
|
|
|
|
return
|
2013-11-02 02:36:24 +04:00
|
|
|
} else {
|
2014-06-17 20:28:42 +04:00
|
|
|
// If help is called, regardless of other flags, we print that
|
|
|
|
if c.helpFlagVal {
|
|
|
|
c.Help()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-02 02:36:24 +04:00
|
|
|
argWoFlags := c.Flags().Args()
|
|
|
|
if len(argWoFlags) > 0 {
|
2014-06-27 20:27:46 +04:00
|
|
|
// If there are arguments (not flags) one of the earlier
|
|
|
|
// cases should have caught it.. It means invalid usage
|
|
|
|
// print the usage
|
2013-11-02 02:36:24 +04:00
|
|
|
c.Usage()
|
|
|
|
} else {
|
2014-06-27 20:27:46 +04:00
|
|
|
// Only flags left... Call root.Run
|
|
|
|
c.preRun()
|
2013-11-02 02:36:24 +04:00
|
|
|
c.Run(c, argWoFlags)
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2015-03-11 20:22:30 +03:00
|
|
|
if err == flag.ErrHelp {
|
|
|
|
c.Help()
|
|
|
|
} else {
|
|
|
|
c.Println("Error:", err.Error())
|
|
|
|
c.Printf("%v: invalid command %#q\n", c.Root().Name(), os.Args[1:])
|
|
|
|
c.Printf("Run '%v help' for usage\n", c.Root().Name())
|
|
|
|
}
|
2013-11-02 02:36:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) initHelp() {
|
|
|
|
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.`,
|
|
|
|
Run: c.HelpFunc(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.AddCommand(c.helpCommand)
|
|
|
|
}
|
|
|
|
|
2013-09-24 20:47:29 +04:00
|
|
|
// Used for testing
|
|
|
|
func (c *Command) ResetCommands() {
|
|
|
|
c.commands = nil
|
2014-11-12 07:32:54 +03:00
|
|
|
c.helpCommand = nil
|
2014-12-09 16:42:53 +03:00
|
|
|
c.cmdErrorBuf = new(bytes.Buffer)
|
|
|
|
c.cmdErrorBuf.Reset()
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
|
|
|
|
2014-12-13 08:05:43 +03:00
|
|
|
//Commands returns a slice of child commands.
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) Commands() []*Command {
|
|
|
|
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
|
|
|
|
}
|
2013-09-24 20:47:29 +04:00
|
|
|
c.commands = append(c.commands, x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-03 09:50:53 +03:00
|
|
|
// AddCommand removes one or more commands from a parent command.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-24 20:47:29 +04:00
|
|
|
// Convenience method to Print to the defined output
|
|
|
|
func (c *Command) Print(i ...interface{}) {
|
2013-11-02 02:36:24 +04:00
|
|
|
fmt.Fprint(c.Out(), i...)
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience method to Println to the defined output
|
|
|
|
func (c *Command) Println(i ...interface{}) {
|
|
|
|
str := fmt.Sprintln(i...)
|
|
|
|
c.Print(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience method to Printf to the defined output
|
|
|
|
func (c *Command) Printf(format string, i ...interface{}) {
|
|
|
|
str := fmt.Sprintf(format, i...)
|
|
|
|
c.Print(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output the usage for the command
|
|
|
|
// Used when a user provides invalid input
|
2013-11-02 02:36:24 +04:00
|
|
|
// Can be defined by user by overriding UsageFunc
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) Usage() error {
|
2013-09-29 10:02:49 +04:00
|
|
|
c.mergePersistentFlags()
|
2013-11-02 02:36:24 +04:00
|
|
|
err := c.UsageFunc()(c)
|
2013-09-24 20:47:29 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-09-29 10:02:49 +04:00
|
|
|
// Output the help for the command
|
|
|
|
// Used when a user calls help [command]
|
|
|
|
// by the default HelpFunc in the commander
|
|
|
|
func (c *Command) Help() error {
|
|
|
|
c.mergePersistentFlags()
|
2013-11-02 02:36:24 +04:00
|
|
|
err := tmpl(c.Out(), c.HelpTemplate(), c)
|
2013-09-29 10:02:49 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-09-29 06:42:30 +04:00
|
|
|
func (c *Command) UsageString() string {
|
2013-11-02 02:36:24 +04:00
|
|
|
tmpOutput := c.output
|
2013-09-29 06:42:30 +04:00
|
|
|
bb := new(bytes.Buffer)
|
2013-11-02 02:36:24 +04:00
|
|
|
c.SetOutput(bb)
|
2013-09-29 06:42:30 +04:00
|
|
|
c.Usage()
|
2013-11-02 02:36:24 +04:00
|
|
|
c.output = tmpOutput
|
2013-09-29 06:42:30 +04:00
|
|
|
return bb.String()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
//The full usage for a given command (including parents)
|
|
|
|
func (c *Command) UseLine() string {
|
|
|
|
str := ""
|
|
|
|
if c.HasParent() {
|
|
|
|
str = c.parent.CommandPath() + " "
|
|
|
|
}
|
|
|
|
return str + c.Use
|
|
|
|
}
|
|
|
|
|
|
|
|
// For use in determining which flags have been assigned to which commands
|
|
|
|
// and which persist
|
|
|
|
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) {
|
|
|
|
if x.HasPersistentFlags() {
|
|
|
|
if x.persistentFlag(f.Name) == nil {
|
|
|
|
c.Println(" -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, " [L]")
|
|
|
|
} else {
|
|
|
|
c.Println(" -"+f.Shorthand+",", "--"+f.Name, "["+f.DefValue+"]", "", f.Value, " [LP]")
|
|
|
|
}
|
|
|
|
} 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]")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
c.Println(x.flagErrorBuf)
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2014-09-26 03:03:34 +04:00
|
|
|
// Determine if a given string is an alias of the command.
|
|
|
|
func (c *Command) HasAlias(s string) bool {
|
|
|
|
for _, a := range c.Aliases {
|
|
|
|
if a == s {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) NameAndAliases() string {
|
|
|
|
return strings.Join(append([]string{c.Name()}, c.Aliases...), ", ")
|
|
|
|
}
|
|
|
|
|
2015-02-13 02:12:07 +03:00
|
|
|
func (c *Command) HasExample() bool {
|
|
|
|
return len(c.Example) > 0
|
|
|
|
}
|
|
|
|
|
2013-09-24 20:47:29 +04:00
|
|
|
// Determine if the command is itself runnable
|
|
|
|
func (c *Command) Runnable() bool {
|
|
|
|
return c.Run != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if the command has children commands
|
|
|
|
func (c *Command) HasSubCommands() bool {
|
|
|
|
return len(c.commands) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if the command is a child command
|
|
|
|
func (c *Command) HasParent() bool {
|
|
|
|
return c.parent != nil
|
|
|
|
}
|
|
|
|
|
2015-01-29 23:55:29 +03:00
|
|
|
// Get the complete FlagSet that applies 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)
|
|
|
|
if c.flagErrorBuf == nil {
|
|
|
|
c.flagErrorBuf = new(bytes.Buffer)
|
|
|
|
}
|
|
|
|
c.flags.SetOutput(c.flagErrorBuf)
|
2014-09-13 00:43:50 +04:00
|
|
|
c.PersistentFlags().BoolVarP(&c.helpFlagVal, "help", "h", false, "help for "+c.Name())
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
|
|
|
return c.flags
|
|
|
|
}
|
|
|
|
|
2015-01-29 23:55:29 +03:00
|
|
|
// Get the local FlagSet specifically set in the current command
|
|
|
|
func (c *Command) LocalFlags() *flag.FlagSet {
|
|
|
|
c.mergePersistentFlags()
|
|
|
|
|
|
|
|
local := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
2015-02-28 03:26:56 +03:00
|
|
|
c.lflags.VisitAll(func(f *flag.Flag) {
|
|
|
|
local.AddFlag(f)
|
2015-01-29 23:55:29 +03:00
|
|
|
})
|
|
|
|
return local
|
|
|
|
}
|
|
|
|
|
2015-02-14 03:44:12 +03:00
|
|
|
// All Flags which were inherited from parents commands
|
|
|
|
func (c *Command) InheritedFlags() *flag.FlagSet {
|
|
|
|
c.mergePersistentFlags()
|
|
|
|
|
2015-02-28 03:26:56 +03:00
|
|
|
inherited := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
|
|
|
local := c.LocalFlags()
|
|
|
|
|
|
|
|
var rmerge func(x *Command)
|
2015-02-14 03:44:12 +03:00
|
|
|
|
2015-02-28 03:26:56 +03:00
|
|
|
rmerge = func(x *Command) {
|
|
|
|
if x.HasPersistentFlags() {
|
|
|
|
x.PersistentFlags().VisitAll(func(f *flag.Flag) {
|
|
|
|
if inherited.Lookup(f.Name) == nil && local.Lookup(f.Name) == nil {
|
|
|
|
inherited.AddFlag(f)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if x.HasParent() {
|
|
|
|
rmerge(x.parent)
|
|
|
|
}
|
|
|
|
}
|
2015-02-14 03:44:12 +03:00
|
|
|
|
|
|
|
if c.HasParent() {
|
|
|
|
rmerge(c.parent)
|
|
|
|
}
|
|
|
|
|
2015-02-28 03:26:56 +03:00
|
|
|
return inherited
|
2015-02-14 03:44:12 +03:00
|
|
|
}
|
|
|
|
|
2015-02-14 03:45:00 +03:00
|
|
|
// All Flags which were not inherited from parent commands
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-01-29 23:55:29 +03:00
|
|
|
// Get 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)
|
|
|
|
if c.flagErrorBuf == nil {
|
|
|
|
c.flagErrorBuf = new(bytes.Buffer)
|
|
|
|
}
|
|
|
|
c.pflags.SetOutput(c.flagErrorBuf)
|
|
|
|
}
|
|
|
|
return c.pflags
|
|
|
|
}
|
|
|
|
|
|
|
|
// For use in testing
|
|
|
|
func (c *Command) ResetFlags() {
|
|
|
|
c.flagErrorBuf = new(bytes.Buffer)
|
|
|
|
c.flagErrorBuf.Reset()
|
|
|
|
c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
|
|
|
c.flags.SetOutput(c.flagErrorBuf)
|
|
|
|
c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
|
|
|
c.pflags.SetOutput(c.flagErrorBuf)
|
|
|
|
}
|
|
|
|
|
2015-01-29 23:55:29 +03:00
|
|
|
// Does the command contain 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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does the command contain persistent flags
|
|
|
|
func (c *Command) HasPersistentFlags() bool {
|
|
|
|
return c.PersistentFlags().HasFlags()
|
|
|
|
}
|
|
|
|
|
2015-01-29 23:55:29 +03:00
|
|
|
// Does the command has flags specifically declared locally
|
|
|
|
func (c *Command) HasLocalFlags() bool {
|
|
|
|
return c.LocalFlags().HasFlags()
|
|
|
|
}
|
|
|
|
|
2015-02-28 03:26:56 +03:00
|
|
|
func (c *Command) HasInheritedFlags() bool {
|
|
|
|
return c.InheritedFlags().HasFlags()
|
|
|
|
}
|
|
|
|
|
2013-09-24 20:47:29 +04:00
|
|
|
// Climbs up the command tree looking for matching flag
|
|
|
|
func (c *Command) Flag(name string) (flag *flag.Flag) {
|
|
|
|
flag = c.Flags().Lookup(name)
|
|
|
|
|
|
|
|
if flag == nil {
|
|
|
|
flag = c.persistentFlag(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// recursively find matching persistent flag
|
|
|
|
func (c *Command) persistentFlag(name string) (flag *flag.Flag) {
|
|
|
|
if c.HasPersistentFlags() {
|
|
|
|
flag = c.PersistentFlags().Lookup(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if flag == nil && c.HasParent() {
|
|
|
|
flag = c.parent.persistentFlag(name)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parses persistent flag tree & local flags
|
|
|
|
func (c *Command) ParseFlags(args []string) (err error) {
|
|
|
|
c.mergePersistentFlags()
|
|
|
|
err = c.Flags().Parse(args)
|
2015-03-11 20:22:30 +03:00
|
|
|
return
|
2013-09-24 20:47:29 +04:00
|
|
|
}
|
|
|
|
|
2015-01-29 23:55:29 +03:00
|
|
|
func (c *Command) Parent() *Command {
|
|
|
|
return c.parent
|
|
|
|
}
|
|
|
|
|
2013-09-24 20:47:29 +04:00
|
|
|
func (c *Command) mergePersistentFlags() {
|
|
|
|
var rmerge func(x *Command)
|
|
|
|
|
2015-02-28 03:26:56 +03:00
|
|
|
// Save the set of local flags
|
|
|
|
if c.lflags == nil {
|
|
|
|
c.lflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
|
|
|
if c.flagErrorBuf == nil {
|
|
|
|
c.flagErrorBuf = new(bytes.Buffer)
|
|
|
|
}
|
|
|
|
c.lflags.SetOutput(c.flagErrorBuf)
|
|
|
|
addtolocal := func(f *flag.Flag) {
|
|
|
|
c.lflags.AddFlag(f)
|
|
|
|
}
|
|
|
|
c.Flags().VisitAll(addtolocal)
|
|
|
|
c.PersistentFlags().VisitAll(addtolocal)
|
|
|
|
}
|
2013-09-24 20:47:29 +04:00
|
|
|
rmerge = func(x *Command) {
|
|
|
|
if x.HasPersistentFlags() {
|
|
|
|
x.PersistentFlags().VisitAll(func(f *flag.Flag) {
|
|
|
|
if c.Flags().Lookup(f.Name) == nil {
|
|
|
|
c.Flags().AddFlag(f)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if x.HasParent() {
|
|
|
|
rmerge(x.parent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rmerge(c)
|
|
|
|
}
|