2013-07-18 00:00:02 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Context is a type that is passed through to
|
|
|
|
// each Handler action in a cli application. Context
|
|
|
|
// can be used to retrieve context-specific Args and
|
|
|
|
// parsed command-line options.
|
|
|
|
type Context struct {
|
|
|
|
flagSet *flag.FlagSet
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewContext(flagSet *flag.FlagSet) *Context {
|
|
|
|
return &Context{flagSet}
|
|
|
|
}
|
|
|
|
|
2013-07-19 00:29:06 +00:00
|
|
|
func (c *Context) Int(name string) int {
|
2013-07-18 00:00:02 +00:00
|
|
|
flag := c.flagSet.Lookup(name)
|
|
|
|
if flag != nil {
|
|
|
|
val, err := strconv.Atoi(flag.Value.String())
|
|
|
|
if err != nil {
|
2013-07-18 00:03:19 +00:00
|
|
|
panic(err)
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
return val
|
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 00:29:06 +00:00
|
|
|
func (c *Context) Bool(name string) bool {
|
2013-07-18 00:00:02 +00:00
|
|
|
flag := c.flagSet.Lookup(name)
|
|
|
|
if flag != nil {
|
|
|
|
val, err := strconv.ParseBool(flag.Value.String())
|
|
|
|
if err != nil {
|
2013-07-18 00:03:19 +00:00
|
|
|
panic(err)
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
return val
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 00:29:06 +00:00
|
|
|
func (c *Context) String(name string) string {
|
2013-07-18 00:00:02 +00:00
|
|
|
flag := c.flagSet.Lookup(name)
|
|
|
|
if flag != nil {
|
|
|
|
return flag.Value.String()
|
|
|
|
} else {
|
2013-07-18 00:03:19 +00:00
|
|
|
return ""
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) Args() []string {
|
2013-07-18 00:03:19 +00:00
|
|
|
return c.flagSet.Args()
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|