/
command.go
29 lines (24 loc) · 1017 Bytes
/
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
package alf
import (
"context"
"flag"
)
// A Command performs a task. A parent Delegator passes control to one of these.
type Command struct {
// Description should provide a short summary.
Description string
// Setup is passed the parent flag set and returns a flag set. Call the
// (*flag.FlagSet).Init method to reuse the parent flags and modify it as
// needed. You could also just allow the input flagset to pass through. If
// you don't want to share any flag data between parent and child, then
// create a new flag set.
Setup func(parentFlags flag.FlagSet) *flag.FlagSet
// Run is a wrapper function that selects the necessary command line inputs,
// executes the command and returns any errors.
Run func(ctx context.Context) error
flags *flag.FlagSet
}
// Summary provides a short, one-line description.
func (c *Command) Summary() string { return c.Description }
// Perform calls Run to execute the task at hand.
func (c *Command) Perform(ctx context.Context) error { return c.Run(ctx) }