-
Notifications
You must be signed in to change notification settings - Fork 0
/
gofh.go
38 lines (31 loc) · 966 Bytes
/
gofh.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
package gofh
func Init() *Flags {
return &Flags{Commands: make([]*Command, 0)}
}
type Flags struct {
Commands []*Command
DefaultHandler func()
}
type Callback func(map[string]string)
func (self *Flags) HandleCommand(pattern string, callback Callback) {
self.Commands = append(self.Commands, &Command{Pattern: pattern, Callback: callback})
}
func (self *Flags) HandleCommandWithOptions(pattern string, options []*Option, callback Callback) {
self.Commands = append(self.Commands, &Command{Pattern: pattern, Callback: callback, Options: options})
}
func (self *Flags) SetDefaultHandler(handler func()) {
self.DefaultHandler = handler
}
func (self *Flags) Parse(args []string) {
for _, command := range self.Commands {
if command.Callback != nil {
if options := command.Matches(args); options != nil && command.Callback != nil {
command.Callback(options)
return
}
}
}
if self.DefaultHandler != nil {
self.DefaultHandler()
}
}