| 81 | } |
| 82 | |
| 83 | func helpCommandAction(ctx context.Context, cmd *Command) error { |
| 84 | args := cmd.Args() |
| 85 | firstArg := args.First() |
| 86 | |
| 87 | tracef("doing help for cmd %[1]q with args %[2]q", cmd, args) |
| 88 | |
| 89 | // helpCommandAction is triggered in several ways: |
| 90 | // |
| 91 | // * the command has no user-defined Action (default action fallback) |
| 92 | // * the --help / -h flag was parsed (via cmd.checkHelp()) |
| 93 | // * the "help" subcommand (or "h" alias) was dispatched |
| 94 | // |
| 95 | // Possible invocations: |
| 96 | // |
| 97 | // $ app # default action; show root help |
| 98 | // $ app --help / -h # flag; show root help (ignores subsequent args) |
| 99 | // $ app help / h # subcommand; show root help |
| 100 | // $ app help / h foo # subcommand; show help for subcommand "foo" |
| 101 | // $ app --help / -h foo # flag; show help for subcommand "foo" |
| 102 | // $ app foo --help / -h # flag on subcommand; show help for "foo" |
| 103 | // $ app foo help / h # subcommand on subcommand; show help for "foo" |
| 104 | // $ app foo (no action) # default action on subcommand; show help for "foo" |
| 105 | |
| 106 | // Case 4. when executing a help command set the context to parent |
| 107 | // to allow resolution of subsequent args. This will transform |
| 108 | // $ app help foo |
| 109 | // to |
| 110 | // $ app foo |
| 111 | // which will then be handled as case 3 |
| 112 | if cmd.parent != nil && cmd.builtInHelp { |
| 113 | tracef("setting cmd to cmd.parent") |
| 114 | cmd = cmd.parent |
| 115 | } |
| 116 | |
| 117 | // Case 4. $ app help foo |
| 118 | // foo is the command for which help needs to be shown |
| 119 | if firstArg != "" { |
| 120 | /* if firstArg == "--" { |
| 121 | return nil |
| 122 | }*/ |
| 123 | tracef("returning ShowCommandHelp with %[1]q", firstArg) |
| 124 | return ShowCommandHelp(ctx, cmd, firstArg) |
| 125 | } |
| 126 | |
| 127 | // Case 1 & 2 |
| 128 | // Special case when running help on main app itself as opposed to individual |
| 129 | // commands/subcommands |
| 130 | if cmd.parent == nil { |
| 131 | tracef("returning ShowRootCommandHelp") |
| 132 | _ = ShowRootCommandHelp(cmd) |
| 133 | return nil |
| 134 | } |
| 135 | |
| 136 | // Case 3, 5 |
| 137 | if len(cmd.VisibleCommands()) == 0 { |
| 138 | tracef("running HelpPrinter with command %[1]q", cmd.Name) |
| 139 | return ShowCommandHelp(ctx, cmd.parent, cmd.Name) |
| 140 | } |