2013-07-19 08:34:01 -07:00
|
|
|
package cli
|
|
|
|
|
|
|
|
|
|
import (
|
2013-09-04 13:58:31 -06:00
|
|
|
"fmt"
|
2014-06-12 00:39:13 -07:00
|
|
|
"io"
|
2013-09-14 15:29:57 -07:00
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
2016-04-03 17:50:08 +08:00
|
|
|
"path/filepath"
|
2016-04-27 11:34:01 -04:00
|
|
|
"reflect"
|
2015-08-21 10:58:14 +02:00
|
|
|
"sort"
|
2013-11-15 12:40:18 +01:00
|
|
|
"time"
|
2013-07-19 08:34:01 -07:00
|
|
|
)
|
|
|
|
|
|
2016-04-28 10:15:04 -04:00
|
|
|
var (
|
2016-05-01 09:09:54 -04:00
|
|
|
changeLogURL = "https://github.com/codegangsta/cli/blob/master/CHANGELOG.md"
|
|
|
|
|
appActionDeprecationURL = fmt.Sprintf("%s#deprecated-cli-app-action-signature", changeLogURL)
|
|
|
|
|
runAndExitOnErrorDeprecationURL = fmt.Sprintf("%s#deprecated-cli-app-runandexitonerror", changeLogURL)
|
2016-04-28 11:03:10 -04:00
|
|
|
|
2016-04-29 03:01:57 -04:00
|
|
|
contactSysadmin = "This is an error in the application. Please contact the distributor of this application if this is not you."
|
|
|
|
|
|
2016-04-28 10:15:04 -04:00
|
|
|
errNonFuncAction = NewExitError("ERROR invalid Action type. "+
|
2016-04-29 03:01:57 -04:00
|
|
|
fmt.Sprintf("Must be a func of type `cli.ActionFunc`. %s", contactSysadmin)+
|
2016-04-28 11:03:10 -04:00
|
|
|
fmt.Sprintf("See %s", appActionDeprecationURL), 2)
|
2016-04-28 10:15:04 -04:00
|
|
|
errInvalidActionSignature = NewExitError("ERROR invalid Action signature. "+
|
2016-04-29 03:01:57 -04:00
|
|
|
fmt.Sprintf("Must be `cli.ActionFunc`. %s", contactSysadmin)+
|
2016-04-28 11:03:10 -04:00
|
|
|
fmt.Sprintf("See %s", appActionDeprecationURL), 2)
|
2016-04-28 10:15:04 -04:00
|
|
|
)
|
|
|
|
|
|
2016-02-09 09:36:13 -07:00
|
|
|
// App is the main structure of a cli application. It is recommended that
|
2015-06-11 10:24:06 +00:00
|
|
|
// an app be created with the cli.NewApp() function
|
2013-07-19 08:34:01 -07:00
|
|
|
type App struct {
|
2015-12-18 23:28:32 +05:30
|
|
|
// The name of the program. Defaults to path.Base(os.Args[0])
|
2013-07-19 08:34:01 -07:00
|
|
|
Name string
|
2015-08-12 22:14:26 -07:00
|
|
|
// Full name of command for help, defaults to Name
|
2015-08-12 21:43:14 -07:00
|
|
|
HelpName string
|
2013-07-19 08:34:01 -07:00
|
|
|
// Description of the program.
|
|
|
|
|
Usage string
|
2015-10-24 23:51:06 -06:00
|
|
|
// Text to override the USAGE section of help
|
|
|
|
|
UsageText string
|
2015-08-03 16:51:11 -07:00
|
|
|
// Description of the program argument format.
|
|
|
|
|
ArgsUsage string
|
2013-07-19 08:34:01 -07:00
|
|
|
// Version of the program
|
|
|
|
|
Version string
|
|
|
|
|
// List of commands to execute
|
|
|
|
|
Commands []Command
|
2013-07-20 15:50:13 -07:00
|
|
|
// List of flags to parse
|
|
|
|
|
Flags []Flag
|
2014-04-12 08:32:53 -05:00
|
|
|
// Boolean to enable bash completion commands
|
|
|
|
|
EnableBashCompletion bool
|
2014-07-13 14:16:30 +01:00
|
|
|
// Boolean to hide built-in help command
|
|
|
|
|
HideHelp bool
|
2016-03-11 16:25:30 +00:00
|
|
|
// Boolean to hide built-in version flag and the VERSION section of help
|
2014-11-12 12:38:58 +01:00
|
|
|
HideVersion bool
|
2016-05-09 09:40:09 -04:00
|
|
|
// Populate on app startup, only gettable through method Categories()
|
2015-08-21 13:25:37 +02:00
|
|
|
categories CommandCategories
|
2014-04-12 08:32:53 -05:00
|
|
|
// An action to execute when the bash-completion flag is set
|
2016-04-25 18:29:05 -04:00
|
|
|
BashComplete BashCompleteFunc
|
2014-01-01 15:00:20 -06:00
|
|
|
// An action to execute before any subcommands are run, but after the context is ready
|
|
|
|
|
// If a non-nil error is returned, no subcommands are run
|
2016-04-25 18:29:05 -04:00
|
|
|
Before BeforeFunc
|
2014-11-18 23:44:21 +01:00
|
|
|
// An action to execute after any subcommands are run, but after the subcommand has finished
|
2015-02-20 16:21:27 -05:00
|
|
|
// It is run even if Action() panics
|
2016-04-25 18:29:05 -04:00
|
|
|
After AfterFunc
|
2013-07-19 08:34:01 -07:00
|
|
|
// The action to execute when no subcommands are specified
|
2016-04-27 11:34:01 -04:00
|
|
|
Action interface{}
|
|
|
|
|
// TODO: replace `Action: interface{}` with `Action: ActionFunc` once some kind
|
|
|
|
|
// of deprecation period has passed, maybe?
|
|
|
|
|
|
2014-03-30 20:40:46 -07:00
|
|
|
// Execute this function if the proper command cannot be found
|
2016-04-25 18:29:05 -04:00
|
|
|
CommandNotFound CommandNotFoundFunc
|
|
|
|
|
// Execute this function if an usage error occurs
|
|
|
|
|
OnUsageError OnUsageErrorFunc
|
2013-11-15 12:40:18 +01:00
|
|
|
// Compilation date
|
|
|
|
|
Compiled time.Time
|
2015-02-21 10:44:00 +11:00
|
|
|
// List of all authors who contributed
|
2015-01-31 10:04:52 +11:00
|
|
|
Authors []Author
|
2015-06-09 16:35:50 -06:00
|
|
|
// Copyright of the binary if any
|
|
|
|
|
Copyright string
|
2015-02-21 10:44:00 +11:00
|
|
|
// Name of Author (Note: Use App.Authors, this is deprecated)
|
|
|
|
|
Author string
|
|
|
|
|
// Email of Author (Note: Use App.Authors, this is deprecated)
|
|
|
|
|
Email string
|
2014-12-01 23:50:04 -05:00
|
|
|
// Writer writer to write output to
|
|
|
|
|
Writer io.Writer
|
2016-05-06 12:14:26 -04:00
|
|
|
// ErrWriter writes error output
|
|
|
|
|
ErrWriter io.Writer
|
2016-03-02 10:45:13 +08:00
|
|
|
// Other custom info
|
2016-04-30 10:42:07 +08:00
|
|
|
Metadata map[string]interface{}
|
2016-05-03 06:54:05 -04:00
|
|
|
|
|
|
|
|
didSetup bool
|
2013-11-15 12:40:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tries to find out when this binary was compiled.
|
|
|
|
|
// Returns the current time if it fails to find it.
|
|
|
|
|
func compileTime() time.Time {
|
|
|
|
|
info, err := os.Stat(os.Args[0])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return time.Now()
|
|
|
|
|
}
|
|
|
|
|
return info.ModTime()
|
2013-07-19 08:34:01 -07:00
|
|
|
}
|
|
|
|
|
|
2016-05-09 10:12:59 -04:00
|
|
|
// NewApp creates a new cli Application with some reasonable defaults for Name,
|
|
|
|
|
// Usage, Version and Action.
|
2013-07-19 08:34:01 -07:00
|
|
|
func NewApp() *App {
|
|
|
|
|
return &App{
|
2016-04-03 17:50:08 +08:00
|
|
|
Name: filepath.Base(os.Args[0]),
|
|
|
|
|
HelpName: filepath.Base(os.Args[0]),
|
2014-04-12 08:32:53 -05:00
|
|
|
Usage: "A new cli application",
|
2015-10-24 23:51:06 -06:00
|
|
|
UsageText: "",
|
2014-04-12 08:32:53 -05:00
|
|
|
Version: "0.0.0",
|
|
|
|
|
BashComplete: DefaultAppComplete,
|
|
|
|
|
Action: helpCommand.Action,
|
|
|
|
|
Compiled: compileTime(),
|
2014-12-01 23:50:04 -05:00
|
|
|
Writer: os.Stdout,
|
2013-07-19 08:34:01 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-03 06:54:05 -04:00
|
|
|
// Setup runs initialization code to ensure all data structures are ready for
|
|
|
|
|
// `Run` or inspection prior to `Run`. It is internally called by `Run`, but
|
|
|
|
|
// will return early if setup has already happened.
|
|
|
|
|
func (a *App) Setup() {
|
|
|
|
|
if a.didSetup {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a.didSetup = true
|
|
|
|
|
|
2015-03-10 07:59:59 -07:00
|
|
|
if a.Author != "" || a.Email != "" {
|
2015-03-10 08:12:44 -07:00
|
|
|
a.Authors = append(a.Authors, Author{Name: a.Author, Email: a.Email})
|
2015-02-21 10:44:00 +11:00
|
|
|
}
|
|
|
|
|
|
2015-08-12 21:43:14 -07:00
|
|
|
newCmds := []Command{}
|
|
|
|
|
for _, c := range a.Commands {
|
2015-08-12 21:58:25 -07:00
|
|
|
if c.HelpName == "" {
|
|
|
|
|
c.HelpName = fmt.Sprintf("%s %s", a.HelpName, c.Name)
|
|
|
|
|
}
|
2015-08-12 21:43:14 -07:00
|
|
|
newCmds = append(newCmds, c)
|
|
|
|
|
}
|
|
|
|
|
a.Commands = newCmds
|
|
|
|
|
|
2016-03-20 12:17:13 -07:00
|
|
|
a.categories = CommandCategories{}
|
|
|
|
|
for _, command := range a.Commands {
|
|
|
|
|
a.categories = a.categories.AddCommand(command.Category, command)
|
|
|
|
|
}
|
|
|
|
|
sort.Sort(a.categories)
|
|
|
|
|
|
2013-07-20 08:44:09 -07:00
|
|
|
// append help to commands
|
2014-07-13 14:16:30 +01:00
|
|
|
if a.Command(helpCommand.Name) == nil && !a.HideHelp {
|
2013-09-14 15:29:57 -07:00
|
|
|
a.Commands = append(a.Commands, helpCommand)
|
2014-12-01 23:20:21 -05:00
|
|
|
if (HelpFlag != BoolFlag{}) {
|
|
|
|
|
a.appendFlag(HelpFlag)
|
|
|
|
|
}
|
2013-09-14 15:29:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//append version/help flags
|
2014-04-12 08:32:53 -05:00
|
|
|
if a.EnableBashCompletion {
|
|
|
|
|
a.appendFlag(BashCompletionFlag)
|
|
|
|
|
}
|
2014-11-12 12:38:58 +01:00
|
|
|
|
|
|
|
|
if !a.HideVersion {
|
|
|
|
|
a.appendFlag(VersionFlag)
|
|
|
|
|
}
|
2016-05-03 06:54:05 -04:00
|
|
|
}
|
|
|
|
|
|
2016-05-09 10:12:59 -04:00
|
|
|
// Run is the entry point to the cli app. Parses the arguments slice and routes
|
|
|
|
|
// to the proper flag/args combination
|
2016-05-03 06:54:05 -04:00
|
|
|
func (a *App) Run(arguments []string) (err error) {
|
|
|
|
|
a.Setup()
|
2013-07-20 08:44:09 -07:00
|
|
|
|
2013-07-19 15:10:34 -07:00
|
|
|
// parse flags
|
2013-07-20 08:21:20 -07:00
|
|
|
set := flagSet(a.Name, a.Flags)
|
2013-09-04 13:58:31 -06:00
|
|
|
set.SetOutput(ioutil.Discard)
|
2014-11-18 23:44:21 +01:00
|
|
|
err = set.Parse(arguments[1:])
|
2013-11-20 01:05:18 -07:00
|
|
|
nerr := normalizeFlags(a.Flags, set)
|
2016-02-04 15:25:41 +08:00
|
|
|
context := NewContext(a, set, nil)
|
2013-11-20 01:05:18 -07:00
|
|
|
if nerr != nil {
|
2014-12-01 23:57:35 -05:00
|
|
|
fmt.Fprintln(a.Writer, nerr)
|
2013-11-20 01:05:18 -07:00
|
|
|
ShowAppHelp(context)
|
2016-04-27 09:12:34 -04:00
|
|
|
return nerr
|
2013-11-20 01:05:18 -07:00
|
|
|
}
|
2013-09-04 13:58:31 -06:00
|
|
|
|
2015-12-13 09:31:32 +10:00
|
|
|
if checkCompletions(context) {
|
2016-04-27 09:12:34 -04:00
|
|
|
return nil
|
2015-12-13 09:31:32 +10:00
|
|
|
}
|
|
|
|
|
|
2013-07-24 07:35:45 -07:00
|
|
|
if err != nil {
|
2016-01-23 12:47:24 +01:00
|
|
|
if a.OnUsageError != nil {
|
|
|
|
|
err := a.OnUsageError(context, err, false)
|
2016-05-02 11:25:37 -04:00
|
|
|
HandleExitCoder(err)
|
2016-04-27 09:12:34 -04:00
|
|
|
return err
|
2016-01-23 12:47:24 +01:00
|
|
|
}
|
2016-05-09 10:15:05 -04:00
|
|
|
fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
|
|
|
|
|
ShowAppHelp(context)
|
|
|
|
|
return err
|
2013-07-24 07:35:45 -07:00
|
|
|
}
|
2013-07-19 08:34:01 -07:00
|
|
|
|
2015-09-27 23:42:17 -07:00
|
|
|
if !a.HideHelp && checkHelp(context) {
|
2015-10-18 17:02:23 -07:00
|
|
|
ShowAppHelp(context)
|
2016-04-27 09:12:34 -04:00
|
|
|
return nil
|
2013-11-01 06:45:19 -07:00
|
|
|
}
|
|
|
|
|
|
2015-09-27 23:42:17 -07:00
|
|
|
if !a.HideVersion && checkVersion(context) {
|
2015-10-18 17:02:23 -07:00
|
|
|
ShowVersion(context)
|
2016-04-27 09:12:34 -04:00
|
|
|
return nil
|
2013-11-01 06:45:19 -07:00
|
|
|
}
|
2013-07-20 08:44:09 -07:00
|
|
|
|
2014-11-18 23:44:21 +01:00
|
|
|
if a.After != nil {
|
|
|
|
|
defer func() {
|
2016-04-27 11:34:01 -04:00
|
|
|
if afterErr := a.After(context); afterErr != nil {
|
2015-06-01 21:11:20 -07:00
|
|
|
if err != nil {
|
|
|
|
|
err = NewMultiError(err, afterErr)
|
|
|
|
|
} else {
|
|
|
|
|
err = afterErr
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-11-18 23:44:21 +01:00
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-01 15:00:20 -06:00
|
|
|
if a.Before != nil {
|
2016-04-28 17:15:16 -04:00
|
|
|
beforeErr := a.Before(context)
|
|
|
|
|
if beforeErr != nil {
|
|
|
|
|
fmt.Fprintf(a.Writer, "%v\n\n", beforeErr)
|
2015-12-25 22:08:22 +01:00
|
|
|
ShowAppHelp(context)
|
2016-04-28 17:15:16 -04:00
|
|
|
HandleExitCoder(beforeErr)
|
|
|
|
|
err = beforeErr
|
2016-04-27 09:12:34 -04:00
|
|
|
return err
|
2014-01-01 15:00:20 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-19 08:34:01 -07:00
|
|
|
args := context.Args()
|
2013-11-24 14:40:21 +01:00
|
|
|
if args.Present() {
|
|
|
|
|
name := args.First()
|
2013-09-14 15:29:57 -07:00
|
|
|
c := a.Command(name)
|
|
|
|
|
if c != nil {
|
2013-11-01 06:45:19 -07:00
|
|
|
return c.Run(context)
|
2013-07-19 08:34:01 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run default Action
|
2016-04-27 11:34:01 -04:00
|
|
|
err = HandleAction(a.Action, context)
|
2016-04-27 09:12:34 -04:00
|
|
|
|
2016-05-02 11:25:37 -04:00
|
|
|
HandleExitCoder(err)
|
2016-04-27 09:12:34 -04:00
|
|
|
return err
|
2013-07-19 08:34:01 -07:00
|
|
|
}
|
2013-09-14 15:29:57 -07:00
|
|
|
|
2016-04-27 12:23:09 -04:00
|
|
|
// DEPRECATED: Another entry point to the cli app, takes care of passing arguments and error handling
|
|
|
|
|
func (a *App) RunAndExitOnError() {
|
2016-05-06 12:14:26 -04:00
|
|
|
fmt.Fprintf(a.errWriter(),
|
2016-05-01 09:09:54 -04:00
|
|
|
"DEPRECATED cli.App.RunAndExitOnError. %s See %s\n",
|
|
|
|
|
contactSysadmin, runAndExitOnErrorDeprecationURL)
|
2016-04-27 12:23:09 -04:00
|
|
|
if err := a.Run(os.Args); err != nil {
|
2016-05-06 12:14:26 -04:00
|
|
|
fmt.Fprintln(a.errWriter(), err)
|
2016-05-02 11:25:37 -04:00
|
|
|
OsExiter(1)
|
2016-04-27 12:23:09 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-09 10:12:59 -04:00
|
|
|
// RunAsSubcommand invokes the subcommand given the context, parses ctx.Args() to
|
|
|
|
|
// generate command-specific flags
|
2016-04-27 09:12:34 -04:00
|
|
|
func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
2014-04-16 11:18:00 -05:00
|
|
|
// append help to commands
|
2014-04-17 11:48:00 -05:00
|
|
|
if len(a.Commands) > 0 {
|
2014-07-13 14:16:30 +01:00
|
|
|
if a.Command(helpCommand.Name) == nil && !a.HideHelp {
|
2014-04-17 11:48:00 -05:00
|
|
|
a.Commands = append(a.Commands, helpCommand)
|
2014-12-01 23:20:21 -05:00
|
|
|
if (HelpFlag != BoolFlag{}) {
|
|
|
|
|
a.appendFlag(HelpFlag)
|
|
|
|
|
}
|
2014-04-17 11:48:00 -05:00
|
|
|
}
|
2014-04-16 11:18:00 -05:00
|
|
|
}
|
|
|
|
|
|
2015-08-12 21:43:14 -07:00
|
|
|
newCmds := []Command{}
|
|
|
|
|
for _, c := range a.Commands {
|
2015-08-12 21:58:25 -07:00
|
|
|
if c.HelpName == "" {
|
|
|
|
|
c.HelpName = fmt.Sprintf("%s %s", a.HelpName, c.Name)
|
|
|
|
|
}
|
2015-08-12 21:43:14 -07:00
|
|
|
newCmds = append(newCmds, c)
|
|
|
|
|
}
|
|
|
|
|
a.Commands = newCmds
|
|
|
|
|
|
2014-04-17 11:48:00 -05:00
|
|
|
// append flags
|
2014-04-16 11:18:00 -05:00
|
|
|
if a.EnableBashCompletion {
|
|
|
|
|
a.appendFlag(BashCompletionFlag)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// parse flags
|
|
|
|
|
set := flagSet(a.Name, a.Flags)
|
|
|
|
|
set.SetOutput(ioutil.Discard)
|
2014-11-18 23:44:21 +01:00
|
|
|
err = set.Parse(ctx.Args().Tail())
|
2014-04-16 11:18:00 -05:00
|
|
|
nerr := normalizeFlags(a.Flags, set)
|
2015-05-18 17:39:48 +02:00
|
|
|
context := NewContext(a, set, ctx)
|
2014-04-16 11:18:00 -05:00
|
|
|
|
|
|
|
|
if nerr != nil {
|
2014-12-01 23:57:35 -05:00
|
|
|
fmt.Fprintln(a.Writer, nerr)
|
2015-06-24 22:46:33 -07:00
|
|
|
fmt.Fprintln(a.Writer)
|
2014-04-17 11:48:00 -05:00
|
|
|
if len(a.Commands) > 0 {
|
|
|
|
|
ShowSubcommandHelp(context)
|
|
|
|
|
} else {
|
|
|
|
|
ShowCommandHelp(ctx, context.Args().First())
|
|
|
|
|
}
|
2016-04-27 09:12:34 -04:00
|
|
|
return nerr
|
2014-04-16 11:18:00 -05:00
|
|
|
}
|
|
|
|
|
|
2015-12-13 09:31:32 +10:00
|
|
|
if checkCompletions(context) {
|
2016-04-27 09:12:34 -04:00
|
|
|
return nil
|
2015-12-13 09:31:32 +10:00
|
|
|
}
|
|
|
|
|
|
2014-04-16 11:18:00 -05:00
|
|
|
if err != nil {
|
2016-01-23 12:47:24 +01:00
|
|
|
if a.OnUsageError != nil {
|
|
|
|
|
err = a.OnUsageError(context, err, true)
|
2016-04-27 09:18:42 -04:00
|
|
|
HandleExitCoder(err)
|
2016-04-28 17:15:16 -04:00
|
|
|
return err
|
2016-01-23 12:47:24 +01:00
|
|
|
}
|
2016-05-09 10:15:05 -04:00
|
|
|
fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
|
|
|
|
|
ShowSubcommandHelp(context)
|
|
|
|
|
return err
|
2014-04-16 11:18:00 -05:00
|
|
|
}
|
|
|
|
|
|
2014-04-17 11:48:00 -05:00
|
|
|
if len(a.Commands) > 0 {
|
|
|
|
|
if checkSubcommandHelp(context) {
|
2016-04-27 09:12:34 -04:00
|
|
|
return nil
|
2014-04-17 11:48:00 -05:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if checkCommandHelp(ctx, context.Args().First()) {
|
2016-04-27 09:12:34 -04:00
|
|
|
return nil
|
2014-04-17 11:48:00 -05:00
|
|
|
}
|
2014-04-16 11:18:00 -05:00
|
|
|
}
|
|
|
|
|
|
2014-11-18 23:44:21 +01:00
|
|
|
if a.After != nil {
|
|
|
|
|
defer func() {
|
2016-04-27 09:12:34 -04:00
|
|
|
afterErr := a.After(context)
|
2015-06-01 21:11:20 -07:00
|
|
|
if afterErr != nil {
|
2016-04-27 09:18:42 -04:00
|
|
|
HandleExitCoder(err)
|
2015-06-01 21:11:20 -07:00
|
|
|
if err != nil {
|
|
|
|
|
err = NewMultiError(err, afterErr)
|
|
|
|
|
} else {
|
|
|
|
|
err = afterErr
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-11-18 23:44:21 +01:00
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-16 15:26:28 -05:00
|
|
|
if a.Before != nil {
|
2016-04-28 17:15:16 -04:00
|
|
|
beforeErr := a.Before(context)
|
|
|
|
|
if beforeErr != nil {
|
|
|
|
|
HandleExitCoder(beforeErr)
|
|
|
|
|
err = beforeErr
|
2016-04-27 09:12:34 -04:00
|
|
|
return err
|
2014-04-16 15:26:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-16 11:18:00 -05:00
|
|
|
args := context.Args()
|
|
|
|
|
if args.Present() {
|
|
|
|
|
name := args.First()
|
|
|
|
|
c := a.Command(name)
|
|
|
|
|
if c != nil {
|
|
|
|
|
return c.Run(context)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run default Action
|
2016-04-27 11:34:01 -04:00
|
|
|
err = HandleAction(a.Action, context)
|
|
|
|
|
|
2016-05-02 11:25:37 -04:00
|
|
|
HandleExitCoder(err)
|
2016-04-27 11:34:01 -04:00
|
|
|
return err
|
2014-04-16 11:18:00 -05:00
|
|
|
}
|
|
|
|
|
|
2016-05-09 10:12:59 -04:00
|
|
|
// Command returns the named command on App. Returns nil if the command does not exist
|
2013-09-14 15:29:57 -07:00
|
|
|
func (a *App) Command(name string) *Command {
|
|
|
|
|
for _, c := range a.Commands {
|
|
|
|
|
if c.HasName(name) {
|
|
|
|
|
return &c
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-03 06:54:05 -04:00
|
|
|
// Categories returns a slice containing all the categories with the commands they contain
|
2015-08-21 13:25:37 +02:00
|
|
|
func (a *App) Categories() CommandCategories {
|
|
|
|
|
return a.categories
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-03 06:54:05 -04:00
|
|
|
// VisibleCategories returns a slice of categories and commands that are
|
|
|
|
|
// Hidden=false
|
|
|
|
|
func (a *App) VisibleCategories() []*CommandCategory {
|
|
|
|
|
ret := []*CommandCategory{}
|
|
|
|
|
for _, category := range a.categories {
|
|
|
|
|
if visible := func() *CommandCategory {
|
|
|
|
|
for _, command := range category.Commands {
|
|
|
|
|
if !command.Hidden {
|
|
|
|
|
return category
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}(); visible != nil {
|
|
|
|
|
ret = append(ret, visible)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VisibleCommands returns a slice of the Commands with Hidden=false
|
|
|
|
|
func (a *App) VisibleCommands() []Command {
|
|
|
|
|
ret := []Command{}
|
|
|
|
|
for _, command := range a.Commands {
|
|
|
|
|
if !command.Hidden {
|
|
|
|
|
ret = append(ret, command)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-01 08:36:17 -04:00
|
|
|
// VisibleFlags returns a slice of the Flags with Hidden=false
|
|
|
|
|
func (a *App) VisibleFlags() []Flag {
|
|
|
|
|
return visibleFlags(a.Flags)
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-14 15:29:57 -07:00
|
|
|
func (a *App) hasFlag(flag Flag) bool {
|
|
|
|
|
for _, f := range a.Flags {
|
|
|
|
|
if flag == f {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-06 12:14:26 -04:00
|
|
|
func (a *App) errWriter() io.Writer {
|
|
|
|
|
|
|
|
|
|
// When the app ErrWriter is nil use the package level one.
|
|
|
|
|
if a.ErrWriter == nil {
|
|
|
|
|
return ErrWriter
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return a.ErrWriter
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-14 15:29:57 -07:00
|
|
|
func (a *App) appendFlag(flag Flag) {
|
|
|
|
|
if !a.hasFlag(flag) {
|
|
|
|
|
a.Flags = append(a.Flags, flag)
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-31 10:04:52 +11:00
|
|
|
|
|
|
|
|
// Author represents someone who has contributed to a cli project.
|
|
|
|
|
type Author struct {
|
|
|
|
|
Name string // The Authors name
|
|
|
|
|
Email string // The Authors email
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// String makes Author comply to the Stringer interface, to allow an easy print in the templating process
|
|
|
|
|
func (a Author) String() string {
|
|
|
|
|
e := ""
|
|
|
|
|
if a.Email != "" {
|
2015-02-21 10:44:00 +11:00
|
|
|
e = "<" + a.Email + "> "
|
2015-01-31 10:04:52 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fmt.Sprintf("%v %v", a.Name, e)
|
|
|
|
|
}
|
2016-04-27 11:34:01 -04:00
|
|
|
|
|
|
|
|
// HandleAction uses ✧✧✧reflection✧✧✧ to figure out if the given Action is an
|
2016-04-28 10:15:04 -04:00
|
|
|
// ActionFunc, a func with the legacy signature for Action, or some other
|
|
|
|
|
// invalid thing. If it's an ActionFunc or a func with the legacy signature for
|
|
|
|
|
// Action, the func is run!
|
2016-04-28 11:03:10 -04:00
|
|
|
func HandleAction(action interface{}, context *Context) (err error) {
|
|
|
|
|
defer func() {
|
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
|
switch r.(type) {
|
|
|
|
|
case error:
|
|
|
|
|
err = r.(error)
|
|
|
|
|
default:
|
|
|
|
|
err = NewExitError(fmt.Sprintf("ERROR unknown Action error: %v. See %s", r, appActionDeprecationURL), 2)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2016-04-27 11:34:01 -04:00
|
|
|
if reflect.TypeOf(action).Kind() != reflect.Func {
|
2016-04-28 10:15:04 -04:00
|
|
|
return errNonFuncAction
|
2016-04-27 11:34:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vals := reflect.ValueOf(action).Call([]reflect.Value{reflect.ValueOf(context)})
|
|
|
|
|
|
|
|
|
|
if len(vals) == 0 {
|
2016-05-06 12:14:26 -04:00
|
|
|
fmt.Fprintf(ErrWriter,
|
2016-05-01 09:09:54 -04:00
|
|
|
"DEPRECATED Action signature. Must be `cli.ActionFunc`. %s See %s\n",
|
|
|
|
|
contactSysadmin, appActionDeprecationURL)
|
2016-04-29 03:01:57 -04:00
|
|
|
return nil
|
2016-04-27 11:34:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(vals) > 1 {
|
2016-04-28 10:15:04 -04:00
|
|
|
return errInvalidActionSignature
|
2016-04-27 11:34:01 -04:00
|
|
|
}
|
|
|
|
|
|
2016-05-02 11:25:37 -04:00
|
|
|
if retErr, ok := vals[0].Interface().(error); vals[0].IsValid() && ok {
|
2016-04-28 11:03:10 -04:00
|
|
|
return retErr
|
2016-04-27 11:34:01 -04:00
|
|
|
}
|
|
|
|
|
|
2016-04-28 11:03:10 -04:00
|
|
|
return err
|
2016-04-27 11:34:01 -04:00
|
|
|
}
|