2015-07-20 19:18:25 +00:00
|
|
|
package cli
|
2013-07-18 00:00:02 +00:00
|
|
|
|
|
|
|
import (
|
2013-07-19 00:39:42 +00:00
|
|
|
"flag"
|
2016-07-31 21:14:46 +00:00
|
|
|
"os"
|
2013-07-24 14:35:45 +00:00
|
|
|
"testing"
|
2014-08-02 21:32:32 +00:00
|
|
|
"time"
|
2013-07-18 00:00:02 +00:00
|
|
|
)
|
|
|
|
|
2013-07-20 22:53:25 +00:00
|
|
|
func TestNewContext(t *testing.T) {
|
2013-07-19 01:00:52 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Int("myflag", 12, "doc")
|
2016-06-16 15:13:32 +00:00
|
|
|
set.Int64("myflagInt64", int64(12), "doc")
|
|
|
|
set.Uint("myflagUint", uint(93), "doc")
|
|
|
|
set.Uint64("myflagUint64", uint64(93), "doc")
|
2016-04-28 17:03:25 +00:00
|
|
|
set.Float64("myflag64", float64(17), "doc")
|
2013-07-19 01:00:52 +00:00
|
|
|
globalSet := flag.NewFlagSet("test", 0)
|
|
|
|
globalSet.Int("myflag", 42, "doc")
|
2016-06-16 14:14:28 +00:00
|
|
|
globalSet.Int64("myflagInt64", int64(42), "doc")
|
2016-06-16 15:13:32 +00:00
|
|
|
globalSet.Uint("myflagUint", uint(33), "doc")
|
|
|
|
globalSet.Uint64("myflagUint64", uint64(33), "doc")
|
2016-04-28 17:03:25 +00:00
|
|
|
globalSet.Float64("myflag64", float64(47), "doc")
|
2015-07-20 19:18:25 +00:00
|
|
|
globalCtx := NewContext(nil, globalSet, nil)
|
|
|
|
command := Command{Name: "mycommand"}
|
|
|
|
c := NewContext(nil, set, globalCtx)
|
2014-03-29 19:02:05 +00:00
|
|
|
c.Command = command
|
2013-07-19 01:00:52 +00:00
|
|
|
expect(t, c.Int("myflag"), 12)
|
2016-06-11 22:41:24 +00:00
|
|
|
expect(t, c.Int64("myflagInt64"), int64(12))
|
2016-06-16 15:13:32 +00:00
|
|
|
expect(t, c.Uint("myflagUint"), uint(93))
|
|
|
|
expect(t, c.Uint64("myflagUint64"), uint64(93))
|
2016-04-28 17:03:25 +00:00
|
|
|
expect(t, c.Float64("myflag64"), float64(17))
|
2013-07-19 01:00:52 +00:00
|
|
|
expect(t, c.GlobalInt("myflag"), 42)
|
2016-06-11 22:41:24 +00:00
|
|
|
expect(t, c.GlobalInt64("myflagInt64"), int64(42))
|
2016-06-16 15:13:32 +00:00
|
|
|
expect(t, c.GlobalUint("myflagUint"), uint(33))
|
|
|
|
expect(t, c.GlobalUint64("myflagUint64"), uint64(33))
|
2016-04-28 17:03:25 +00:00
|
|
|
expect(t, c.GlobalFloat64("myflag64"), float64(47))
|
2014-03-29 19:02:05 +00:00
|
|
|
expect(t, c.Command.Name, "mycommand")
|
2013-07-19 01:00:52 +00:00
|
|
|
}
|
|
|
|
|
2013-07-20 22:53:25 +00:00
|
|
|
func TestContext_Int(t *testing.T) {
|
2013-07-19 00:39:42 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Int("myflag", 12, "doc")
|
2015-07-20 19:18:25 +00:00
|
|
|
c := NewContext(nil, set, nil)
|
2013-07-19 00:39:42 +00:00
|
|
|
expect(t, c.Int("myflag"), 12)
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
|
2016-06-11 22:41:24 +00:00
|
|
|
func TestContext_Int64(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Int64("myflagInt64", 12, "doc")
|
|
|
|
c := NewContext(nil, set, nil)
|
|
|
|
expect(t, c.Int64("myflagInt64"), int64(12))
|
|
|
|
}
|
|
|
|
|
2016-06-16 15:13:32 +00:00
|
|
|
func TestContext_Uint(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Uint("myflagUint", uint(13), "doc")
|
|
|
|
c := NewContext(nil, set, nil)
|
|
|
|
expect(t, c.Uint("myflagUint"), uint(13))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_Uint64(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Uint64("myflagUint64", uint64(9), "doc")
|
|
|
|
c := NewContext(nil, set, nil)
|
|
|
|
expect(t, c.Uint64("myflagUint64"), uint64(9))
|
|
|
|
}
|
|
|
|
|
2016-04-28 17:03:25 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-06-11 22:41:24 +00:00
|
|
|
func TestContext_GlobalInt64(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Int64("myflagInt64", 12, "doc")
|
|
|
|
c := NewContext(nil, set, nil)
|
|
|
|
expect(t, c.GlobalInt64("myflagInt64"), int64(12))
|
|
|
|
expect(t, c.GlobalInt64("nope"), int64(0))
|
|
|
|
}
|
|
|
|
|
2016-04-28 17:03:25 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2014-08-02 21:32:32 +00:00
|
|
|
func TestContext_Duration(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Duration("myflag", time.Duration(12*time.Second), "doc")
|
2015-07-20 19:18:25 +00:00
|
|
|
c := NewContext(nil, set, nil)
|
2014-08-02 21:32:32 +00:00
|
|
|
expect(t, c.Duration("myflag"), time.Duration(12*time.Second))
|
|
|
|
}
|
|
|
|
|
2013-07-20 22:53:25 +00:00
|
|
|
func TestContext_String(t *testing.T) {
|
2013-07-19 00:39:42 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.String("myflag", "hello world", "doc")
|
2015-07-20 19:18:25 +00:00
|
|
|
c := NewContext(nil, set, nil)
|
2013-07-19 00:39:42 +00:00
|
|
|
expect(t, c.String("myflag"), "hello world")
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
|
2013-07-20 22:53:25 +00:00
|
|
|
func TestContext_Bool(t *testing.T) {
|
2013-07-19 00:39:42 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", false, "doc")
|
2015-07-20 19:18:25 +00:00
|
|
|
c := NewContext(nil, set, nil)
|
2013-07-19 00:39:42 +00:00
|
|
|
expect(t, c.Bool("myflag"), false)
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 01:24:22 +00:00
|
|
|
func TestContext_BoolT(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", true, "doc")
|
2015-07-20 19:18:25 +00:00
|
|
|
c := NewContext(nil, set, nil)
|
2014-03-06 01:24:22 +00:00
|
|
|
expect(t, c.BoolT("myflag"), true)
|
|
|
|
}
|
|
|
|
|
2016-05-07 23:06:28 +00:00
|
|
|
func TestContext_GlobalBool(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
|
|
|
|
globalSet := flag.NewFlagSet("test-global", 0)
|
|
|
|
globalSet.Bool("myflag", false, "doc")
|
|
|
|
globalCtx := NewContext(nil, globalSet, nil)
|
|
|
|
|
|
|
|
c := NewContext(nil, set, globalCtx)
|
|
|
|
expect(t, c.GlobalBool("myflag"), false)
|
|
|
|
expect(t, c.GlobalBool("nope"), false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_GlobalBoolT(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
|
|
|
|
globalSet := flag.NewFlagSet("test-global", 0)
|
|
|
|
globalSet.Bool("myflag", true, "doc")
|
|
|
|
globalCtx := NewContext(nil, globalSet, nil)
|
|
|
|
|
|
|
|
c := NewContext(nil, set, globalCtx)
|
|
|
|
expect(t, c.GlobalBoolT("myflag"), true)
|
|
|
|
expect(t, c.GlobalBoolT("nope"), false)
|
|
|
|
}
|
|
|
|
|
2013-07-20 22:53:25 +00:00
|
|
|
func TestContext_Args(t *testing.T) {
|
2013-07-19 00:39:42 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", false, "doc")
|
2015-07-20 19:18:25 +00:00
|
|
|
c := NewContext(nil, set, nil)
|
2013-07-19 00:39:42 +00:00
|
|
|
set.Parse([]string{"--myflag", "bat", "baz"})
|
|
|
|
expect(t, len(c.Args()), 2)
|
|
|
|
expect(t, c.Bool("myflag"), true)
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
2013-12-18 18:09:16 +00:00
|
|
|
|
2016-02-21 13:57:11 +00:00
|
|
|
func TestContext_NArg(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", false, "doc")
|
|
|
|
c := NewContext(nil, set, nil)
|
|
|
|
set.Parse([]string{"--myflag", "bat", "baz"})
|
|
|
|
expect(t, c.NArg(), 2)
|
|
|
|
}
|
|
|
|
|
2013-12-18 18:09:16 +00:00
|
|
|
func TestContext_IsSet(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", false, "doc")
|
|
|
|
set.String("otherflag", "hello world", "doc")
|
2014-11-11 20:48:07 +00:00
|
|
|
globalSet := flag.NewFlagSet("test", 0)
|
|
|
|
globalSet.Bool("myflagGlobal", true, "doc")
|
2015-07-20 19:18:25 +00:00
|
|
|
globalCtx := NewContext(nil, globalSet, nil)
|
|
|
|
c := NewContext(nil, set, globalCtx)
|
2013-12-18 18:09:16 +00:00
|
|
|
set.Parse([]string{"--myflag", "bat", "baz"})
|
2014-11-11 20:48:07 +00:00
|
|
|
globalSet.Parse([]string{"--myflagGlobal", "bat", "baz"})
|
2013-12-18 18:09:16 +00:00
|
|
|
expect(t, c.IsSet("myflag"), true)
|
|
|
|
expect(t, c.IsSet("otherflag"), false)
|
|
|
|
expect(t, c.IsSet("bogusflag"), false)
|
2014-11-11 20:48:07 +00:00
|
|
|
expect(t, c.IsSet("myflagGlobal"), false)
|
|
|
|
}
|
|
|
|
|
2016-07-31 21:14:46 +00:00
|
|
|
// XXX Corresponds to hack in context.IsSet for flags with EnvVar field
|
|
|
|
// Should be moved to `flag_test` in v2
|
|
|
|
func TestContext_IsSet_fromEnv(t *testing.T) {
|
|
|
|
var timeoutIsSet, tIsSet, noEnvVarIsSet, nIsSet bool
|
|
|
|
|
|
|
|
os.Clearenv()
|
|
|
|
os.Setenv("APP_TIMEOUT_SECONDS", "15.5")
|
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
Float64Flag{Name: "timeout, t", EnvVar: "APP_TIMEOUT_SECONDS"},
|
|
|
|
Float64Flag{Name: "no-env-var, n"},
|
|
|
|
},
|
|
|
|
Action: func(ctx *Context) error {
|
|
|
|
timeoutIsSet = ctx.IsSet("timeout")
|
|
|
|
tIsSet = ctx.IsSet("t")
|
|
|
|
noEnvVarIsSet = ctx.IsSet("no-env-var")
|
|
|
|
nIsSet = ctx.IsSet("n")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"run"})
|
|
|
|
expect(t, timeoutIsSet, true)
|
|
|
|
expect(t, tIsSet, true)
|
|
|
|
expect(t, noEnvVarIsSet, false)
|
|
|
|
expect(t, nIsSet, false)
|
|
|
|
}
|
|
|
|
|
2014-11-11 20:48:07 +00:00
|
|
|
func TestContext_GlobalIsSet(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", false, "doc")
|
|
|
|
set.String("otherflag", "hello world", "doc")
|
|
|
|
globalSet := flag.NewFlagSet("test", 0)
|
|
|
|
globalSet.Bool("myflagGlobal", true, "doc")
|
|
|
|
globalSet.Bool("myflagGlobalUnset", true, "doc")
|
2015-07-20 19:18:25 +00:00
|
|
|
globalCtx := NewContext(nil, globalSet, nil)
|
|
|
|
c := NewContext(nil, set, globalCtx)
|
2014-11-11 20:48:07 +00:00
|
|
|
set.Parse([]string{"--myflag", "bat", "baz"})
|
|
|
|
globalSet.Parse([]string{"--myflagGlobal", "bat", "baz"})
|
|
|
|
expect(t, c.GlobalIsSet("myflag"), false)
|
|
|
|
expect(t, c.GlobalIsSet("otherflag"), false)
|
|
|
|
expect(t, c.GlobalIsSet("bogusflag"), false)
|
|
|
|
expect(t, c.GlobalIsSet("myflagGlobal"), true)
|
|
|
|
expect(t, c.GlobalIsSet("myflagGlobalUnset"), false)
|
2014-11-11 20:50:40 +00:00
|
|
|
expect(t, c.GlobalIsSet("bogusGlobal"), false)
|
2013-12-18 18:09:16 +00:00
|
|
|
}
|
2015-02-26 21:51:01 +00:00
|
|
|
|
2016-07-31 21:14:46 +00:00
|
|
|
// XXX Corresponds to hack in context.IsSet for flags with EnvVar field
|
|
|
|
// Should be moved to `flag_test` in v2
|
|
|
|
func TestContext_GlobalIsSet_fromEnv(t *testing.T) {
|
|
|
|
var timeoutIsSet, tIsSet, noEnvVarIsSet, nIsSet bool
|
|
|
|
|
|
|
|
os.Clearenv()
|
|
|
|
os.Setenv("APP_TIMEOUT_SECONDS", "15.5")
|
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
Float64Flag{Name: "timeout, t", EnvVar: "APP_TIMEOUT_SECONDS"},
|
|
|
|
Float64Flag{Name: "no-env-var, n"},
|
|
|
|
},
|
|
|
|
Commands: []Command{
|
|
|
|
{
|
|
|
|
Name: "hello",
|
|
|
|
Action: func(ctx *Context) error {
|
|
|
|
timeoutIsSet = ctx.GlobalIsSet("timeout")
|
|
|
|
tIsSet = ctx.GlobalIsSet("t")
|
|
|
|
noEnvVarIsSet = ctx.GlobalIsSet("no-env-var")
|
|
|
|
nIsSet = ctx.GlobalIsSet("n")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"run", "hello"})
|
|
|
|
expect(t, timeoutIsSet, true)
|
|
|
|
expect(t, tIsSet, true)
|
|
|
|
expect(t, noEnvVarIsSet, false)
|
|
|
|
expect(t, nIsSet, false)
|
|
|
|
}
|
|
|
|
|
2015-02-26 21:51:01 +00:00
|
|
|
func TestContext_NumFlags(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", false, "doc")
|
|
|
|
set.String("otherflag", "hello world", "doc")
|
|
|
|
globalSet := flag.NewFlagSet("test", 0)
|
|
|
|
globalSet.Bool("myflagGlobal", true, "doc")
|
2015-07-20 19:18:25 +00:00
|
|
|
globalCtx := NewContext(nil, globalSet, nil)
|
|
|
|
c := NewContext(nil, set, globalCtx)
|
2015-02-26 21:51:01 +00:00
|
|
|
set.Parse([]string{"--myflag", "--otherflag=foo"})
|
|
|
|
globalSet.Parse([]string{"--myflagGlobal"})
|
|
|
|
expect(t, c.NumFlags(), 2)
|
|
|
|
}
|
2016-04-28 20:05:14 +00:00
|
|
|
|
|
|
|
func TestContext_GlobalFlag(t *testing.T) {
|
|
|
|
var globalFlag string
|
|
|
|
var globalFlagSet bool
|
|
|
|
app := NewApp()
|
|
|
|
app.Flags = []Flag{
|
|
|
|
StringFlag{Name: "global, g", Usage: "global"},
|
|
|
|
}
|
2016-04-28 21:15:16 +00:00
|
|
|
app.Action = func(c *Context) error {
|
2016-04-28 20:05:14 +00:00
|
|
|
globalFlag = c.GlobalString("global")
|
|
|
|
globalFlagSet = c.GlobalIsSet("global")
|
2016-04-28 21:15:16 +00:00
|
|
|
return nil
|
2016-04-28 20:05:14 +00:00
|
|
|
}
|
|
|
|
app.Run([]string{"command", "-g", "foo"})
|
|
|
|
expect(t, globalFlag, "foo")
|
|
|
|
expect(t, globalFlagSet, true)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_GlobalFlagsInSubcommands(t *testing.T) {
|
|
|
|
subcommandRun := false
|
|
|
|
parentFlag := false
|
|
|
|
app := NewApp()
|
|
|
|
|
|
|
|
app.Flags = []Flag{
|
|
|
|
BoolFlag{Name: "debug, d", Usage: "Enable debugging"},
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Commands = []Command{
|
2016-05-09 13:40:09 +00:00
|
|
|
{
|
2016-04-28 20:05:14 +00:00
|
|
|
Name: "foo",
|
|
|
|
Flags: []Flag{
|
|
|
|
BoolFlag{Name: "parent, p", Usage: "Parent flag"},
|
|
|
|
},
|
|
|
|
Subcommands: []Command{
|
|
|
|
{
|
|
|
|
Name: "bar",
|
2016-04-28 20:20:02 +00:00
|
|
|
Action: func(c *Context) error {
|
2016-04-28 20:05:14 +00:00
|
|
|
if c.GlobalBool("debug") {
|
|
|
|
subcommandRun = true
|
|
|
|
}
|
|
|
|
if c.GlobalBool("parent") {
|
|
|
|
parentFlag = true
|
|
|
|
}
|
2016-04-28 20:20:02 +00:00
|
|
|
return nil
|
2016-04-28 20:05:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Run([]string{"command", "-d", "foo", "-p", "bar"})
|
|
|
|
|
|
|
|
expect(t, subcommandRun, true)
|
|
|
|
expect(t, parentFlag, true)
|
|
|
|
}
|
2016-04-30 15:25:05 +00:00
|
|
|
|
2015-06-08 20:21:26 +00:00
|
|
|
func TestContext_Set(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Int("int", 5, "an int")
|
2016-04-30 15:25:05 +00:00
|
|
|
c := NewContext(nil, set, nil)
|
2015-06-08 20:21:26 +00:00
|
|
|
|
|
|
|
c.Set("int", "1")
|
|
|
|
expect(t, c.Int("int"), 1)
|
|
|
|
}
|
2016-04-30 15:46:47 +00:00
|
|
|
|
|
|
|
func TestContext_GlobalSet(t *testing.T) {
|
|
|
|
gSet := flag.NewFlagSet("test", 0)
|
|
|
|
gSet.Int("int", 5, "an int")
|
|
|
|
|
|
|
|
set := flag.NewFlagSet("sub", 0)
|
|
|
|
set.Int("int", 3, "an int")
|
|
|
|
|
|
|
|
pc := NewContext(nil, gSet, nil)
|
|
|
|
c := NewContext(nil, set, pc)
|
|
|
|
|
|
|
|
c.Set("int", "1")
|
|
|
|
expect(t, c.Int("int"), 1)
|
|
|
|
expect(t, c.GlobalInt("int"), 5)
|
|
|
|
|
|
|
|
c.GlobalSet("int", "1")
|
|
|
|
expect(t, c.Int("int"), 1)
|
|
|
|
expect(t, c.GlobalInt("int"), 1)
|
|
|
|
}
|