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"
|
2013-11-15 11:40:18 +00:00
|
|
|
"time"
|
2013-07-19 15:34:01 +00:00
|
|
|
)
|
|
|
|
|
2013-11-01 14:31:37 +00:00
|
|
|
// App is the main structure of a cli application. It is recomended that
|
2015-06-11 10:24:06 +00:00
|
|
|
// an app be created with the cli.NewApp() function
|
2013-07-19 15:34:01 +00:00
|
|
|
type App struct {
|
|
|
|
// The name of the program. Defaults to os.Args[0]
|
|
|
|
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-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
|
|
|
|
// List of commands to execute
|
|
|
|
Commands []Command
|
2013-07-20 22:50:13 +00:00
|
|
|
// List of flags to parse
|
|
|
|
Flags []Flag
|
2014-04-12 13:32:53 +00:00
|
|
|
// Boolean to enable bash completion commands
|
|
|
|
EnableBashCompletion bool
|
2014-07-13 13:16:30 +00:00
|
|
|
// Boolean to hide built-in help command
|
|
|
|
HideHelp bool
|
2014-11-12 11:38:58 +00:00
|
|
|
// Boolean to hide built-in version flag
|
|
|
|
HideVersion bool
|
2014-04-12 13:32:53 +00:00
|
|
|
// An action to execute when the bash-completion flag is set
|
|
|
|
BashComplete func(context *Context)
|
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
|
|
|
|
Before func(context *Context) error
|
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
|
2014-11-18 22:44:21 +00:00
|
|
|
After func(context *Context) error
|
2013-07-19 15:34:01 +00:00
|
|
|
// The action to execute when no subcommands are specified
|
2013-07-20 22:50:13 +00:00
|
|
|
Action func(context *Context)
|
2014-03-31 03:40:46 +00:00
|
|
|
// Execute this function if the proper command cannot be found
|
|
|
|
CommandNotFound func(context *Context, command string)
|
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
|
2015-01-30 23:04:52 +00:00
|
|
|
Authors []Author
|
2015-06-09 22:35:50 +00:00
|
|
|
// Copyright of the binary if any
|
|
|
|
Copyright string
|
2015-02-20 23:44:00 +00: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-02 04:50:04 +00:00
|
|
|
// Writer writer to write output to
|
|
|
|
Writer io.Writer
|
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
|
|
|
}
|
|
|
|
|
2013-11-01 14:31:37 +00:00
|
|
|
// Creates a new cli Application with some reasonable defaults for Name, Usage, Version and Action.
|
2013-07-19 15:34:01 +00:00
|
|
|
func NewApp() *App {
|
|
|
|
return &App{
|
2014-04-12 13:32:53 +00:00
|
|
|
Name: os.Args[0],
|
2015-08-13 04:43:14 +00:00
|
|
|
HelpName: os.Args[0],
|
2014-04-12 13:32:53 +00:00
|
|
|
Usage: "A new cli application",
|
|
|
|
Version: "0.0.0",
|
|
|
|
BashComplete: DefaultAppComplete,
|
|
|
|
Action: helpCommand.Action,
|
|
|
|
Compiled: compileTime(),
|
2014-12-02 04:50:04 +00:00
|
|
|
Writer: os.Stdout,
|
2013-07-19 15:34:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-01 14:31:37 +00:00
|
|
|
// Entry point to the cli app. Parses the arguments slice and routes to the proper flag/args combination
|
2015-01-09 23:51:06 +00:00
|
|
|
func (a *App) Run(arguments []string) (err error) {
|
2015-03-10 14:59:59 +00:00
|
|
|
if a.Author != "" || a.Email != "" {
|
2015-03-10 15:12:44 +00:00
|
|
|
a.Authors = append(a.Authors, Author{Name: a.Author, Email: a.Email})
|
2015-02-20 23:44:00 +00:00
|
|
|
}
|
|
|
|
|
2015-08-13 04:43:14 +00:00
|
|
|
newCmds := []Command{}
|
|
|
|
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
|
|
|
|
|
2013-07-20 15:44:09 +00:00
|
|
|
// append help to commands
|
2014-07-13 13:16:30 +00:00
|
|
|
if a.Command(helpCommand.Name) == nil && !a.HideHelp {
|
2013-09-14 22:29:57 +00:00
|
|
|
a.Commands = append(a.Commands, helpCommand)
|
2014-12-02 04:20:21 +00:00
|
|
|
if (HelpFlag != BoolFlag{}) {
|
|
|
|
a.appendFlag(HelpFlag)
|
|
|
|
}
|
2013-09-14 22:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//append version/help flags
|
2014-04-12 13:32:53 +00:00
|
|
|
if a.EnableBashCompletion {
|
|
|
|
a.appendFlag(BashCompletionFlag)
|
|
|
|
}
|
2014-11-12 11:38:58 +00:00
|
|
|
|
|
|
|
if !a.HideVersion {
|
|
|
|
a.appendFlag(VersionFlag)
|
|
|
|
}
|
2013-07-20 15:44:09 +00:00
|
|
|
|
2013-07-19 22:10:34 +00:00
|
|
|
// parse flags
|
2013-07-20 15:21:20 +00:00
|
|
|
set := flagSet(a.Name, a.Flags)
|
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)
|
|
|
|
if nerr != nil {
|
2014-12-02 04:57:35 +00:00
|
|
|
fmt.Fprintln(a.Writer, nerr)
|
2015-05-18 15:39:48 +00:00
|
|
|
context := NewContext(a, set, nil)
|
2013-11-20 08:05:18 +00:00
|
|
|
ShowAppHelp(context)
|
|
|
|
return nerr
|
|
|
|
}
|
2015-05-18 15:39:48 +00:00
|
|
|
context := NewContext(a, set, nil)
|
2013-09-04 19:58:31 +00:00
|
|
|
|
2013-07-24 14:35:45 +00:00
|
|
|
if err != nil {
|
2015-06-25 05:46:33 +00:00
|
|
|
fmt.Fprintln(a.Writer, "Incorrect Usage.")
|
2014-12-02 04:57:35 +00:00
|
|
|
fmt.Fprintln(a.Writer)
|
2015-06-25 05:46:33 +00:00
|
|
|
ShowAppHelp(context)
|
2013-11-01 13:23:19 +00:00
|
|
|
return err
|
2013-07-24 14:35:45 +00:00
|
|
|
}
|
2013-07-19 15:34:01 +00:00
|
|
|
|
2014-04-10 17:14:13 +00:00
|
|
|
if checkCompletions(context) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-01 13:45:19 +00:00
|
|
|
if checkHelp(context) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if checkVersion(context) {
|
|
|
|
return nil
|
|
|
|
}
|
2013-07-20 15:44:09 +00:00
|
|
|
|
2014-11-18 22:44:21 +00:00
|
|
|
if a.After != nil {
|
|
|
|
defer func() {
|
2015-06-02 04:11:20 +00:00
|
|
|
afterErr := a.After(context)
|
|
|
|
if afterErr != nil {
|
|
|
|
if err != nil {
|
|
|
|
err = NewMultiError(err, afterErr)
|
|
|
|
} else {
|
|
|
|
err = afterErr
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 22:44:21 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-01-01 21:00:20 +00:00
|
|
|
if a.Before != nil {
|
|
|
|
err := a.Before(context)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run default Action
|
|
|
|
a.Action(context)
|
2013-11-01 13:23:19 +00:00
|
|
|
return nil
|
2013-07-19 15:34:01 +00:00
|
|
|
}
|
2013-09-14 22:29:57 +00:00
|
|
|
|
2014-07-13 16:26:20 +00:00
|
|
|
// Another entry point to the cli app, takes care of passing arguments and error handling
|
|
|
|
func (a *App) RunAndExitOnError() {
|
|
|
|
if err := a.Run(os.Args); err != nil {
|
2014-12-02 04:57:35 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2014-07-13 16:26:20 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-16 16:18:00 +00:00
|
|
|
// Invokes the subcommand given the context, parses ctx.Args() to generate command-specific flags
|
2015-01-09 23:51:06 +00:00
|
|
|
func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
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 {
|
2014-04-17 16:48:00 +00:00
|
|
|
a.Commands = append(a.Commands, helpCommand)
|
2014-12-02 04:20:21 +00:00
|
|
|
if (HelpFlag != BoolFlag{}) {
|
|
|
|
a.appendFlag(HelpFlag)
|
|
|
|
}
|
2014-04-17 16:48:00 +00:00
|
|
|
}
|
2014-04-16 16:18:00 +00:00
|
|
|
}
|
|
|
|
|
2015-08-13 04:43:14 +00:00
|
|
|
newCmds := []Command{}
|
|
|
|
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
|
2014-04-16 16:18:00 +00:00
|
|
|
if a.EnableBashCompletion {
|
|
|
|
a.appendFlag(BashCompletionFlag)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse flags
|
|
|
|
set := flagSet(a.Name, a.Flags)
|
|
|
|
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())
|
|
|
|
}
|
2014-04-16 16:18:00 +00:00
|
|
|
return nerr
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2015-06-25 05:46:33 +00:00
|
|
|
fmt.Fprintln(a.Writer, "Incorrect Usage.")
|
|
|
|
fmt.Fprintln(a.Writer)
|
2014-04-16 16:18:00 +00:00
|
|
|
ShowSubcommandHelp(context)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if checkCompletions(context) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-04-17 16:48:00 +00:00
|
|
|
if len(a.Commands) > 0 {
|
|
|
|
if checkSubcommandHelp(context) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if checkCommandHelp(ctx, context.Args().First()) {
|
|
|
|
return nil
|
|
|
|
}
|
2014-04-16 16:18:00 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 22:44:21 +00:00
|
|
|
if a.After != nil {
|
|
|
|
defer func() {
|
2015-06-02 04:11:20 +00:00
|
|
|
afterErr := a.After(context)
|
|
|
|
if afterErr != nil {
|
|
|
|
if err != nil {
|
|
|
|
err = NewMultiError(err, afterErr)
|
|
|
|
} else {
|
|
|
|
err = afterErr
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 22:44:21 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-04-16 20:26:28 +00:00
|
|
|
if a.Before != nil {
|
|
|
|
err := a.Before(context)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2015-01-09 19:46:29 +00:00
|
|
|
a.Action(context)
|
2014-04-17 16:48:00 +00:00
|
|
|
|
2014-04-16 16:18:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-01 14:31:37 +00:00
|
|
|
// 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) {
|
|
|
|
return &c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) hasFlag(flag Flag) bool {
|
|
|
|
for _, f := range a.Flags {
|
|
|
|
if flag == f {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) appendFlag(flag Flag) {
|
|
|
|
if !a.hasFlag(flag) {
|
|
|
|
a.Flags = append(a.Flags, flag)
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
func (a Author) String() string {
|
|
|
|
e := ""
|
|
|
|
if a.Email != "" {
|
2015-02-20 23:44:00 +00:00
|
|
|
e = "<" + a.Email + "> "
|
2015-01-30 23:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%v %v", a.Name, e)
|
|
|
|
}
|