urfave-cli/command.go
Casey McTaggart 3e07cbd8ba Show usage and exit with error whenever arguments parsing fails
Signed-off-by: Damien Le Berrigaud <damien@pivotallabs.com>
2013-09-04 13:58:31 -06:00

45 lines
754 B
Go

package cli
import (
"io/ioutil"
"os"
"fmt"
)
type Command struct {
Name string
ShortName string
Usage string
Description string
Action func(context *Context)
Flags []Flag
}
func (c Command) Run(ctx *Context) {
// append help to flags
c.Flags = append(
c.Flags,
helpFlag{"show help"},
)
set := flagSet(c.Name, c.Flags)
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)
c.Action(context)
}
func (c Command) HasName(name string) bool {
return c.Name == name || c.ShortName == name
}