aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/codegangsta/cli/app.go
diff options
context:
space:
mode:
Diffstat (limited to 'Godeps/_workspace/src/github.com/codegangsta/cli/app.go')
-rw-r--r--Godeps/_workspace/src/github.com/codegangsta/cli/app.go62
1 files changed, 39 insertions, 23 deletions
diff --git a/Godeps/_workspace/src/github.com/codegangsta/cli/app.go b/Godeps/_workspace/src/github.com/codegangsta/cli/app.go
index 9a15c0c03..1ea3fd0b1 100644
--- a/Godeps/_workspace/src/github.com/codegangsta/cli/app.go
+++ b/Godeps/_workspace/src/github.com/codegangsta/cli/app.go
@@ -5,18 +5,21 @@ import (
"io"
"io/ioutil"
"os"
+ "path"
"time"
)
-// App is the main structure of a cli application. It is recomended that
+// App is the main structure of a cli application. It is recommended that
// an app be created with the cli.NewApp() function
type App struct {
- // The name of the program. Defaults to os.Args[0]
+ // The name of the program. Defaults to path.Base(os.Args[0])
Name string
// Full name of command for help, defaults to Name
HelpName string
// Description of the program.
Usage string
+ // Text to override the USAGE section of help
+ UsageText string
// Description of the program argument format.
ArgsUsage string
// Version of the program
@@ -43,6 +46,10 @@ type App struct {
Action func(context *Context)
// Execute this function if the proper command cannot be found
CommandNotFound func(context *Context, command string)
+ // Execute this function, if an usage error occurs. This is useful for displaying customized usage error messages.
+ // This function is able to replace the original error messages.
+ // If this function is not set, the "Incorrect usage" is displayed and the execution is interrupted.
+ OnUsageError func(context *Context, err error, isSubcommand bool) error
// Compilation date
Compiled time.Time
// List of all authors who contributed
@@ -70,9 +77,10 @@ func compileTime() time.Time {
// Creates a new cli Application with some reasonable defaults for Name, Usage, Version and Action.
func NewApp() *App {
return &App{
- Name: os.Args[0],
- HelpName: os.Args[0],
+ Name: path.Base(os.Args[0]),
+ HelpName: path.Base(os.Args[0]),
Usage: "A new cli application",
+ UsageText: "",
Version: "0.0.0",
BashComplete: DefaultAppComplete,
Action: helpCommand.Action,
@@ -118,25 +126,28 @@ func (a *App) Run(arguments []string) (err error) {
set.SetOutput(ioutil.Discard)
err = set.Parse(arguments[1:])
nerr := normalizeFlags(a.Flags, set)
+ context := NewContext(a, set, nil)
if nerr != nil {
fmt.Fprintln(a.Writer, nerr)
- context := NewContext(a, set, nil)
ShowAppHelp(context)
return nerr
}
- context := NewContext(a, set, nil)
-
- if err != nil {
- fmt.Fprintln(a.Writer, "Incorrect Usage.")
- fmt.Fprintln(a.Writer)
- ShowAppHelp(context)
- return err
- }
if checkCompletions(context) {
return nil
}
+ if err != nil {
+ if a.OnUsageError != nil {
+ err := a.OnUsageError(context, err, false)
+ return err
+ } else {
+ fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
+ ShowAppHelp(context)
+ return err
+ }
+ }
+
if !a.HideHelp && checkHelp(context) {
ShowAppHelp(context)
return nil
@@ -149,8 +160,7 @@ func (a *App) Run(arguments []string) (err error) {
if a.After != nil {
defer func() {
- afterErr := a.After(context)
- if afterErr != nil {
+ if afterErr := a.After(context); afterErr != nil {
if err != nil {
err = NewMultiError(err, afterErr)
} else {
@@ -161,8 +171,10 @@ func (a *App) Run(arguments []string) (err error) {
}
if a.Before != nil {
- err := a.Before(context)
+ err = a.Before(context)
if err != nil {
+ fmt.Fprintf(a.Writer, "%v\n\n", err)
+ ShowAppHelp(context)
return err
}
}
@@ -233,17 +245,21 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
return nerr
}
- if err != nil {
- fmt.Fprintln(a.Writer, "Incorrect Usage.")
- fmt.Fprintln(a.Writer)
- ShowSubcommandHelp(context)
- return err
- }
-
if checkCompletions(context) {
return nil
}
+ if err != nil {
+ if a.OnUsageError != nil {
+ err = a.OnUsageError(context, err, true)
+ return err
+ } else {
+ fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
+ ShowSubcommandHelp(context)
+ return err
+ }
+ }
+
if len(a.Commands) > 0 {
if checkSubcommandHelp(context) {
return nil