commit
b5844af298
39
app.go
39
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
|
||||
}
|
||||
|
||||
@ -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,20 +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)
|
||||
fmt.Println("subcommands")
|
||||
} else {
|
||||
ShowCommandHelp(ctx, context.Args().First())
|
||||
fmt.Println("commands")
|
||||
}
|
||||
fmt.Println("")
|
||||
showErrAndHelp(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())
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user