This repository has been archived by the owner on Dec 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp-command.go
111 lines (101 loc) · 2.71 KB
/
help-command.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package artillery
import (
"fmt"
"sort"
"strings"
"github.com/hashibuto/artillery/pkg/tg"
ns "github.com/hashibuto/nilshell"
)
type helpCommandArgs struct {
Verbose bool
Command []string
}
func makeHelpCommand() *Command {
return &Command{
Name: "help",
Description: "display the command set, and contextual help",
Arguments: []*Argument{
{
Name: "command",
Description: "command and subcommand if available",
IsArray: true,
CompletionFunc: func(prefix string, processor *Processor) []string {
commandNames := []string{}
for key := range processor.commandLookup {
if strings.HasPrefix(key, prefix) {
commandNames = append(commandNames, key)
}
}
return commandNames
},
},
},
OnExecute: func(ns Namespace, processor *Processor) error {
helpArgs := &helpCommandArgs{}
err := Reflect(ns, helpArgs)
if err != nil {
return err
}
if len(helpArgs.Command) == 0 {
fmt.Println()
groups := []string{}
byGroup := map[string][]*Command{}
for _, cmd := range processor.commandLookup {
_, ok := byGroup[cmd.Group]
if !ok {
groups = append(groups, cmd.Group)
byGroup[cmd.Group] = []*Command{}
}
byGroup[cmd.Group] = append(byGroup[cmd.Group], cmd)
}
// Alphabetize the groups
sort.Slice(groups, func(i, j int) bool {
return i < j
})
// Alphabetize within the groups
for _, group := range byGroup {
sort.Slice(group, func(i, j int) bool {
return group[i].Name < group[j].Name
})
}
for _, groupName := range groups {
group := byGroup[groupName]
if groupName == "" {
if processor.DefaultHeading == "" {
groupName = "commands"
} else {
groupName = processor.DefaultHeading
}
}
tg.Print(tg.Bold, tg.Blue, groupName, "\n\n", tg.Reset)
table := tg.NewTable("command", "description")
table.HideHeading = true
for _, cmd := range group {
table.Append(cmd.Name, cmd.Description)
}
table.Render()
fmt.Println()
}
} else {
var curCommand *Command
var ok bool
curLookup := processor.commandLookup
for _, cmdName := range helpArgs.Command {
curCommand, ok = curLookup[cmdName]
if !ok {
return fmt.Errorf("unknown command or subcommand \"%s\"", cmdName)
}
curLookup = curCommand.subCommandLookup
}
curCommand.DisplayHelp()
}
return nil
},
OnCompleteOverride: func(cmd *Command, tokens []any, processor *Processor) []*ns.AutoComplete {
// Everything after "help "
before := processor.beforeAndCursor[5:]
full := processor.full[5:]
return processor.OnComplete(before, processor.afterCursor, full)
},
}
}