2013-07-19 15:34:01 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2013-09-04 19:58:31 +00:00
|
|
|
"fmt"
|
2014-06-12 07:39:13 +00:00
|
|
|
"io"
|
2013-09-14 22:29:57 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2016-04-03 09:50:08 +00:00
|
|
|
"path/filepath"
|
2016-05-22 01:29:45 +00:00
|
|
|
"reflect"
|
2015-08-21 08:58:14 +00:00
|
|
|
"sort"
|
2013-11-15 11:40:18 +00:00
|
|
|
"time"
|
2013-07-19 15:34:01 +00:00
|
|
|
)
|
|
|
|
|
2016-06-22 16:47:57 +00:00
|
|
|
// App is the main structure of a cli application.
|
2013-07-19 15:34:01 +00:00
|
|
|
type App struct {
|
2015-12-18 17:58:32 +00:00
|
|
|
// The name of the program. Defaults to path.Base(os.Args[0])
|
2013-07-19 15:34:01 +00:00
|
|
|
Name string
|
2015-08-13 05:14:26 +00:00
|
|
|
// Full name of command for help, defaults to Name
|
2015-08-13 04:43:14 +00:00
|
|
|
HelpName string
|
2013-07-19 15:34:01 +00:00
|
|
|
// Description of the program.
|
|
|
|
Usage string
|
2015-10-25 05:51:06 +00:00
|
|
|
// Text to override the USAGE section of help
|
|
|
|
UsageText string
|
2015-08-03 23:51:11 +00:00
|
|
|
// Description of the program argument format.
|
|
|
|
ArgsUsage string
|
2013-07-19 15:34:01 +00:00
|
|
|
// Version of the program
|
|
|
|
Version string
|
2016-10-12 15:06:41 +00:00
|
|
|
// Description of the program
|
|
|
|
Description string
|
2013-07-19 15:34:01 +00:00
|
|
|
// List of commands to execute
|
2016-05-22 19:20:52 +00:00
|
|
|
Commands []*Command
|
2013-07-20 22:50:13 +00:00
|
|
|
// List of flags to parse
|
|
|
|
Flags []Flag
|
2016-07-22 08:19:29 +00:00
|
|
|
// Boolean to enable shell completion commands
|
|
|
|
EnableShellCompletion bool
|
2014-07-13 13:16:30 +00: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 11:38:58 +00:00
|
|
|
HideVersion bool
|
2016-05-25 16:05:14 +00:00
|
|
|
// Categories contains the categorized commands and is populated on app startup
|
|
|
|
Categories CommandCategories
|
2016-07-22 08:19:29 +00:00
|
|
|
// An action to execute when the shell completion flag is set
|
|
|
|
ShellComplete ShellCompleteFunc
|
2014-01-01 21:00:20 +00: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 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-07-19 15:34:01 +00:00
|
|
|
// The action to execute when no subcommands are specified
|
2016-05-17 19:11:44 +00:00
|
|
|
Action ActionFunc
|
2014-03-31 03:40:46 +00:00
|
|
|
// Execute this function if the proper command cannot be found
|
2016-04-25 22:29:05 +00:00
|
|
|
CommandNotFound CommandNotFoundFunc
|
|
|
|
// Execute this function if an usage error occurs
|
|
|
|
OnUsageError OnUsageErrorFunc
|
2013-11-15 11:40:18 +00:00
|
|
|
// Compilation date
|
|
|
|
Compiled time.Time
|
2015-02-20 23:44:00 +00:00
|
|
|
// List of all authors who contributed
|
2016-05-22 19:20:52 +00:00
|
|
|
Authors []*Author
|
2015-06-09 22:35:50 +00:00
|
|
|
// Copyright of the binary if any
|
|
|
|
Copyright string
|
2014-12-02 04:50:04 +00:00
|
|
|
// Writer writer to write output to
|
|
|
|
Writer io.Writer
|
2016-05-06 16:14:26 +00:00
|
|
|
// ErrWriter writes error output
|
|
|
|
ErrWriter io.Writer
|
2016-03-02 02:45:13 +00:00
|
|
|
// Other custom info
|
2016-04-30 02:42:07 +00:00
|
|
|
Metadata map[string]interface{}
|
2016-11-25 09:07:42 +00:00
|
|
|
// Carries a function which returns app specific info.
|
|
|
|
ExtraInfo func() map[string]string
|
2016-11-25 08:16:48 +00:00
|
|
|
// CustomAppHelpTemplate the text template for app help topic.
|
|
|
|
// cli.go uses text/template to render templates. You can
|
|
|
|
// render custom help text by setting this variable.
|
|
|
|
CustomAppHelpTemplate string
|
2016-05-03 10:54:05 +00:00
|
|
|
|
|
|
|
didSetup bool
|
2013-11-15 11:40:18 +00: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 15:34:01 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 10:54:05 +00: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
|
|
|
|
|
2016-06-22 16:47:57 +00:00
|
|
|
if a.Name == "" {
|
|
|
|
a.Name = filepath.Base(os.Args[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.HelpName == "" {
|
|
|
|
a.HelpName = filepath.Base(os.Args[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.Usage == "" {
|
|
|
|
a.Usage = "A new cli application"
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.Version == "" {
|
|
|
|
a.Version = "0.0.0"
|
|
|
|
}
|
|
|
|
|
2016-07-22 08:19:29 +00:00
|
|
|
if a.ShellComplete == nil {
|
|
|
|
a.ShellComplete = DefaultAppComplete
|
2016-06-22 16:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if a.Action == nil {
|
|
|
|
a.Action = helpCommand.Action
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.Compiled == (time.Time{}) {
|
|
|
|
a.Compiled = compileTime()
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.Writer == nil {
|
|
|
|
a.Writer = os.Stdout
|
|
|
|
}
|
|
|
|
|
2016-05-22 19:20:52 +00:00
|
|
|
newCmds := []*Command{}
|
2015-08-13 04:43:14 +00:00
|
|
|
for _, c := range a.Commands {
|
2015-08-13 04:58:25 +00:00
|
|
|
if c.HelpName == "" {
|
|
|
|
c.HelpName = fmt.Sprintf("%s %s", a.HelpName, c.Name)
|
|
|
|
}
|
2015-08-13 04:43:14 +00:00
|
|
|
newCmds = append(newCmds, c)
|
|
|
|
}
|
|
|
|
a.Commands = newCmds
|
|
|
|
|
2014-07-13 13:16:30 +00:00
|
|
|
if a.Command(helpCommand.Name) == nil && !a.HideHelp {
|
2016-05-22 19:20:52 +00:00
|
|
|
a.appendCommand(helpCommand)
|
|
|
|
|
|
|
|
if HelpFlag != nil {
|
2014-12-02 04:20:21 +00:00
|
|
|
a.appendFlag(HelpFlag)
|
|
|
|
}
|
2013-09-14 22:29:57 +00:00
|
|
|
}
|
|
|
|
|
2016-07-22 08:19:29 +00:00
|
|
|
if a.EnableShellCompletion {
|
|
|
|
a.appendFlag(GenerateCompletionFlag)
|
2016-07-21 19:01:59 +00:00
|
|
|
a.appendFlag(InitCompletionFlag)
|
2014-04-12 13:32:53 +00:00
|
|
|
}
|
2014-11-12 11:38:58 +00:00
|
|
|
|
|
|
|
if !a.HideVersion {
|
|
|
|
a.appendFlag(VersionFlag)
|
|
|
|
}
|
2016-06-22 13:34:24 +00:00
|
|
|
|
2016-06-22 16:03:21 +00:00
|
|
|
a.Categories = newCommandCategories()
|
2016-06-22 13:34:24 +00:00
|
|
|
for _, command := range a.Commands {
|
2016-06-22 16:03:21 +00:00
|
|
|
a.Categories.AddCommand(command.Category, command)
|
2016-06-22 13:34:24 +00:00
|
|
|
}
|
2016-06-22 16:03:21 +00:00
|
|
|
sort.Sort(a.Categories.(*commandCategories))
|
2016-07-11 06:39:58 +00:00
|
|
|
|
|
|
|
if a.Metadata == nil {
|
|
|
|
a.Metadata = make(map[string]interface{})
|
|
|
|
}
|
2016-10-19 03:56:31 +00:00
|
|
|
|
|
|
|
if a.Writer == nil {
|
|
|
|
a.Writer = os.Stdout
|
|
|
|
}
|
2016-05-03 10:54:05 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00: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 10:54:05 +00:00
|
|
|
func (a *App) Run(arguments []string) (err error) {
|
|
|
|
a.Setup()
|
2013-07-20 15:44:09 +00:00
|
|
|
|
2016-11-04 20:56:28 +00:00
|
|
|
// handle the completion flag separately from the flagset since
|
|
|
|
// completion could be attempted after a flag, but before its value was put
|
|
|
|
// on the command line. this causes the flagset to interpret the completion
|
|
|
|
// flag name as the value of the flag before it which is undesirable
|
|
|
|
// note that we can only do this because the shell autocomplete function
|
|
|
|
// always appends the completion flag at the end of the command
|
2016-11-14 16:35:22 +00:00
|
|
|
shellComplete, arguments := checkShellCompleteFlag(a, arguments)
|
2016-11-04 20:56:28 +00:00
|
|
|
|
2013-07-19 22:10:34 +00:00
|
|
|
// parse flags
|
2016-09-17 23:54:29 +00:00
|
|
|
set, err := flagSet(a.Name, a.Flags)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-09-04 19:58:31 +00:00
|
|
|
set.SetOutput(ioutil.Discard)
|
2014-11-18 22:44:21 +00:00
|
|
|
err = set.Parse(arguments[1:])
|
2013-11-20 08:05:18 +00:00
|
|
|
nerr := normalizeFlags(a.Flags, set)
|
2016-02-04 07:25:41 +00:00
|
|
|
context := NewContext(a, set, nil)
|
2013-11-20 08:05:18 +00:00
|
|
|
if nerr != nil {
|
2014-12-02 04:57:35 +00:00
|
|
|
fmt.Fprintln(a.Writer, nerr)
|
2013-11-20 08:05:18 +00:00
|
|
|
ShowAppHelp(context)
|
2016-04-27 13:12:34 +00:00
|
|
|
return nerr
|
2013-11-20 08:05:18 +00:00
|
|
|
}
|
2016-11-14 16:35:22 +00:00
|
|
|
context.shellComplete = shellComplete
|
2013-09-04 19:58:31 +00:00
|
|
|
|
2015-12-12 23:31:32 +00:00
|
|
|
if checkCompletions(context) {
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2015-12-12 23:31:32 +00:00
|
|
|
}
|
|
|
|
|
2016-07-22 08:19:29 +00:00
|
|
|
if done, cerr := checkInitCompletion(context); done {
|
|
|
|
if cerr != nil {
|
|
|
|
err = cerr
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-21 19:01:59 +00:00
|
|
|
}
|
|
|
|
|
2013-07-24 14:35:45 +00:00
|
|
|
if err != nil {
|
2016-01-23 11:47:24 +00:00
|
|
|
if a.OnUsageError != nil {
|
|
|
|
err := a.OnUsageError(context, err, false)
|
2016-05-02 15:25:37 +00:00
|
|
|
HandleExitCoder(err)
|
2016-04-27 13:12:34 +00:00
|
|
|
return err
|
2016-01-23 11:47:24 +00:00
|
|
|
}
|
2016-10-07 14:38:36 +00:00
|
|
|
fmt.Fprintf(a.Writer, "%s %s\n\n", "Incorrect Usage.", err.Error())
|
2016-05-09 14:15:05 +00:00
|
|
|
ShowAppHelp(context)
|
|
|
|
return err
|
2013-07-24 14:35:45 +00:00
|
|
|
}
|
2013-07-19 15:34:01 +00:00
|
|
|
|
2015-09-28 06:42:17 +00:00
|
|
|
if !a.HideHelp && checkHelp(context) {
|
2015-10-19 00:02:23 +00:00
|
|
|
ShowAppHelp(context)
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2013-11-01 13:45:19 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 06:42:17 +00:00
|
|
|
if !a.HideVersion && checkVersion(context) {
|
2015-10-19 00:02:23 +00:00
|
|
|
ShowVersion(context)
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2013-11-01 13:45:19 +00:00
|
|
|
}
|
2013-07-20 15:44:09 +00:00
|
|
|
|
2014-11-18 22:44:21 +00:00
|
|
|
if a.After != nil {
|
|
|
|
defer func() {
|
2016-04-27 15:34:01 +00:00
|
|
|
if afterErr := a.After(context); afterErr != nil {
|
2015-06-02 04:11:20 +00:00
|
|
|
if err != nil {
|
2016-05-25 16:05:14 +00:00
|
|
|
err = newMultiError(err, afterErr)
|
2015-06-02 04:11:20 +00:00
|
|
|
} else {
|
|
|
|
err = afterErr
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 22:44:21 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-01-01 21:00:20 +00:00
|
|
|
if a.Before != nil {
|
2016-04-28 21:15:16 +00:00
|
|
|
beforeErr := a.Before(context)
|
|
|
|
if beforeErr != nil {
|
2015-12-25 21:08:22 +00:00
|
|
|
ShowAppHelp(context)
|
2016-04-28 21:15:16 +00:00
|
|
|
HandleExitCoder(beforeErr)
|
|
|
|
err = beforeErr
|
2016-04-27 13:12:34 +00:00
|
|
|
return err
|
2014-01-01 21:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 15:34:01 +00:00
|
|
|
args := context.Args()
|
2013-11-24 13:40:21 +00:00
|
|
|
if args.Present() {
|
|
|
|
name := args.First()
|
2013-09-14 22:29:57 +00:00
|
|
|
c := a.Command(name)
|
|
|
|
if c != nil {
|
2013-11-01 13:45:19 +00:00
|
|
|
return c.Run(context)
|
2013-07-19 15:34:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-12 21:37:07 +00:00
|
|
|
if a.Action == nil {
|
|
|
|
a.Action = helpCommand.Action
|
|
|
|
}
|
|
|
|
|
2013-07-19 15:34:01 +00:00
|
|
|
// Run default Action
|
2016-05-17 19:11:44 +00:00
|
|
|
err = a.Action(context)
|
2016-04-27 13:12:34 +00:00
|
|
|
|
2016-05-02 15:25:37 +00:00
|
|
|
HandleExitCoder(err)
|
2016-04-27 13:12:34 +00:00
|
|
|
return err
|
2013-07-19 15:34:01 +00:00
|
|
|
}
|
2013-09-14 22:29:57 +00:00
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// RunAsSubcommand invokes the subcommand given the context, parses ctx.Args() to
|
|
|
|
// generate command-specific flags
|
2016-04-27 13:12:34 +00:00
|
|
|
func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
2016-10-29 21:13:18 +00:00
|
|
|
a.Setup()
|
|
|
|
|
2014-04-16 16:18:00 +00:00
|
|
|
// append help to commands
|
2014-04-17 16:48:00 +00:00
|
|
|
if len(a.Commands) > 0 {
|
2014-07-13 13:16:30 +00:00
|
|
|
if a.Command(helpCommand.Name) == nil && !a.HideHelp {
|
2016-05-22 19:20:52 +00:00
|
|
|
a.appendCommand(helpCommand)
|
|
|
|
|
|
|
|
if HelpFlag != nil {
|
2014-12-02 04:20:21 +00:00
|
|
|
a.appendFlag(HelpFlag)
|
|
|
|
}
|
2014-04-17 16:48:00 +00:00
|
|
|
}
|
2014-04-16 16:18:00 +00:00
|
|
|
}
|
|
|
|
|
2016-05-22 19:20:52 +00:00
|
|
|
newCmds := []*Command{}
|
2015-08-13 04:43:14 +00:00
|
|
|
for _, c := range a.Commands {
|
2015-08-13 04:58:25 +00:00
|
|
|
if c.HelpName == "" {
|
|
|
|
c.HelpName = fmt.Sprintf("%s %s", a.HelpName, c.Name)
|
|
|
|
}
|
2015-08-13 04:43:14 +00:00
|
|
|
newCmds = append(newCmds, c)
|
|
|
|
}
|
|
|
|
a.Commands = newCmds
|
|
|
|
|
2014-04-17 16:48:00 +00:00
|
|
|
// append flags
|
2016-07-22 08:19:29 +00:00
|
|
|
if a.EnableShellCompletion {
|
|
|
|
a.appendFlag(GenerateCompletionFlag)
|
2014-04-16 16:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse flags
|
2016-09-17 23:54:29 +00:00
|
|
|
set, err := flagSet(a.Name, a.Flags)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-16 16:18:00 +00:00
|
|
|
set.SetOutput(ioutil.Discard)
|
2014-11-18 22:44:21 +00:00
|
|
|
err = set.Parse(ctx.Args().Tail())
|
2014-04-16 16:18:00 +00:00
|
|
|
nerr := normalizeFlags(a.Flags, set)
|
2015-05-18 15:39:48 +00:00
|
|
|
context := NewContext(a, set, ctx)
|
2014-04-16 16:18:00 +00:00
|
|
|
|
|
|
|
if nerr != nil {
|
2014-12-02 04:57:35 +00:00
|
|
|
fmt.Fprintln(a.Writer, nerr)
|
2015-06-25 05:46:33 +00:00
|
|
|
fmt.Fprintln(a.Writer)
|
2014-04-17 16:48:00 +00:00
|
|
|
if len(a.Commands) > 0 {
|
|
|
|
ShowSubcommandHelp(context)
|
|
|
|
} else {
|
|
|
|
ShowCommandHelp(ctx, context.Args().First())
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nerr
|
2014-04-16 16:18:00 +00:00
|
|
|
}
|
|
|
|
|
2015-12-12 23:31:32 +00:00
|
|
|
if checkCompletions(context) {
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2015-12-12 23:31:32 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 16:18:00 +00:00
|
|
|
if err != nil {
|
2016-01-23 11:47:24 +00:00
|
|
|
if a.OnUsageError != nil {
|
|
|
|
err = a.OnUsageError(context, err, true)
|
2016-04-27 13:18:42 +00:00
|
|
|
HandleExitCoder(err)
|
2016-04-28 21:15:16 +00:00
|
|
|
return err
|
2016-01-23 11:47:24 +00:00
|
|
|
}
|
2016-10-07 14:38:36 +00:00
|
|
|
fmt.Fprintf(a.Writer, "%s %s\n\n", "Incorrect Usage.", err.Error())
|
2016-05-09 14:15:05 +00:00
|
|
|
ShowSubcommandHelp(context)
|
|
|
|
return err
|
2014-04-16 16:18:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-17 16:48:00 +00:00
|
|
|
if len(a.Commands) > 0 {
|
|
|
|
if checkSubcommandHelp(context) {
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-04-17 16:48:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if checkCommandHelp(ctx, context.Args().First()) {
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-04-17 16:48:00 +00:00
|
|
|
}
|
2014-04-16 16:18:00 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 22:44:21 +00:00
|
|
|
if a.After != nil {
|
|
|
|
defer func() {
|
2016-04-27 13:12:34 +00:00
|
|
|
afterErr := a.After(context)
|
2015-06-02 04:11:20 +00:00
|
|
|
if afterErr != nil {
|
2016-04-27 13:18:42 +00:00
|
|
|
HandleExitCoder(err)
|
2015-06-02 04:11:20 +00:00
|
|
|
if err != nil {
|
2016-05-25 16:05:14 +00:00
|
|
|
err = newMultiError(err, afterErr)
|
2015-06-02 04:11:20 +00:00
|
|
|
} else {
|
|
|
|
err = afterErr
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 22:44:21 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-04-16 20:26:28 +00:00
|
|
|
if a.Before != nil {
|
2016-04-28 21:15:16 +00:00
|
|
|
beforeErr := a.Before(context)
|
|
|
|
if beforeErr != nil {
|
|
|
|
HandleExitCoder(beforeErr)
|
|
|
|
err = beforeErr
|
2016-04-27 13:12:34 +00:00
|
|
|
return err
|
2014-04-16 20:26:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-16 16:18:00 +00: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-05-17 19:11:44 +00:00
|
|
|
err = a.Action(context)
|
2016-04-27 15:34:01 +00:00
|
|
|
|
2016-05-02 15:25:37 +00:00
|
|
|
HandleExitCoder(err)
|
2016-04-27 15:34:01 +00:00
|
|
|
return err
|
2014-04-16 16:18:00 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// Command returns the named command on App. Returns nil if the command does not exist
|
2013-09-14 22:29:57 +00:00
|
|
|
func (a *App) Command(name string) *Command {
|
|
|
|
for _, c := range a.Commands {
|
|
|
|
if c.HasName(name) {
|
2016-05-22 19:20:52 +00:00
|
|
|
return c
|
2013-09-14 22:29:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-03 10:54:05 +00:00
|
|
|
// VisibleCategories returns a slice of categories and commands that are
|
|
|
|
// Hidden=false
|
2016-05-25 16:05:14 +00:00
|
|
|
func (a *App) VisibleCategories() []CommandCategory {
|
|
|
|
ret := []CommandCategory{}
|
|
|
|
for _, category := range a.Categories.Categories() {
|
|
|
|
if visible := func() CommandCategory {
|
2016-05-24 00:15:35 +00:00
|
|
|
if len(category.VisibleCommands()) > 0 {
|
|
|
|
return category
|
2016-05-03 10:54:05 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}(); visible != nil {
|
|
|
|
ret = append(ret, visible)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// VisibleCommands returns a slice of the Commands with Hidden=false
|
2016-05-22 19:20:52 +00:00
|
|
|
func (a *App) VisibleCommands() []*Command {
|
|
|
|
ret := []*Command{}
|
2016-05-03 10:54:05 +00:00
|
|
|
for _, command := range a.Commands {
|
|
|
|
if !command.Hidden {
|
|
|
|
ret = append(ret, command)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2016-05-01 12:36:17 +00:00
|
|
|
// VisibleFlags returns a slice of the Flags with Hidden=false
|
|
|
|
func (a *App) VisibleFlags() []Flag {
|
|
|
|
return visibleFlags(a.Flags)
|
|
|
|
}
|
|
|
|
|
2013-09-14 22:29:57 +00:00
|
|
|
func (a *App) hasFlag(flag Flag) bool {
|
|
|
|
for _, f := range a.Flags {
|
2016-05-22 01:29:45 +00:00
|
|
|
if reflect.DeepEqual(flag, f) {
|
2013-09-14 22:29:57 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-05-06 16:14:26 +00: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
|
|
|
|
}
|
|
|
|
|
2016-05-22 19:20:52 +00:00
|
|
|
func (a *App) appendFlag(fl Flag) {
|
|
|
|
if !hasFlag(a.Flags, fl) {
|
|
|
|
a.Flags = append(a.Flags, fl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) appendCommand(c *Command) {
|
|
|
|
if !hasCommand(a.Commands, c) {
|
|
|
|
a.Commands = append(a.Commands, c)
|
2013-09-14 22:29:57 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-30 23:04:52 +00: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
|
2016-05-22 19:20:52 +00:00
|
|
|
func (a *Author) String() string {
|
2015-01-30 23:04:52 +00:00
|
|
|
e := ""
|
|
|
|
if a.Email != "" {
|
2016-10-22 22:58:07 +00:00
|
|
|
e = " <" + a.Email + ">"
|
2015-01-30 23:04:52 +00:00
|
|
|
}
|
|
|
|
|
2016-10-22 22:58:07 +00:00
|
|
|
return fmt.Sprintf("%v%v", a.Name, e)
|
2015-01-30 23:04:52 +00:00
|
|
|
}
|