forked from flynn/flynn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
56 lines (48 loc) · 1.06 KB
/
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
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
// A simple sub command parser based on the flag package
// https://gist.github.com/srid/3949446
package main
import (
"flag"
"fmt"
"os"
)
type subCommand interface {
Name() string
DefineFlags(*flag.FlagSet)
Run(*flag.FlagSet)
}
type subCommandParser struct {
cmd subCommand
fs *flag.FlagSet
}
func ParseCommands(commands ...subCommand) {
scp := make(map[string]*subCommandParser, len(commands))
for _, cmd := range commands {
name := cmd.Name()
scp[name] = &subCommandParser{cmd, flag.NewFlagSet(name, flag.ExitOnError)}
cmd.DefineFlags(scp[name].fs)
}
oldUsage := flag.Usage
flag.Usage = func() {
oldUsage()
for name, sc := range scp {
fmt.Fprintf(os.Stderr, "\n# %s %s\n", os.Args[0], name)
sc.fs.PrintDefaults()
fmt.Fprintf(os.Stderr, "\n")
}
}
flag.Parse()
if flag.NArg() < 1 {
flag.Usage()
os.Exit(1)
}
cmdname := flag.Arg(0)
if sc, ok := scp[cmdname]; ok {
sc.fs.Parse(flag.Args()[1:])
sc.cmd.Run(sc.fs)
} else {
fmt.Fprintf(os.Stderr, "error: %s is not a valid command", cmdname)
flag.Usage()
os.Exit(1)
}
}