From cbd95292ac9c4ba7eb30ca121fbe3825ced64f72 Mon Sep 17 00:00:00 2001 From: jhowarth Date: Mon, 2 Mar 2015 11:18:59 -0800 Subject: [PATCH 1/4] Remove debugging --- app.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/app.go b/app.go index 31a9070..e87ed98 100644 --- a/app.go +++ b/app.go @@ -196,10 +196,8 @@ func (a *App) RunAsSubcommand(ctx *Context) error { fmt.Println("") if len(a.Commands) > 0 { ShowSubcommandHelp(context) - fmt.Println("subcommands") } else { ShowCommandHelp(ctx, context.Args().First()) - fmt.Println("commands") } fmt.Println("") return cerr From e67e05f617978eec7bba579a6c86f3d0c11ad96b Mon Sep 17 00:00:00 2001 From: jhowarth Date: Mon, 2 Mar 2015 11:56:29 -0800 Subject: [PATCH 2/4] DRY error handling --- app.go | 19 +++++++++---------- command.go | 20 ++++++++++---------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/app.go b/app.go index e87ed98..d91e6e3 100644 --- a/app.go +++ b/app.go @@ -179,8 +179,9 @@ func (a *App) RunAsSubcommand(ctx *Context) error { context := NewContext(a, set, ctx.globalSet) - if nerr != nil { - fmt.Println(nerr) + // Define here so it closes over the above variables + showErrAndHelp := func(err error) { + fmt.Println(err) fmt.Println("") if len(a.Commands) > 0 { ShowSubcommandHelp(context) @@ -188,18 +189,16 @@ func (a *App) RunAsSubcommand(ctx *Context) error { ShowCommandHelp(ctx, context.Args().First()) } fmt.Println("") + + } + + if nerr != nil { + showErrAndHelp(nerr) return nerr } if cerr != nil { - fmt.Println(cerr) - fmt.Println("") - if len(a.Commands) > 0 { - ShowSubcommandHelp(context) - } else { - ShowCommandHelp(ctx, context.Args().First()) - } - fmt.Println("") + showErrAndHelp(cerr) return cerr } diff --git a/command.go b/command.go index 2ade6fe..833552c 100644 --- a/command.go +++ b/command.go @@ -73,28 +73,28 @@ func (c Command) Run(ctx *Context) error { err = set.Parse(ctx.Args().Tail()) } - if err != nil { - fmt.Printf("Incorrect Usage.\n\n") + // Define here so it closes over the above variables + showErrAndHelp := func(err error) { + fmt.Println(err) + fmt.Println("") ShowCommandHelp(ctx, c.Name) fmt.Println("") + } + + if err != nil { + showErrAndHelp(fmt.Errorf("Incorrect Usage.")) return err } nerr := normalizeFlags(c.Flags, set) if nerr != nil { - fmt.Println(nerr) - fmt.Println("") - ShowCommandHelp(ctx, c.Name) - fmt.Println("") + showErrAndHelp(nerr) return nerr } cerr := checkRequiredFlags(c.Flags, set) if cerr != nil { - fmt.Println(cerr) - fmt.Println("") - ShowCommandHelp(ctx, c.Name) - fmt.Println("") + showErrAndHelp(cerr) return cerr } From 6023f370c1dfea78d4ff99a6ecc6be261347bfc9 Mon Sep 17 00:00:00 2001 From: jhowarth Date: Mon, 2 Mar 2015 12:00:21 -0800 Subject: [PATCH 3/4] dry error messages --- app.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/app.go b/app.go index d91e6e3..b2cdb57 100644 --- a/app.go +++ b/app.go @@ -91,26 +91,26 @@ func (a *App) Run(arguments []string) error { context := NewContext(a, set, set) - if nerr != nil { - fmt.Println(nerr) + // Define here so it closes over the above variables + showErrAndHelp := func(err error) { + fmt.Println(err) fmt.Println("") ShowAppHelp(context) fmt.Println("") + } + + if nerr != nil { + showErrAndHelp(nerr) return nerr } if cerr != nil { - fmt.Println(cerr) - fmt.Println("") - ShowAppHelp(context) - fmt.Println("") + showErrAndHelp(cerr) return cerr } if err != nil { - fmt.Printf("Incorrect Usage.\n\n") - ShowAppHelp(context) - fmt.Println("") + showErrAndHelp(fmt.Errorf("Incorrect Usage.")) return err } From 145da3210f41f401b1f42a08385d11ee8a80ec97 Mon Sep 17 00:00:00 2001 From: jhowarth Date: Mon, 2 Mar 2015 12:06:42 -0800 Subject: [PATCH 4/4] don't require flags when the help flag is included --- context.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/context.go b/context.go index b8fe7a6..f9b6f92 100644 --- a/context.go +++ b/context.go @@ -340,6 +340,13 @@ func normalizeFlags(flags []Flag, set *flag.FlagSet) error { } func checkRequiredFlags(flags []Flag, set *flag.FlagSet) error { + // If the help flag is included then none of the other flags are required. + for _, f := range flags { + if f.getName() == "help" { + return nil + } + } + visited := make(map[string]bool) set.Visit(func(f *flag.Flag) { visited[f.Name] = true