2019-04-25 17:06:53 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2020-01-04 04:45:36 +00:00
|
|
|
"flag"
|
2019-04-25 17:06:53 +00:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Command is a subcommand for a cli.App.
|
|
|
|
type Command struct {
|
|
|
|
// The name of the command
|
|
|
|
Name string
|
|
|
|
// A list of aliases for the command
|
|
|
|
Aliases []string
|
|
|
|
// A short description of the usage of this command
|
|
|
|
Usage string
|
|
|
|
// Custom text to show on USAGE section of help
|
|
|
|
UsageText string
|
|
|
|
// A longer explanation of how the command works
|
|
|
|
Description string
|
|
|
|
// A short description of the arguments of this command
|
|
|
|
ArgsUsage string
|
|
|
|
// The category the command is part of
|
|
|
|
Category string
|
|
|
|
// The function to call when checking for bash command completions
|
|
|
|
BashComplete BashCompleteFunc
|
|
|
|
// An action to execute before any sub-subcommands are run, but after the context is ready
|
|
|
|
// If a non-nil error is returned, no sub-subcommands are run
|
|
|
|
Before BeforeFunc
|
|
|
|
// An action to execute after any subcommands are run, but after the subcommand has finished
|
|
|
|
// It is run even if Action() panics
|
|
|
|
After AfterFunc
|
|
|
|
// The function to call when this command is invoked
|
2020-01-04 17:44:25 +00:00
|
|
|
Action ActionFunc
|
2019-04-25 17:06:53 +00:00
|
|
|
// Execute this function if a usage error occurs.
|
|
|
|
OnUsageError OnUsageErrorFunc
|
|
|
|
// List of child commands
|
2020-01-04 17:44:25 +00:00
|
|
|
Subcommands []*Command
|
2019-04-25 17:06:53 +00:00
|
|
|
// List of flags to parse
|
|
|
|
Flags []Flag
|
|
|
|
// Treat all flags as normal arguments if true
|
|
|
|
SkipFlagParsing bool
|
|
|
|
// Boolean to hide built-in help command
|
|
|
|
HideHelp bool
|
|
|
|
// Boolean to hide this command from help or completion
|
|
|
|
Hidden bool
|
2020-01-04 04:45:36 +00:00
|
|
|
// Boolean to enable short-option handling so user can combine several
|
|
|
|
// single-character bool arguments into one
|
|
|
|
// i.e. foobar -o -v -> foobar -ov
|
|
|
|
UseShortOptionHandling bool
|
2019-04-25 17:06:53 +00:00
|
|
|
|
|
|
|
// Full name of command for help, defaults to full command name, including parent commands.
|
|
|
|
HelpName string
|
|
|
|
commandNamePath []string
|
|
|
|
|
|
|
|
// CustomHelpTemplate the text template for the command help topic.
|
|
|
|
// cli.go uses text/template to render templates. You can
|
|
|
|
// render custom help text by setting this variable.
|
|
|
|
CustomHelpTemplate string
|
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
type Commands []*Command
|
|
|
|
|
|
|
|
type CommandsByName []*Command
|
2019-04-25 17:06:53 +00:00
|
|
|
|
|
|
|
func (c CommandsByName) Len() int {
|
|
|
|
return len(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c CommandsByName) Less(i, j int) bool {
|
2020-01-04 04:45:36 +00:00
|
|
|
return lexicographicLess(c[i].Name, c[j].Name)
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c CommandsByName) Swap(i, j int) {
|
|
|
|
c[i], c[j] = c[j], c[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// FullName returns the full name of the command.
|
|
|
|
// For subcommands this ensures that parent commands are part of the command path
|
2020-01-04 17:44:25 +00:00
|
|
|
func (c *Command) FullName() string {
|
2019-04-25 17:06:53 +00:00
|
|
|
if c.commandNamePath == nil {
|
|
|
|
return c.Name
|
|
|
|
}
|
|
|
|
return strings.Join(c.commandNamePath, " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run invokes the command given the context, parses ctx.Args() to generate command-specific flags
|
2020-01-04 17:44:25 +00:00
|
|
|
func (c *Command) Run(ctx *Context) (err error) {
|
2019-04-25 17:06:53 +00:00
|
|
|
if len(c.Subcommands) > 0 {
|
|
|
|
return c.startApp(ctx)
|
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
if !c.HideHelp && HelpFlag != nil {
|
2019-04-25 17:06:53 +00:00
|
|
|
// append help to flags
|
2020-01-04 17:44:25 +00:00
|
|
|
c.appendFlag(HelpFlag)
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 04:45:36 +00:00
|
|
|
if ctx.App.UseShortOptionHandling {
|
|
|
|
c.UseShortOptionHandling = true
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
set, err := c.parseFlags(ctx.Args(), ctx.shellComplete)
|
2019-04-25 17:06:53 +00:00
|
|
|
|
|
|
|
context := NewContext(ctx.App, set, ctx)
|
|
|
|
context.Command = c
|
|
|
|
if checkCommandCompletions(context, c.Name) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if c.OnUsageError != nil {
|
2020-01-04 17:44:25 +00:00
|
|
|
err = c.OnUsageError(context, err, false)
|
2020-01-04 04:45:36 +00:00
|
|
|
context.App.handleExitCoder(context, err)
|
2019-04-25 17:06:53 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-01-04 04:45:36 +00:00
|
|
|
_, _ = fmt.Fprintln(context.App.Writer, "Incorrect Usage:", err.Error())
|
|
|
|
_, _ = fmt.Fprintln(context.App.Writer)
|
|
|
|
_ = ShowCommandHelp(context, c.Name)
|
2019-04-25 17:06:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if checkCommandHelp(context, c.Name) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-04 04:45:36 +00:00
|
|
|
cerr := checkRequiredFlags(c.Flags, context)
|
|
|
|
if cerr != nil {
|
|
|
|
_ = ShowCommandHelp(context, c.Name)
|
|
|
|
return cerr
|
|
|
|
}
|
|
|
|
|
2019-04-25 17:06:53 +00:00
|
|
|
if c.After != nil {
|
|
|
|
defer func() {
|
|
|
|
afterErr := c.After(context)
|
|
|
|
if afterErr != nil {
|
2020-01-04 04:45:36 +00:00
|
|
|
context.App.handleExitCoder(context, err)
|
2019-04-25 17:06:53 +00:00
|
|
|
if err != nil {
|
2020-01-04 17:44:25 +00:00
|
|
|
err = newMultiError(err, afterErr)
|
2019-04-25 17:06:53 +00:00
|
|
|
} else {
|
|
|
|
err = afterErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Before != nil {
|
|
|
|
err = c.Before(context)
|
|
|
|
if err != nil {
|
2020-01-04 04:45:36 +00:00
|
|
|
_ = ShowCommandHelp(context, c.Name)
|
|
|
|
context.App.handleExitCoder(context, err)
|
2019-04-25 17:06:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Action == nil {
|
|
|
|
c.Action = helpSubcommand.Action
|
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
context.Command = c
|
|
|
|
err = c.Action(context)
|
2019-04-25 17:06:53 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2020-01-04 04:45:36 +00:00
|
|
|
context.App.handleExitCoder(context, err)
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
func (c *Command) newFlagSet() (*flag.FlagSet, error) {
|
|
|
|
return flagSet(c.Name, c.Flags)
|
|
|
|
}
|
2020-01-04 04:45:36 +00:00
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
func (c *Command) useShortOptionHandling() bool {
|
|
|
|
return c.UseShortOptionHandling
|
|
|
|
}
|
2020-01-04 04:45:36 +00:00
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
func (c *Command) parseFlags(args Args, shellComplete bool) (*flag.FlagSet, error) {
|
2020-01-04 04:45:36 +00:00
|
|
|
set, err := c.newFlagSet()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
if c.SkipFlagParsing {
|
|
|
|
return set, set.Parse(append([]string{"--"}, args.Tail()...))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = parseIter(set, c, args.Tail(), shellComplete)
|
2020-01-04 04:45:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = normalizeFlags(c.Flags, set)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return set, nil
|
|
|
|
}
|
|
|
|
|
2019-04-25 17:06:53 +00:00
|
|
|
// Names returns the names including short names and aliases.
|
2020-01-04 17:44:25 +00:00
|
|
|
func (c *Command) Names() []string {
|
|
|
|
return append([]string{c.Name}, c.Aliases...)
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
// HasName returns true if Command.Name matches given name
|
|
|
|
func (c *Command) HasName(name string) bool {
|
2019-04-25 17:06:53 +00:00
|
|
|
for _, n := range c.Names() {
|
|
|
|
if n == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
func (c *Command) startApp(ctx *Context) error {
|
|
|
|
app := &App{
|
|
|
|
Metadata: ctx.App.Metadata,
|
|
|
|
Name: fmt.Sprintf("%s %s", ctx.App.Name, c.Name),
|
|
|
|
}
|
|
|
|
|
2019-04-25 17:06:53 +00:00
|
|
|
if c.HelpName == "" {
|
|
|
|
app.HelpName = c.HelpName
|
|
|
|
} else {
|
|
|
|
app.HelpName = app.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Usage = c.Usage
|
|
|
|
app.Description = c.Description
|
|
|
|
app.ArgsUsage = c.ArgsUsage
|
|
|
|
|
|
|
|
// set CommandNotFound
|
|
|
|
app.CommandNotFound = ctx.App.CommandNotFound
|
|
|
|
app.CustomAppHelpTemplate = c.CustomHelpTemplate
|
|
|
|
|
|
|
|
// set the flags and commands
|
|
|
|
app.Commands = c.Subcommands
|
|
|
|
app.Flags = c.Flags
|
|
|
|
app.HideHelp = c.HideHelp
|
|
|
|
|
|
|
|
app.Version = ctx.App.Version
|
|
|
|
app.HideVersion = ctx.App.HideVersion
|
|
|
|
app.Compiled = ctx.App.Compiled
|
|
|
|
app.Writer = ctx.App.Writer
|
|
|
|
app.ErrWriter = ctx.App.ErrWriter
|
2020-01-04 17:44:25 +00:00
|
|
|
app.ExitErrHandler = ctx.App.ExitErrHandler
|
2020-01-04 04:45:36 +00:00
|
|
|
app.UseShortOptionHandling = ctx.App.UseShortOptionHandling
|
2019-04-25 17:06:53 +00:00
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
app.categories = newCommandCategories()
|
2019-04-25 17:06:53 +00:00
|
|
|
for _, command := range c.Subcommands {
|
2020-01-04 17:44:25 +00:00
|
|
|
app.categories.AddCommand(command.Category, command)
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
sort.Sort(app.categories.(*commandCategories))
|
2019-04-25 17:06:53 +00:00
|
|
|
|
|
|
|
// bash completion
|
|
|
|
app.EnableBashCompletion = ctx.App.EnableBashCompletion
|
|
|
|
if c.BashComplete != nil {
|
|
|
|
app.BashComplete = c.BashComplete
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the actions
|
|
|
|
app.Before = c.Before
|
|
|
|
app.After = c.After
|
|
|
|
if c.Action != nil {
|
|
|
|
app.Action = c.Action
|
|
|
|
} else {
|
|
|
|
app.Action = helpSubcommand.Action
|
|
|
|
}
|
|
|
|
app.OnUsageError = c.OnUsageError
|
|
|
|
|
|
|
|
for index, cc := range app.Commands {
|
|
|
|
app.Commands[index].commandNamePath = []string{c.Name, cc.Name}
|
|
|
|
}
|
|
|
|
|
|
|
|
return app.RunAsSubcommand(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// VisibleFlags returns a slice of the Flags with Hidden=false
|
2020-01-04 17:44:25 +00:00
|
|
|
func (c *Command) VisibleFlags() []Flag {
|
2019-04-25 17:06:53 +00:00
|
|
|
return visibleFlags(c.Flags)
|
|
|
|
}
|
2020-01-04 17:44:25 +00:00
|
|
|
|
|
|
|
func (c *Command) appendFlag(fl Flag) {
|
|
|
|
if !hasFlag(c.Flags, fl) {
|
|
|
|
c.Flags = append(c.Flags, fl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasCommand(commands []*Command, command *Command) bool {
|
|
|
|
for _, existing := range commands {
|
|
|
|
if command == existing {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|