JMS #4: Support for global flags in cli.Context

This commit is contained in:
Jeremy Saenz 2013-07-18 18:00:52 -07:00
parent 3119b075d6
commit 59b0ce24ef
3 changed files with 71 additions and 30 deletions

2
cli.go
View File

@ -24,7 +24,7 @@ func Run(args []string) {
set := flagSet(Flags) set := flagSet(Flags)
set.Parse(args[1:]) set.Parse(args[1:])
context := NewContext(set) context := NewContext(set, set)
if len(args) > 1 { if len(args) > 1 {
name := args[1] name := args[1]
for _, c := range append(Commands, HelpCommand) { for _, c := range append(Commands, HelpCommand) {

View File

@ -10,19 +10,54 @@ import (
// can be used to retrieve context-specific Args and // can be used to retrieve context-specific Args and
// parsed command-line options. // parsed command-line options.
type Context struct { type Context struct {
flagSet *flag.FlagSet flagSet *flag.FlagSet
globalSet *flag.FlagSet
} }
func NewContext(flagSet *flag.FlagSet) *Context { func NewContext(set *flag.FlagSet, globalSet *flag.FlagSet) *Context {
return &Context{flagSet} return &Context{set, globalSet}
} }
// Looks up the value of a local int flag, returns 0 if no int flag exists
func (c *Context) Int(name string) int { func (c *Context) Int(name string) int {
flag := c.flagSet.Lookup(name) return c.lookupInt(name, c.flagSet)
if flag != nil { }
val, err := strconv.Atoi(flag.Value.String())
// Looks up the value of a local bool flag, returns false if no bool flag exists
func (c *Context) Bool(name string) bool {
return c.lookupBool(name, c.flagSet)
}
// Looks up the value of a local string flag, returns "" if no string flag exists
func (c *Context) String(name string) string {
return c.lookupString(name, c.flagSet)
}
// Looks up the value of a global int flag, returns 0 if no int flag exists
func (c *Context) GlobalInt(name string) int {
return c.lookupInt(name, c.globalSet)
}
// Looks up the value of a global bool flag, returns false if no bool flag exists
func (c *Context) GlobalBool(name string) bool {
return c.lookupBool(name, c.globalSet)
}
// Looks up the value of a global string flag, returns "" if no string flag exists
func (c *Context) GlobalString(name string) string {
return c.lookupString(name, c.globalSet)
}
func (c *Context) Args() []string {
return c.flagSet.Args()
}
func (c *Context) lookupInt(name string, set *flag.FlagSet) int {
f := set.Lookup(name)
if f != nil {
val, err := strconv.Atoi(f.Value.String())
if err != nil { if err != nil {
panic(err) return 0
} }
return val return val
} else { } else {
@ -30,28 +65,24 @@ func (c *Context) Int(name string) int {
} }
} }
func (c *Context) Bool(name string) bool { func (c *Context) lookupString(name string, set *flag.FlagSet) string {
flag := c.flagSet.Lookup(name) f := set.Lookup(name)
if flag != nil { if f != nil {
val, err := strconv.ParseBool(flag.Value.String()) return f.Value.String()
} else {
return ""
}
}
func (c *Context) lookupBool(name string, set *flag.FlagSet) bool {
f := set.Lookup(name)
if f != nil {
val, err := strconv.ParseBool(f.Value.String())
if err != nil { if err != nil {
panic(err) return false
} }
return val return val
} else { } else {
return false return false
} }
} }
func (c *Context) String(name string) string {
flag := c.flagSet.Lookup(name)
if flag != nil {
return flag.Value.String()
} else {
return ""
}
}
func (c *Context) Args() []string {
return c.flagSet.Args()
}

View File

@ -5,31 +5,41 @@ import (
"testing" "testing"
) )
func Test_New(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.Int("myflag", 12, "doc")
globalSet := flag.NewFlagSet("test", 0)
globalSet.Int("myflag", 42, "doc")
c := NewContext(set, globalSet)
expect(t, c.Int("myflag"), 12)
expect(t, c.GlobalInt("myflag"), 42)
}
func Test_Int(t *testing.T) { func Test_Int(t *testing.T) {
set := flag.NewFlagSet("test", 0) set := flag.NewFlagSet("test", 0)
set.Int("myflag", 12, "doc") set.Int("myflag", 12, "doc")
c := NewContext(set) c := NewContext(set, set)
expect(t, c.Int("myflag"), 12) expect(t, c.Int("myflag"), 12)
} }
func Test_String(t *testing.T) { func Test_String(t *testing.T) {
set := flag.NewFlagSet("test", 0) set := flag.NewFlagSet("test", 0)
set.String("myflag", "hello world", "doc") set.String("myflag", "hello world", "doc")
c := NewContext(set) c := NewContext(set, set)
expect(t, c.String("myflag"), "hello world") expect(t, c.String("myflag"), "hello world")
} }
func Test_Bool(t *testing.T) { func Test_Bool(t *testing.T) {
set := flag.NewFlagSet("test", 0) set := flag.NewFlagSet("test", 0)
set.Bool("myflag", false, "doc") set.Bool("myflag", false, "doc")
c := NewContext(set) c := NewContext(set, set)
expect(t, c.Bool("myflag"), false) expect(t, c.Bool("myflag"), false)
} }
func Test_Args(t *testing.T) { func Test_Args(t *testing.T) {
set := flag.NewFlagSet("test", 0) set := flag.NewFlagSet("test", 0)
set.Bool("myflag", false, "doc") set.Bool("myflag", false, "doc")
c := NewContext(set) c := NewContext(set, set)
set.Parse([]string{"--myflag", "bat", "baz"}) set.Parse([]string{"--myflag", "bat", "baz"})
expect(t, len(c.Args()), 2) expect(t, len(c.Args()), 2)
expect(t, c.Bool("myflag"), true) expect(t, c.Bool("myflag"), true)