1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-01-08 04:57:31 +00:00

Help subcommand method

This commit is contained in:
diamondburned (Forefront) 2020-01-23 22:46:17 -08:00
parent 7af350ba55
commit a5d817b28d
2 changed files with 49 additions and 30 deletions

View file

@ -281,6 +281,8 @@ func (ctx *Context) HelpAdmin() string {
}
func (ctx *Context) help(hideAdmin bool) string {
const indent = " "
var help strings.Builder
// Generate the headers and descriptions
@ -291,7 +293,7 @@ func (ctx *Context) help(hideAdmin bool) string {
}
if ctx.Description != "" {
help.WriteString("\n " + ctx.Description)
help.WriteString("\n" + indent + ctx.Description)
}
if ctx.Flag.Is(AdminOnly) {
@ -310,7 +312,7 @@ func (ctx *Context) help(hideAdmin bool) string {
continue
}
help.WriteString(" " + ctx.Prefix + cmd.Command)
help.WriteString(indent + ctx.Prefix + cmd.Command)
switch {
case len(cmd.Usage()) > 0:
@ -326,34 +328,8 @@ func (ctx *Context) help(hideAdmin bool) string {
var subcommands = ctx.Subcommands()
for _, sub := range subcommands {
if sub.Flag.Is(AdminOnly) && hideAdmin {
continue
}
subHelp.WriteString(" " + sub.Command)
if sub.Description != "" {
subHelp.WriteString(": " + sub.Description)
}
subHelp.WriteByte('\n')
for _, cmd := range sub.Commands {
if cmd.Flag.Is(AdminOnly) && hideAdmin {
continue
}
subHelp.WriteString(" " +
ctx.Prefix + sub.Command + " " + cmd.Command)
switch {
case len(cmd.Usage()) > 0:
subHelp.WriteString(" " + strings.Join(cmd.Usage(), " "))
case cmd.Description != "":
subHelp.WriteString(": " + cmd.Description)
}
subHelp.WriteByte('\n')
if help := sub.Help(ctx.Prefix, indent, hideAdmin); help != "" {
subHelp.WriteString(help)
}
}

View file

@ -154,6 +154,49 @@ func (sub *Subcommand) ChangeCommandInfo(methodName, cmd, desc string) bool {
return false
}
func (sub *Subcommand) Help(prefix, indent string, hideAdmin bool) string {
if sub.Flag.Is(AdminOnly) && hideAdmin {
return ""
}
var subHelp string
if sub.Command != "" {
subHelp += indent + sub.Command
}
if sub.Description != "" {
if subHelp != "" {
subHelp += ": "
} else {
subHelp += indent
}
subHelp += sub.Description
}
subHelp += "\n"
for _, cmd := range sub.Commands {
if cmd.Flag.Is(AdminOnly) && hideAdmin {
continue
}
subHelp += indent + indent +
prefix + sub.Command + " " + cmd.Command
switch {
case len(cmd.Usage()) > 0:
subHelp += " " + strings.Join(cmd.Usage(), " ")
case cmd.Description != "":
subHelp += ": " + cmd.Description
}
subHelp += "\n"
}
return subHelp
}
func (sub *Subcommand) reflectCommands() error {
t := reflect.TypeOf(sub.command)
v := reflect.ValueOf(sub.command)