-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_rootcommand_test.go
More file actions
158 lines (119 loc) · 3.6 KB
/
example_rootcommand_test.go
File metadata and controls
158 lines (119 loc) · 3.6 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package cmder_test
import (
"context"
"fmt"
"github.com/brandon1024/cmder"
)
func ExampleRootCommand() {
cmd := &ParentCommand{
subcommands: []cmder.Command{&ChildCommand{}},
}
args := []string{"child", "hello-world"}
if err := cmder.Execute(context.Background(), cmd, cmder.WithArgs(args)); err != nil {
fmt.Printf("unexpected error occurred: %v", err)
}
// Output:
// parent: init [child hello-world]
// child: init [hello-world]
// child: run [hello-world]
// child: destroy [hello-world]
// parent: destroy [child hello-world]
}
// === PARENT COMMAND ===
const ParentCommandUsageLine = `parent [<subcommand>] [<args>...]`
const ParentCommandShortHelpText = `Example of parent command`
const ParentCommandHelpText = `
'parent' demonstrates an example of a command with subcommands. When executed without any arguments, the parent's Run
routine is executed, but if the child subcommand is provided the child subcommand Run routine will be executed instead.
The parent implements RootCommand indicating that it is a root command with runnable subcommands. The child does not
implement RootCommand, indicating it is a leaf command.
In this example, the parent and child commands implement RunnableLifecycle, and their respective init/destroy routines
are invoked in this order:
1. parent Initialize
2. child Initialize
3. child Run
4. child Destroy
5. parent Destroy
`
const ParentCommandExamples = `
# run the parent 'Run' routine
parent
# run the child 'Run' routine
parent child
# run with some additional args
parent hello-world
parent child hello-world
`
type ParentCommand struct {
subcommands []cmder.Command
}
func (c *ParentCommand) Name() string {
return "parent"
}
func (c *ParentCommand) Initialize(ctx context.Context, args []string) error {
fmt.Printf("%s: init %v\n", c.Name(), args)
return nil
}
func (c *ParentCommand) Run(ctx context.Context, args []string) error {
fmt.Printf("%s: run %v\n", c.Name(), args)
return nil
}
func (c *ParentCommand) Destroy(ctx context.Context, args []string) error {
fmt.Printf("%s: destroy %v\n", c.Name(), args)
return nil
}
func (c *ParentCommand) UsageLine() string {
return ParentCommandUsageLine
}
func (c *ParentCommand) ShortHelpText() string {
return ParentCommandShortHelpText
}
func (c *ParentCommand) HelpText() string {
return ParentCommandHelpText
}
func (c *ParentCommand) ExampleText() string {
return ParentCommandExamples
}
func (c *ParentCommand) Subcommands() []cmder.Command {
return c.subcommands
}
// === CHILD COMMAND ===
const ChildCommandUsageLine = `child [<args>...]`
const ChildCommandShortHelpText = `Example of child command`
const ChildCommandHelpText = `
'child' is the subcommand of 'parent'.
`
const ChildCommandExamples = `
# run the child 'Run' routine
parent child
# run with some additional args
parent child hello-world
`
type ChildCommand struct{}
func (c *ChildCommand) Name() string {
return "child"
}
func (c *ChildCommand) Initialize(ctx context.Context, args []string) error {
fmt.Printf("%s: init %v\n", c.Name(), args)
return nil
}
func (c *ChildCommand) Run(ctx context.Context, args []string) error {
fmt.Printf("%s: run %v\n", c.Name(), args)
return nil
}
func (c *ChildCommand) Destroy(ctx context.Context, args []string) error {
fmt.Printf("%s: destroy %v\n", c.Name(), args)
return nil
}
func (c *ChildCommand) UsageLine() string {
return ChildCommandUsageLine
}
func (c *ChildCommand) ShortHelpText() string {
return ChildCommandShortHelpText
}
func (c *ChildCommand) HelpText() string {
return ChildCommandHelpText
}
func (c *ChildCommand) ExampleText() string {
return ChildCommandExamples
}