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

View File

@ -1,5 +1,11 @@
package cli
import (
"io/ioutil"
"os"
"fmt"
)
type Command struct {
Name string
ShortName string
@ -17,7 +23,15 @@ func (c Command) Run(ctx *Context) {
)
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)
checkCommandHelp(context, c.Name)