Repository navigation
Expand file tree
/
Copy pathhelp.go
More file actions
85 lines (70 loc) · 2.57 KB
/
Copy pathhelp.go
File metadata and controls
85 lines (70 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package cli
import (
"context"
"strings"
"github.com/urfave/cli/v3"
)
// helpWord is the one leftover argument a group answers rather than refuses.
// HideHelpCommand on the root removes urfave's help command from the whole tree, so
// without this the word reaches parentAction as an unknown command.
const helpWord = "help"
// helpFor answers "help" with the help of whatever the remaining arguments name.
// urfave's ShowCommandHelp descends one level per call, so the path is walked here, and
// an argument that names no command stays a usage fault.
func helpFor(ctx context.Context, cmd *cli.Command, path []string) error {
parent := cmd
for _, name := range path {
sub := cmd.Command(name)
if sub == nil {
return unknownCommand(cmd, name)
}
parent, cmd = cmd, sub
}
return showHelp(ctx, cmd, parent)
}
// showHelp prints one command's own help. A leaf is printed through its parent because
// that is urfave's only path that honors CustomHelpTemplate, the template carrying the
// inherited-options section.
func showHelp(ctx context.Context, cmd, parent *cli.Command) error {
switch {
case cmd.Root() == cmd:
return cli.ShowRootCommandHelp(cmd)
case parent != nil && parent != cmd && len(cmd.Commands) == 0:
return cli.ShowCommandHelp(ctx, parent, cmd.Name)
default:
return cli.ShowSubcommandHelp(cmd)
}
}
// attachInheritedOptions gives every leaf a help template that also lists the flags its
// parent declares. Urfave builds GLOBAL OPTIONS from the root's persistent flags alone,
// so a connection flag a leaf parses through its parent appears in no leaf's help. Only a
// group's flags are copied down, because the root's are that GLOBAL OPTIONS section.
func attachInheritedOptions(root *cli.Command) {
for _, group := range root.Commands {
section := inheritedOptions(group.Flags)
if section == "" {
continue
}
for _, leaf := range group.Commands {
if len(leaf.Commands) == 0 {
leaf.CustomHelpTemplate = cli.CommandHelpTemplate + section
}
}
}
}
// inheritedOptions renders the section. It is built in Go rather than by the template because
// urfave's template data is the leaf, which reaches the root's flags and never its parent's. A
// Local flag is left out because urfave does not apply one to a subcommand.
func inheritedOptions(flags []cli.Flag) string {
var section strings.Builder
for _, fl := range flags {
if local, ok := fl.(cli.LocalFlag); ok && local.IsLocal() {
continue
}
section.WriteString("\n " + fl.String())
}
if section.Len() == 0 {
return ""
}
return "\nINHERITED OPTIONS:" + section.String() + "\n"
}