forked from RedTeamPentesting/monsoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp.go
87 lines (66 loc) · 2.94 KB
/
help.go
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
86
87
package main
import (
"os"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// setupHelp sets the templates used by cobra to format the help messages.
func setupHelp(cmd *cobra.Command) {
cobra.AddTemplateFunc("wrapFlags", wrapFlags)
cmd.SetUsageTemplate(usageTemplateGlobal)
cmd.SetHelpCommand(&cobra.Command{
Use: "help [command]",
Short: "Help about any command",
Long: `Help provides help for any command in the application.
Simply type monsoon help [command] for full details.`,
Run: func(c *cobra.Command, args []string) {
cmd, _, e := c.Root().Find(args)
if cmd == nil || e != nil {
c.Printf("Unknown help topic %q\n", args)
_ = c.Root().Usage()
return
}
cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown
cmd.SetUsageTemplate(usageTemplateHelpCommand)
_ = cmd.Help()
},
})
}
// wrapFlags returns a help text for all flags wrapped at the terminal size.
func wrapFlags(f *pflag.FlagSet) string {
width := getTermWidth(int(os.Stdin.Fd()))
return f.FlagUsagesWrapped(width)
}
const usageTemplateGlobal = `Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} command [options]{{end}}{{if gt (len .Aliases) 0}}
Aliases:
{{.NameAndAliases}}{{end}}{{if .HasAvailableSubCommands}}
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
Options:
{{ wrapFlags .LocalFlags | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
Global Options:
{{ wrapFlags .InheritedFlags | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}{{if .HasExample}}
Use "{{.Parent.Name}} help {{.Name}}" for examples.{{end}}
`
const usageTemplateHelpCommand = `Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
Aliases:
{{.NameAndAliases}}{{end}}{{if .HasAvailableSubCommands}}
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
Options:
{{ wrapFlags .LocalFlags | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
Global Options:
{{ wrapFlags .InheritedFlags | trimTrailingWhitespaces}}{{end}}{{if .HasExample}}
Examples:
{{.Example}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`