2013-07-19 15:34:01 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2013-09-04 19:58:31 +00:00
|
|
|
"fmt"
|
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
|
|
|
|
// and 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
|
|
|
|
// Description of the program.
|
|
|
|
Usage string
|
|
|
|
// 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
|
|
|
|
// 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
|
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)
|
2013-11-15 11:40:18 +00:00
|
|
|
// Compilation date
|
|
|
|
Compiled time.Time
|
|
|
|
// Author
|
|
|
|
Author string
|
|
|
|
// Author e-mail
|
|
|
|
Email string
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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],
|
|
|
|
Usage: "A new cli application",
|
|
|
|
Version: "0.0.0",
|
|
|
|
BashComplete: DefaultAppComplete,
|
|
|
|
Action: helpCommand.Action,
|
|
|
|
Compiled: compileTime(),
|
|
|
|
Author: "Author",
|
|
|
|
Email: "unknown@email",
|
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
|
2013-11-01 13:23:19 +00:00
|
|
|
func (a *App) Run(arguments []string) error {
|
2013-07-20 15:44:09 +00:00
|
|
|
// append help to commands
|
2013-09-14 22:29:57 +00:00
|
|
|
if a.Command(helpCommand.Name) == nil {
|
|
|
|
a.Commands = append(a.Commands, helpCommand)
|
|
|
|
}
|
|
|
|
|
|
|
|
//append version/help flags
|
2014-04-12 13:32:53 +00:00
|
|
|
if a.EnableBashCompletion {
|
|
|
|
a.appendFlag(BashCompletionFlag)
|
|
|
|
}
|
2013-12-02 22:55:24 +00:00
|
|
|
a.appendFlag(BoolFlag{"version, v", "print the version"})
|
2013-11-21 01:24:37 +00:00
|
|
|
a.appendFlag(BoolFlag{"help, h", "show help"})
|
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)
|
2013-07-24 14:35:45 +00:00
|
|
|
err := set.Parse(arguments[1:])
|
2013-11-20 08:05:18 +00:00
|
|
|
nerr := normalizeFlags(a.Flags, set)
|
|
|
|
if nerr != nil {
|
|
|
|
fmt.Println(nerr)
|
|
|
|
context := NewContext(a, set, set)
|
|
|
|
ShowAppHelp(context)
|
|
|
|
fmt.Println("")
|
|
|
|
return nerr
|
|
|
|
}
|
2013-09-04 19:58:31 +00:00
|
|
|
context := NewContext(a, set, set)
|
|
|
|
|
2013-07-24 14:35:45 +00:00
|
|
|
if err != nil {
|
2013-11-15 10:35:24 +00:00
|
|
|
fmt.Printf("Incorrect Usage.\n\n")
|
2013-09-04 19:58:31 +00:00
|
|
|
ShowAppHelp(context)
|
|
|
|
fmt.Println("")
|
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-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-04-16 16:18:00 +00:00
|
|
|
// Invokes the subcommand given the context, parses ctx.Args() to generate command-specific flags
|
|
|
|
func (a *App) RunAsSubcommand(c *Context) error {
|
|
|
|
// append help to commands
|
|
|
|
if a.Command(helpCommand.Name) == nil {
|
|
|
|
a.Commands = append(a.Commands, helpCommand)
|
|
|
|
}
|
|
|
|
|
|
|
|
// append help flags
|
|
|
|
if a.EnableBashCompletion {
|
|
|
|
a.appendFlag(BashCompletionFlag)
|
|
|
|
}
|
|
|
|
a.appendFlag(BoolFlag{"help, h", "show help"})
|
|
|
|
|
|
|
|
// parse flags
|
|
|
|
set := flagSet(a.Name, a.Flags)
|
|
|
|
set.SetOutput(ioutil.Discard)
|
|
|
|
err := set.Parse(c.Args().Tail())
|
|
|
|
nerr := normalizeFlags(a.Flags, set)
|
|
|
|
context := NewContext(a, set, set)
|
|
|
|
|
|
|
|
if nerr != nil {
|
|
|
|
fmt.Println(nerr)
|
|
|
|
ShowSubcommandHelp(context)
|
|
|
|
fmt.Println("")
|
|
|
|
return nerr
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Incorrect Usage.\n\n")
|
|
|
|
ShowSubcommandHelp(context)
|
|
|
|
fmt.Println("")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if checkCompletions(context) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if checkSubcommandHelp(context) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
args := context.Args()
|
|
|
|
if args.Present() {
|
|
|
|
name := args.First()
|
|
|
|
c := a.Command(name)
|
|
|
|
if c != nil {
|
|
|
|
return c.Run(context)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run default Action
|
|
|
|
a.Action(context)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|