-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutedcmd.go
More file actions
58 lines (48 loc) · 1.15 KB
/
routedcmd.go
File metadata and controls
58 lines (48 loc) · 1.15 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
package ctrl
import (
"flag"
"fmt"
)
type FlagCmd func(*flag.FlagSet) Cmd
type RoutedCmd struct {
name, help string
cmd Cmd
flags *flag.FlagSet
definesFlags bool
parallell *bool
}
func NewRoutedCmd(name string, cmd Cmd, help string, flags *flag.FlagSet) *RoutedCmd {
rc := &RoutedCmd{name: name, help: help, cmd: cmd, flags: flags}
if flags == nil {
rc.flags = flag.NewFlagSet(name, flag.ExitOnError)
} else {
rc.definesFlags = true
}
// universal cmd flags
// rc.parallell = rc.flags.Bool("P", false, "run in parallell")
return rc
}
//func printUniversalFlags() {
// fmt.Fprintf(os.Stderr, "\nall commands defines these flags:\n")
// fmt.Fprintf(os.Stderr, " -P=false: run in parallel\n")
//}
func (rc *RoutedCmd) Match(s string) bool {
return rc.name == s
}
func (rc *RoutedCmd) copy() *RoutedCmd {
rccopy := *rc
return &rccopy
}
func (rc *RoutedCmd) Print() {
fmt.Printf(" %s:\t%s\n", rc.name, rc.help)
if rc.definesFlags == true {
rc.flags.PrintDefaults()
}
}
func (rc *RoutedCmd) Parse(args []string) []string {
rc.flags.Parse(args)
return rc.flags.Args()
}
func (rc *RoutedCmd) GetCmd() Cmd {
return rc.cmd
}