/
help.go
69 lines (52 loc) · 1.47 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
package exec
import (
"fmt"
"strings"
"github.com/aergoio/aergo/v2/cmd/brick/context"
"github.com/aergoio/aergo/v2/types"
)
func init() {
registerExec(&help{})
}
type help struct{}
func (c *help) Command() string {
return "help"
}
func (c *help) Syntax() string {
return context.CommandSymbol
}
func (c *help) Usage() string {
return "help [command]"
}
func (c *help) Describe() string {
return "print usages and descriptions of commands"
}
func (c *help) Validate(args string) error {
if args == "" {
return nil
}
executor := GetExecutor(args)
if executor == nil {
return fmt.Errorf("command not found")
}
return nil
}
func (c *help) Run(args string) (string, uint64, []*types.Event, error) {
var result strings.Builder
if args == "" {
// print whole usage guide
result.WriteString(fmt.Sprintf("Aergo Brick, Toy for Developing Contracts, version %s\n", context.GitHash))
result.WriteString(fmt.Sprintf("\n%-12s%s\n", "Command", "Usage"))
result.WriteString(fmt.Sprintf("=====================================================\n"))
for _, executor := range AllExecutors() {
result.WriteString(fmt.Sprintf("%-12s%s\n", executor.Command(), executor.Usage()))
}
return result.String(), 0, nil, nil
}
// print details of a specific command
executor := GetExecutor(args)
if executor != nil {
result.WriteString(fmt.Sprintf("%s\tusage: %s\tdescr: %s", executor.Command(), executor.Usage(), executor.Describe()))
}
return result.String(), 0, nil, nil
}