Show usage and exit with error whenever arguments parsing fails

Signed-off-by: Damien Le Berrigaud <damien@pivotallabs.com>
This commit is contained in:
Casey McTaggart 2013-09-04 13:58:31 -06:00 committed by Damien Le Berrigaud
parent 16d10cc127
commit 3e07cbd8ba
2 changed files with 23 additions and 2 deletions

9
app.go
View File

@ -2,6 +2,8 @@ package cli
import ( import (
"os" "os"
"io/ioutil"
"fmt"
) )
type App struct { type App struct {
@ -40,12 +42,17 @@ func (a *App) Run(arguments []string) {
// parse flags // parse flags
set := flagSet(a.Name, a.Flags) set := flagSet(a.Name, a.Flags)
set.SetOutput(ioutil.Discard)
err := set.Parse(arguments[1:]) err := set.Parse(arguments[1:])
context := NewContext(a, set, set)
if err != nil { if err != nil {
fmt.Println("Incorrect Usage.\n")
ShowAppHelp(context)
fmt.Println("")
os.Exit(1) os.Exit(1)
} }
context := NewContext(a, set, set)
checkHelp(context) checkHelp(context)
checkVersion(context) checkVersion(context)

View File

@ -1,5 +1,11 @@
package cli package cli
import (
"io/ioutil"
"os"
"fmt"
)
type Command struct { type Command struct {
Name string Name string
ShortName string ShortName string
@ -17,7 +23,15 @@ func (c Command) Run(ctx *Context) {
) )
set := flagSet(c.Name, c.Flags) set := flagSet(c.Name, c.Flags)
set.Parse(ctx.Args()[1:]) set.SetOutput(ioutil.Discard)
err := set.Parse(ctx.Args()[1:])
if err != nil {
fmt.Println("Incorrect Usage.\n")
ShowCommandHelp(ctx, c.Name)
fmt.Println("")
os.Exit(1)
}
context := NewContext(ctx.App, set, ctx.globalSet) context := NewContext(ctx.App, set, ctx.globalSet)
checkCommandHelp(context, c.Name) checkCommandHelp(context, c.Name)