parent
4733699ce3
commit
bd0f13d022
@ -79,6 +79,15 @@ func (c *Context) GlobalInt(name string) int {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Looks up the value of a global float64 flag, returns float64(0) if no float64
|
||||||
|
// flag exists
|
||||||
|
func (c *Context) GlobalFloat64(name string) float64 {
|
||||||
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
||||||
|
return lookupFloat64(name, fs)
|
||||||
|
}
|
||||||
|
return float64(0)
|
||||||
|
}
|
||||||
|
|
||||||
// Looks up the value of a global time.Duration flag, returns 0 if no time.Duration flag exists
|
// Looks up the value of a global time.Duration flag, returns 0 if no time.Duration flag exists
|
||||||
func (c *Context) GlobalDuration(name string) time.Duration {
|
func (c *Context) GlobalDuration(name string) time.Duration {
|
||||||
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
||||||
|
@ -9,14 +9,18 @@ import (
|
|||||||
func TestNewContext(t *testing.T) {
|
func TestNewContext(t *testing.T) {
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
set.Int("myflag", 12, "doc")
|
set.Int("myflag", 12, "doc")
|
||||||
|
set.Float64("myflag64", float64(17), "doc")
|
||||||
globalSet := flag.NewFlagSet("test", 0)
|
globalSet := flag.NewFlagSet("test", 0)
|
||||||
globalSet.Int("myflag", 42, "doc")
|
globalSet.Int("myflag", 42, "doc")
|
||||||
|
globalSet.Float64("myflag64", float64(47), "doc")
|
||||||
globalCtx := NewContext(nil, globalSet, nil)
|
globalCtx := NewContext(nil, globalSet, nil)
|
||||||
command := Command{Name: "mycommand"}
|
command := Command{Name: "mycommand"}
|
||||||
c := NewContext(nil, set, globalCtx)
|
c := NewContext(nil, set, globalCtx)
|
||||||
c.Command = command
|
c.Command = command
|
||||||
expect(t, c.Int("myflag"), 12)
|
expect(t, c.Int("myflag"), 12)
|
||||||
|
expect(t, c.Float64("myflag64"), float64(17))
|
||||||
expect(t, c.GlobalInt("myflag"), 42)
|
expect(t, c.GlobalInt("myflag"), 42)
|
||||||
|
expect(t, c.GlobalFloat64("myflag64"), float64(47))
|
||||||
expect(t, c.Command.Name, "mycommand")
|
expect(t, c.Command.Name, "mycommand")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,6 +31,29 @@ func TestContext_Int(t *testing.T) {
|
|||||||
expect(t, c.Int("myflag"), 12)
|
expect(t, c.Int("myflag"), 12)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContext_GlobalInt(t *testing.T) {
|
||||||
|
set := flag.NewFlagSet("test", 0)
|
||||||
|
set.Int("myflag", 12, "doc")
|
||||||
|
c := NewContext(nil, set, nil)
|
||||||
|
expect(t, c.GlobalInt("myflag"), 12)
|
||||||
|
expect(t, c.GlobalInt("nope"), 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContext_Float64(t *testing.T) {
|
||||||
|
set := flag.NewFlagSet("test", 0)
|
||||||
|
set.Float64("myflag", float64(17), "doc")
|
||||||
|
c := NewContext(nil, set, nil)
|
||||||
|
expect(t, c.Float64("myflag"), float64(17))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContext_GlobalFloat64(t *testing.T) {
|
||||||
|
set := flag.NewFlagSet("test", 0)
|
||||||
|
set.Float64("myflag", float64(17), "doc")
|
||||||
|
c := NewContext(nil, set, nil)
|
||||||
|
expect(t, c.GlobalFloat64("myflag"), float64(17))
|
||||||
|
expect(t, c.GlobalFloat64("nope"), float64(0))
|
||||||
|
}
|
||||||
|
|
||||||
func TestContext_Duration(t *testing.T) {
|
func TestContext_Duration(t *testing.T) {
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
set.Duration("myflag", time.Duration(12*time.Second), "doc")
|
set.Duration("myflag", time.Duration(12*time.Second), "doc")
|
||||||
|
Loading…
Reference in New Issue
Block a user