2013-07-19 00:29:06 +00:00
|
|
|
package cli
|
|
|
|
|
2013-09-04 19:58:31 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2013-09-18 16:24:20 +00:00
|
|
|
"io/ioutil"
|
2016-03-20 19:17:13 +00:00
|
|
|
"sort"
|
2013-09-18 16:24:20 +00:00
|
|
|
"strings"
|
2013-09-04 19:58:31 +00:00
|
|
|
)
|
|
|
|
|
2013-11-01 14:31:37 +00:00
|
|
|
// Command is a subcommand for a cli.App.
|
2013-07-19 00:29:06 +00:00
|
|
|
type Command struct {
|
2013-11-01 14:31:37 +00:00
|
|
|
// The name of the command
|
|
|
|
Name string
|
2015-03-10 04:24:57 +00:00
|
|
|
// A list of aliases for the command
|
|
|
|
Aliases []string
|
2013-11-01 14:31:37 +00:00
|
|
|
// A short description of the usage of this command
|
|
|
|
Usage string
|
2015-10-25 05:37:21 +00:00
|
|
|
// Custom text to show on USAGE section of help
|
|
|
|
UsageText string
|
2014-04-16 16:18:00 +00:00
|
|
|
// A longer explanation of how the command works
|
2013-09-24 01:41:31 +00:00
|
|
|
Description string
|
2015-08-03 23:51:11 +00:00
|
|
|
// A short description of the arguments of this command
|
|
|
|
ArgsUsage string
|
2014-12-15 22:35:49 +00:00
|
|
|
// The category the command is part of
|
|
|
|
Category string
|
2016-07-22 08:19:29 +00:00
|
|
|
// The function to call when checking for shell command completions
|
|
|
|
ShellComplete ShellCompleteFunc
|
2014-04-16 20:26:28 +00:00
|
|
|
// 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
|
2016-04-25 22:29:05 +00:00
|
|
|
Before BeforeFunc
|
2014-11-18 22:44:21 +00:00
|
|
|
// An action to execute after any subcommands are run, but after the subcommand has finished
|
2015-02-20 21:21:27 +00:00
|
|
|
// It is run even if Action() panics
|
2016-04-25 22:29:05 +00:00
|
|
|
After AfterFunc
|
2013-11-01 14:31:37 +00:00
|
|
|
// The function to call when this command is invoked
|
2016-05-17 19:11:44 +00:00
|
|
|
Action ActionFunc
|
2016-04-25 22:29:05 +00:00
|
|
|
// Execute this function if a usage error occurs.
|
|
|
|
OnUsageError OnUsageErrorFunc
|
2014-04-16 16:18:00 +00:00
|
|
|
// List of child commands
|
2016-05-22 19:20:52 +00:00
|
|
|
Subcommands []*Command
|
2014-04-16 20:26:28 +00:00
|
|
|
// List of flags to parse
|
|
|
|
Flags []Flag
|
2014-04-14 21:44:32 +00:00
|
|
|
// Treat all flags as normal arguments if true
|
|
|
|
SkipFlagParsing bool
|
2014-07-13 13:16:30 +00:00
|
|
|
// Boolean to hide built-in help command
|
|
|
|
HideHelp bool
|
2015-02-06 08:46:32 +00:00
|
|
|
// Boolean to hide this command from help or completion
|
|
|
|
Hidden bool
|
2015-06-20 23:59:54 +00:00
|
|
|
|
2015-08-13 05:14:26 +00:00
|
|
|
// Full name of command for help, defaults to full command name, including parent commands.
|
2015-08-13 04:43:14 +00:00
|
|
|
HelpName string
|
2015-06-20 23:59:54 +00:00
|
|
|
commandNamePath []string
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// FullName returns the full name of the command.
|
2015-06-20 23:59:54 +00:00
|
|
|
// For subcommands this ensures that parent commands are part of the command path
|
2016-05-22 19:20:52 +00:00
|
|
|
func (c *Command) FullName() string {
|
2015-06-20 23:59:54 +00:00
|
|
|
if c.commandNamePath == nil {
|
|
|
|
return c.Name
|
|
|
|
}
|
|
|
|
return strings.Join(c.commandNamePath, " ")
|
2013-07-19 00:29:06 +00:00
|
|
|
}
|
2013-07-19 02:23:00 +00:00
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// Run invokes the command given the context, parses ctx.Args() to generate command-specific flags
|
2016-05-22 19:20:52 +00:00
|
|
|
func (c *Command) Run(ctx *Context) (err error) {
|
2015-12-25 20:45:58 +00:00
|
|
|
if len(c.Subcommands) > 0 {
|
2014-04-16 16:18:00 +00:00
|
|
|
return c.startApp(ctx)
|
|
|
|
}
|
|
|
|
|
2016-05-22 19:20:52 +00:00
|
|
|
if !c.HideHelp && HelpFlag != nil {
|
2014-07-13 13:16:30 +00:00
|
|
|
// append help to flags
|
2016-05-22 19:20:52 +00:00
|
|
|
c.appendFlag(HelpFlag)
|
2014-07-13 13:16:30 +00:00
|
|
|
}
|
2013-08-14 04:40:39 +00:00
|
|
|
|
2016-07-22 08:19:29 +00:00
|
|
|
if ctx.App.EnableShellCompletion {
|
|
|
|
c.appendFlag(GenerateCompletionFlag)
|
2014-04-12 13:32:53 +00:00
|
|
|
}
|
|
|
|
|
2013-07-20 15:21:20 +00:00
|
|
|
set := flagSet(c.Name, c.Flags)
|
2013-09-04 19:58:31 +00:00
|
|
|
set.SetOutput(ioutil.Discard)
|
2013-09-18 16:24:20 +00:00
|
|
|
|
2016-05-07 23:36:54 +00:00
|
|
|
if c.SkipFlagParsing {
|
|
|
|
err = set.Parse(append([]string{"--"}, ctx.Args().Tail()...))
|
2015-10-27 04:40:03 +00:00
|
|
|
} else {
|
2016-05-07 23:36:54 +00:00
|
|
|
err = set.Parse(ctx.Args().Tail())
|
2013-09-18 16:24:20 +00:00
|
|
|
}
|
2013-09-04 19:58:31 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2016-01-23 11:47:24 +00:00
|
|
|
if c.OnUsageError != nil {
|
2016-04-25 22:29:05 +00:00
|
|
|
err := c.OnUsageError(ctx, err, false)
|
2016-04-27 13:18:42 +00:00
|
|
|
HandleExitCoder(err)
|
2016-04-27 13:12:34 +00:00
|
|
|
return err
|
2016-01-23 11:47:24 +00:00
|
|
|
}
|
2016-05-24 02:03:19 +00:00
|
|
|
fmt.Fprintf(ctx.App.Writer, "Incorrect Usage: %s\n\n", err)
|
2016-05-09 14:15:05 +00:00
|
|
|
fmt.Fprintln(ctx.App.Writer)
|
|
|
|
ShowCommandHelp(ctx, c.Name)
|
|
|
|
return err
|
2013-09-04 19:58:31 +00:00
|
|
|
}
|
2013-08-14 04:40:39 +00:00
|
|
|
|
2013-11-20 08:05:18 +00:00
|
|
|
nerr := normalizeFlags(c.Flags, set)
|
|
|
|
if nerr != nil {
|
2014-12-02 04:57:35 +00:00
|
|
|
fmt.Fprintln(ctx.App.Writer, nerr)
|
|
|
|
fmt.Fprintln(ctx.App.Writer)
|
2013-11-20 08:05:18 +00:00
|
|
|
ShowCommandHelp(ctx, c.Name)
|
2016-04-27 13:12:34 +00:00
|
|
|
return nerr
|
2013-11-20 08:05:18 +00:00
|
|
|
}
|
2016-04-25 22:10:10 +00:00
|
|
|
|
2015-05-18 15:39:48 +00:00
|
|
|
context := NewContext(ctx.App, set, ctx)
|
2014-04-10 17:14:13 +00:00
|
|
|
|
|
|
|
if checkCommandCompletions(context, c.Name) {
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-04-10 17:14:13 +00:00
|
|
|
}
|
|
|
|
|
2013-11-01 13:45:19 +00:00
|
|
|
if checkCommandHelp(context, c.Name) {
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2013-11-01 13:45:19 +00:00
|
|
|
}
|
2015-12-25 20:45:58 +00:00
|
|
|
|
|
|
|
if c.After != nil {
|
|
|
|
defer func() {
|
2016-04-27 13:12:34 +00:00
|
|
|
afterErr := c.After(context)
|
2015-12-25 20:45:58 +00:00
|
|
|
if afterErr != nil {
|
2016-04-27 13:18:42 +00:00
|
|
|
HandleExitCoder(err)
|
2015-12-25 20:45:58 +00:00
|
|
|
if err != nil {
|
2016-05-25 16:05:14 +00:00
|
|
|
err = newMultiError(err, afterErr)
|
2015-12-25 20:45:58 +00:00
|
|
|
} else {
|
|
|
|
err = afterErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Before != nil {
|
2016-04-27 13:12:34 +00:00
|
|
|
err = c.Before(context)
|
2015-12-25 20:45:58 +00:00
|
|
|
if err != nil {
|
2015-12-25 21:08:22 +00:00
|
|
|
fmt.Fprintln(ctx.App.Writer, err)
|
|
|
|
fmt.Fprintln(ctx.App.Writer)
|
|
|
|
ShowCommandHelp(ctx, c.Name)
|
2016-04-27 13:18:42 +00:00
|
|
|
HandleExitCoder(err)
|
2016-04-27 13:12:34 +00:00
|
|
|
return err
|
2015-12-25 20:45:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-29 19:02:05 +00:00
|
|
|
context.Command = c
|
2016-05-17 19:11:44 +00:00
|
|
|
err = c.Action(context)
|
2016-04-27 15:34:01 +00:00
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
if err != nil {
|
2016-04-27 13:18:42 +00:00
|
|
|
HandleExitCoder(err)
|
2016-04-27 13:12:34 +00:00
|
|
|
}
|
|
|
|
return err
|
2013-07-19 02:23:00 +00:00
|
|
|
}
|
2013-07-19 02:30:18 +00:00
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// Names returns the names including short names and aliases.
|
2016-05-22 19:20:52 +00:00
|
|
|
func (c *Command) Names() []string {
|
|
|
|
return append([]string{c.Name}, c.Aliases...)
|
2015-03-10 04:24:57 +00:00
|
|
|
}
|
|
|
|
|
2016-05-17 19:11:44 +00:00
|
|
|
// HasName returns true if Command.Name matches given name
|
2016-05-22 19:20:52 +00:00
|
|
|
func (c *Command) HasName(name string) bool {
|
2015-03-10 04:24:57 +00:00
|
|
|
for _, n := range c.Names() {
|
|
|
|
if n == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2013-07-19 02:30:18 +00:00
|
|
|
}
|
2014-04-16 16:18:00 +00:00
|
|
|
|
2016-05-22 19:20:52 +00:00
|
|
|
func (c *Command) startApp(ctx *Context) error {
|
2016-06-22 16:47:57 +00:00
|
|
|
app := &App{
|
|
|
|
Metadata: ctx.App.Metadata,
|
|
|
|
Name: fmt.Sprintf("%s %s", ctx.App.Name, c.Name),
|
|
|
|
}
|
|
|
|
|
2015-08-13 04:58:25 +00:00
|
|
|
if c.HelpName == "" {
|
|
|
|
app.HelpName = c.HelpName
|
|
|
|
} else {
|
2016-02-04 07:25:41 +00:00
|
|
|
app.HelpName = app.Name
|
2015-08-13 04:58:25 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 16:18:00 +00:00
|
|
|
if c.Description != "" {
|
|
|
|
app.Usage = c.Description
|
|
|
|
} else {
|
|
|
|
app.Usage = c.Usage
|
|
|
|
}
|
|
|
|
|
2014-07-25 17:06:04 +00:00
|
|
|
// set CommandNotFound
|
|
|
|
app.CommandNotFound = ctx.App.CommandNotFound
|
|
|
|
|
2014-04-16 16:18:00 +00:00
|
|
|
// set the flags and commands
|
|
|
|
app.Commands = c.Subcommands
|
|
|
|
app.Flags = c.Flags
|
2014-07-13 13:16:30 +00:00
|
|
|
app.HideHelp = c.HideHelp
|
2015-05-04 01:42:21 +00:00
|
|
|
|
|
|
|
app.Version = ctx.App.Version
|
|
|
|
app.HideVersion = ctx.App.HideVersion
|
|
|
|
app.Compiled = ctx.App.Compiled
|
2015-05-04 01:37:51 +00:00
|
|
|
app.Writer = ctx.App.Writer
|
2014-04-16 16:18:00 +00:00
|
|
|
|
2016-05-25 16:05:14 +00:00
|
|
|
app.Categories = newCommandCategories()
|
2016-03-20 19:17:13 +00:00
|
|
|
for _, command := range c.Subcommands {
|
2016-05-25 16:05:14 +00:00
|
|
|
app.Categories.AddCommand(command.Category, command)
|
2016-03-20 19:17:13 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 16:05:14 +00:00
|
|
|
sort.Sort(app.Categories.(*commandCategories))
|
2016-03-20 19:17:13 +00:00
|
|
|
|
2014-04-16 16:18:00 +00:00
|
|
|
// bash completion
|
2016-07-22 08:19:29 +00:00
|
|
|
app.EnableShellCompletion = ctx.App.EnableShellCompletion
|
|
|
|
if c.ShellComplete != nil {
|
|
|
|
app.ShellComplete = c.ShellComplete
|
2014-04-16 16:18:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 20:26:28 +00:00
|
|
|
// set the actions
|
|
|
|
app.Before = c.Before
|
2014-11-18 22:44:21 +00:00
|
|
|
app.After = c.After
|
2014-04-16 16:18:00 +00:00
|
|
|
if c.Action != nil {
|
|
|
|
app.Action = c.Action
|
|
|
|
} else {
|
|
|
|
app.Action = helpSubcommand.Action
|
|
|
|
}
|
|
|
|
|
2016-02-04 07:25:41 +00:00
|
|
|
for index, cc := range app.Commands {
|
|
|
|
app.Commands[index].commandNamePath = []string{c.Name, cc.Name}
|
2015-06-20 23:59:54 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 16:18:00 +00:00
|
|
|
return app.RunAsSubcommand(ctx)
|
|
|
|
}
|
2016-05-01 12:36:17 +00:00
|
|
|
|
|
|
|
// VisibleFlags returns a slice of the Flags with Hidden=false
|
2016-05-22 19:20:52 +00:00
|
|
|
func (c *Command) VisibleFlags() []Flag {
|
2016-05-01 12:36:17 +00:00
|
|
|
return visibleFlags(c.Flags)
|
|
|
|
}
|
2016-05-22 19:20:52 +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
|
|
|
|
}
|