Show usage and exit with error whenever arguments parsing fails
Signed-off-by: Damien Le Berrigaud <damien@pivotallabs.com>
This commit is contained in:
parent
16d10cc127
commit
3e07cbd8ba
9
app.go
9
app.go
@ -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)
|
||||
|
||||
|
16
command.go
16
command.go
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user