forked from alecthomas/kingpin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
42 lines (37 loc) · 760 Bytes
/
cmd.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
package kingpin
type CmdClause struct {
*flagGroup
*argGroup
name string
help string
dispatch Dispatch
}
func newCommand(name, help string) *CmdClause {
return &CmdClause{
flagGroup: newFlagGroup(),
argGroup: newArgGroup(),
name: name,
help: help,
}
}
func (c *CmdClause) Dispatch(dispatch Dispatch) *CmdClause {
c.dispatch = dispatch
return c
}
func (c *CmdClause) init() {
c.flagGroup.init()
c.argGroup.init()
}
func (c *CmdClause) parse(tokens tokens) (tokens, error) {
tokens, err := c.flagGroup.parse(tokens, false)
if err != nil {
return nil, err
}
if tokens, err = c.argGroup.parse(tokens); err != nil {
return nil, err
}
if c.dispatch != nil {
return tokens, c.dispatch()
}
return tokens, nil
}