commit
b5844af298
39
app.go
39
app.go
@ -91,26 +91,26 @@ func (a *App) Run(arguments []string) error {
|
|||||||
|
|
||||||
context := NewContext(a, set, set)
|
context := NewContext(a, set, set)
|
||||||
|
|
||||||
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("")
|
||||||
ShowAppHelp(context)
|
ShowAppHelp(context)
|
||||||
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("")
|
|
||||||
ShowAppHelp(context)
|
|
||||||
fmt.Println("")
|
|
||||||
return cerr
|
return cerr
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Incorrect Usage.\n\n")
|
showErrAndHelp(fmt.Errorf("Incorrect Usage."))
|
||||||
ShowAppHelp(context)
|
|
||||||
fmt.Println("")
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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,20 +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)
|
|
||||||
fmt.Println("subcommands")
|
|
||||||
} else {
|
|
||||||
ShowCommandHelp(ctx, context.Args().First())
|
|
||||||
fmt.Println("commands")
|
|
||||||
}
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -340,6 +340,13 @@ func normalizeFlags(flags []Flag, set *flag.FlagSet) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func checkRequiredFlags(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)
|
visited := make(map[string]bool)
|
||||||
set.Visit(func(f *flag.Flag) {
|
set.Visit(func(f *flag.Flag) {
|
||||||
visited[f.Name] = true
|
visited[f.Name] = true
|
||||||
|
Loading…
Reference in New Issue
Block a user