Skip to content

Commit 663895c

Browse files
committed
subcommand: add 'NewSubcommand()'
Add a 'genericSubcommand' type and "constructor" 'NewSubcommand()' to allow for specifying subcommands that do not directly implement the 'Subcommand' interface, but instead are specified with a static name, description, and run function. Signed-off-by: Victoria Dye <[email protected]>
1 parent d6ef794 commit 663895c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

internal/argparse/subcommand.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,33 @@ type Subcommand interface {
55
Description() string
66
Run(args []string) error
77
}
8+
9+
type genericSubcommand struct {
10+
nameStr string
11+
descriptionStr string
12+
runFunc func([]string) error
13+
}
14+
15+
func NewSubcommand(
16+
name string,
17+
description string,
18+
runFunc func([]string) error,
19+
) *genericSubcommand {
20+
return &genericSubcommand{
21+
nameStr: name,
22+
descriptionStr: description,
23+
runFunc: runFunc,
24+
}
25+
}
26+
27+
func (s *genericSubcommand) Name() string {
28+
return s.nameStr
29+
}
30+
31+
func (s *genericSubcommand) Description() string {
32+
return s.descriptionStr
33+
}
34+
35+
func (s *genericSubcommand) Run(args []string) error {
36+
return s.runFunc(args)
37+
}

0 commit comments

Comments
 (0)