DRY error handling
This commit is contained in:
parent
cbd95292ac
commit
e67e05f617
19
app.go
19
app.go
@ -179,8 +179,9 @@ func (a *App) RunAsSubcommand(ctx *Context) error {
|
|||||||
|
|
||||||
context := NewContext(a, set, ctx.globalSet)
|
context := NewContext(a, set, ctx.globalSet)
|
||||||
|
|
||||||
if nerr != nil {
|
// Define here so it closes over the above variables
|
||||||
fmt.Println(nerr)
|
showErrAndHelp := func(err error) {
|
||||||
|
fmt.Println(err)
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
if len(a.Commands) > 0 {
|
if len(a.Commands) > 0 {
|
||||||
ShowSubcommandHelp(context)
|
ShowSubcommandHelp(context)
|
||||||
@ -188,18 +189,16 @@ func (a *App) RunAsSubcommand(ctx *Context) error {
|
|||||||
ShowCommandHelp(ctx, context.Args().First())
|
ShowCommandHelp(ctx, context.Args().First())
|
||||||
}
|
}
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if nerr != nil {
|
||||||
|
showErrAndHelp(nerr)
|
||||||
return nerr
|
return nerr
|
||||||
}
|
}
|
||||||
|
|
||||||
if cerr != nil {
|
if cerr != nil {
|
||||||
fmt.Println(cerr)
|
showErrAndHelp(cerr)
|
||||||
fmt.Println("")
|
|
||||||
if len(a.Commands) > 0 {
|
|
||||||
ShowSubcommandHelp(context)
|
|
||||||
} else {
|
|
||||||
ShowCommandHelp(ctx, context.Args().First())
|
|
||||||
}
|
|
||||||
fmt.Println("")
|
|
||||||
return cerr
|
return cerr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
20
command.go
20
command.go
@ -73,28 +73,28 @@ func (c Command) Run(ctx *Context) error {
|
|||||||
err = set.Parse(ctx.Args().Tail())
|
err = set.Parse(ctx.Args().Tail())
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
// Define here so it closes over the above variables
|
||||||
fmt.Printf("Incorrect Usage.\n\n")
|
showErrAndHelp := func(err error) {
|
||||||
|
fmt.Println(err)
|
||||||
|
fmt.Println("")
|
||||||
ShowCommandHelp(ctx, c.Name)
|
ShowCommandHelp(ctx, c.Name)
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
showErrAndHelp(fmt.Errorf("Incorrect Usage."))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
nerr := normalizeFlags(c.Flags, set)
|
nerr := normalizeFlags(c.Flags, set)
|
||||||
if nerr != nil {
|
if nerr != nil {
|
||||||
fmt.Println(nerr)
|
showErrAndHelp(nerr)
|
||||||
fmt.Println("")
|
|
||||||
ShowCommandHelp(ctx, c.Name)
|
|
||||||
fmt.Println("")
|
|
||||||
return nerr
|
return nerr
|
||||||
}
|
}
|
||||||
|
|
||||||
cerr := checkRequiredFlags(c.Flags, set)
|
cerr := checkRequiredFlags(c.Flags, set)
|
||||||
if cerr != nil {
|
if cerr != nil {
|
||||||
fmt.Println(cerr)
|
showErrAndHelp(cerr)
|
||||||
fmt.Println("")
|
|
||||||
ShowCommandHelp(ctx, c.Name)
|
|
||||||
fmt.Println("")
|
|
||||||
return cerr
|
return cerr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user