|
|
@ -2,6 +2,7 @@ package cli
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"flag"
|
|
|
|
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
)
|
|
|
@ -19,8 +20,6 @@ func TestNewContext(t *testing.T) {
|
|
|
|
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.Float64("myflag64"), float64(17))
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -31,14 +30,6 @@ 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) {
|
|
|
|
func TestContext_Float64(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Float64("myflag", float64(17), "doc")
|
|
|
|
set.Float64("myflag", float64(17), "doc")
|
|
|
@ -46,14 +37,6 @@ func TestContext_Float64(t *testing.T) {
|
|
|
|
expect(t, c.Float64("myflag"), float64(17))
|
|
|
|
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")
|
|
|
@ -82,30 +65,6 @@ func TestContext_BoolT(t *testing.T) {
|
|
|
|
expect(t, c.BoolT("myflag"), true)
|
|
|
|
expect(t, c.BoolT("myflag"), true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestContext_Args(t *testing.T) {
|
|
|
|
func TestContext_Args(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", false, "doc")
|
|
|
|
set.Bool("myflag", false, "doc")
|
|
|
@ -125,37 +84,22 @@ func TestContext_NArg(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
|
|
func TestContext_IsSet(t *testing.T) {
|
|
|
|
func TestContext_IsSet(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", false, "doc")
|
|
|
|
set.Bool("one-flag", false, "doc")
|
|
|
|
set.String("otherflag", "hello world", "doc")
|
|
|
|
set.Bool("two-flag", false, "doc")
|
|
|
|
globalSet := flag.NewFlagSet("test", 0)
|
|
|
|
set.String("three-flag", "hello world", "doc")
|
|
|
|
globalSet.Bool("myflagGlobal", true, "doc")
|
|
|
|
parentSet := flag.NewFlagSet("test", 0)
|
|
|
|
globalCtx := NewContext(nil, globalSet, nil)
|
|
|
|
parentSet.Bool("top-flag", true, "doc")
|
|
|
|
c := NewContext(nil, set, globalCtx)
|
|
|
|
parentCtx := NewContext(nil, parentSet, nil)
|
|
|
|
set.Parse([]string{"--myflag", "bat", "baz"})
|
|
|
|
ctx := NewContext(nil, set, parentCtx)
|
|
|
|
globalSet.Parse([]string{"--myflagGlobal", "bat", "baz"})
|
|
|
|
|
|
|
|
expect(t, c.IsSet("myflag"), true)
|
|
|
|
|
|
|
|
expect(t, c.IsSet("otherflag"), false)
|
|
|
|
|
|
|
|
expect(t, c.IsSet("bogusflag"), false)
|
|
|
|
|
|
|
|
expect(t, c.IsSet("myflagGlobal"), false)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestContext_GlobalIsSet(t *testing.T) {
|
|
|
|
set.Parse([]string{"--one-flag", "--two-flag", "--three-flag", "frob"})
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
parentSet.Parse([]string{"--top-flag"})
|
|
|
|
set.Bool("myflag", false, "doc")
|
|
|
|
|
|
|
|
set.String("otherflag", "hello world", "doc")
|
|
|
|
expect(t, ctx.IsSet("one-flag"), true)
|
|
|
|
globalSet := flag.NewFlagSet("test", 0)
|
|
|
|
expect(t, ctx.IsSet("two-flag"), true)
|
|
|
|
globalSet.Bool("myflagGlobal", true, "doc")
|
|
|
|
expect(t, ctx.IsSet("three-flag"), true)
|
|
|
|
globalSet.Bool("myflagGlobalUnset", true, "doc")
|
|
|
|
expect(t, ctx.IsSet("top-flag"), true)
|
|
|
|
globalCtx := NewContext(nil, globalSet, nil)
|
|
|
|
expect(t, ctx.IsSet("bogus"), false)
|
|
|
|
c := NewContext(nil, set, globalCtx)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
expect(t, c.GlobalIsSet("bogusGlobal"), false)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestContext_NumFlags(t *testing.T) {
|
|
|
|
func TestContext_NumFlags(t *testing.T) {
|
|
|
@ -171,86 +115,82 @@ func TestContext_NumFlags(t *testing.T) {
|
|
|
|
expect(t, c.NumFlags(), 2)
|
|
|
|
expect(t, c.NumFlags(), 2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestContext_GlobalFlag(t *testing.T) {
|
|
|
|
func TestContext_Set(t *testing.T) {
|
|
|
|
var globalFlag string
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
var globalFlagSet bool
|
|
|
|
set.Int("int", 5, "an int")
|
|
|
|
app := NewApp()
|
|
|
|
c := NewContext(nil, set, nil)
|
|
|
|
app.Flags = []Flag{
|
|
|
|
|
|
|
|
StringFlag{Name: "global, g", Usage: "global"},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Action = func(c *Context) error {
|
|
|
|
|
|
|
|
globalFlag = c.GlobalString("global")
|
|
|
|
|
|
|
|
globalFlagSet = c.GlobalIsSet("global")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Run([]string{"command", "-g", "foo"})
|
|
|
|
|
|
|
|
expect(t, globalFlag, "foo")
|
|
|
|
|
|
|
|
expect(t, globalFlagSet, true)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
c.Set("int", "1")
|
|
|
|
|
|
|
|
expect(t, c.Int("int"), 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestContext_GlobalFlagsInSubcommands(t *testing.T) {
|
|
|
|
func TestContext_LocalFlagNames(t *testing.T) {
|
|
|
|
subcommandRun := false
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
parentFlag := false
|
|
|
|
set.Bool("one-flag", false, "doc")
|
|
|
|
app := NewApp()
|
|
|
|
set.String("two-flag", "hello world", "doc")
|
|
|
|
|
|
|
|
parentSet := flag.NewFlagSet("test", 0)
|
|
|
|
|
|
|
|
parentSet.Bool("top-flag", true, "doc")
|
|
|
|
|
|
|
|
parentCtx := NewContext(nil, parentSet, nil)
|
|
|
|
|
|
|
|
ctx := NewContext(nil, set, parentCtx)
|
|
|
|
|
|
|
|
set.Parse([]string{"--one-flag", "--two-flag=foo"})
|
|
|
|
|
|
|
|
parentSet.Parse([]string{"--top-flag"})
|
|
|
|
|
|
|
|
|
|
|
|
app.Flags = []Flag{
|
|
|
|
actualFlags := ctx.LocalFlagNames()
|
|
|
|
BoolFlag{Name: "debug, d", Usage: "Enable debugging"},
|
|
|
|
sort.Strings(actualFlags)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.Commands = []Command{
|
|
|
|
expect(t, actualFlags, []string{"one-flag", "two-flag"})
|
|
|
|
{
|
|
|
|
|
|
|
|
Name: "foo",
|
|
|
|
|
|
|
|
Flags: []Flag{
|
|
|
|
|
|
|
|
BoolFlag{Name: "parent, p", Usage: "Parent flag"},
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
Subcommands: []Command{
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Name: "bar",
|
|
|
|
|
|
|
|
Action: func(c *Context) error {
|
|
|
|
|
|
|
|
if c.GlobalBool("debug") {
|
|
|
|
|
|
|
|
subcommandRun = true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.GlobalBool("parent") {
|
|
|
|
|
|
|
|
parentFlag = true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
app.Run([]string{"command", "-d", "foo", "-p", "bar"})
|
|
|
|
func TestContext_FlagNames(t *testing.T) {
|
|
|
|
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
|
|
|
|
set.Bool("one-flag", false, "doc")
|
|
|
|
|
|
|
|
set.String("two-flag", "hello world", "doc")
|
|
|
|
|
|
|
|
parentSet := flag.NewFlagSet("test", 0)
|
|
|
|
|
|
|
|
parentSet.Bool("top-flag", true, "doc")
|
|
|
|
|
|
|
|
parentCtx := NewContext(nil, parentSet, nil)
|
|
|
|
|
|
|
|
ctx := NewContext(nil, set, parentCtx)
|
|
|
|
|
|
|
|
set.Parse([]string{"--one-flag", "--two-flag=foo"})
|
|
|
|
|
|
|
|
parentSet.Parse([]string{"--top-flag"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
actualFlags := ctx.FlagNames()
|
|
|
|
|
|
|
|
sort.Strings(actualFlags)
|
|
|
|
|
|
|
|
|
|
|
|
expect(t, subcommandRun, true)
|
|
|
|
expect(t, actualFlags, []string{"one-flag", "top-flag", "two-flag"})
|
|
|
|
expect(t, parentFlag, true)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestContext_Set(t *testing.T) {
|
|
|
|
func TestContext_Lineage(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Int("int", 5, "an int")
|
|
|
|
set.Bool("local-flag", false, "doc")
|
|
|
|
c := NewContext(nil, set, nil)
|
|
|
|
parentSet := flag.NewFlagSet("test", 0)
|
|
|
|
|
|
|
|
parentSet.Bool("top-flag", true, "doc")
|
|
|
|
|
|
|
|
parentCtx := NewContext(nil, parentSet, nil)
|
|
|
|
|
|
|
|
ctx := NewContext(nil, set, parentCtx)
|
|
|
|
|
|
|
|
set.Parse([]string{"--local-flag"})
|
|
|
|
|
|
|
|
parentSet.Parse([]string{"--top-flag"})
|
|
|
|
|
|
|
|
|
|
|
|
c.Set("int", "1")
|
|
|
|
lineage := ctx.Lineage()
|
|
|
|
expect(t, c.Int("int"), 1)
|
|
|
|
expect(t, len(lineage), 2)
|
|
|
|
|
|
|
|
expect(t, lineage[0], ctx)
|
|
|
|
|
|
|
|
expect(t, lineage[1], parentCtx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestContext_GlobalSet(t *testing.T) {
|
|
|
|
func TestContext_lookupFlagSet(t *testing.T) {
|
|
|
|
gSet := flag.NewFlagSet("test", 0)
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
gSet.Int("int", 5, "an int")
|
|
|
|
set.Bool("local-flag", false, "doc")
|
|
|
|
|
|
|
|
parentSet := flag.NewFlagSet("test", 0)
|
|
|
|
set := flag.NewFlagSet("sub", 0)
|
|
|
|
parentSet.Bool("top-flag", true, "doc")
|
|
|
|
set.Int("int", 3, "an int")
|
|
|
|
parentCtx := NewContext(nil, parentSet, nil)
|
|
|
|
|
|
|
|
ctx := NewContext(nil, set, parentCtx)
|
|
|
|
|
|
|
|
set.Parse([]string{"--local-flag"})
|
|
|
|
|
|
|
|
parentSet.Parse([]string{"--top-flag"})
|
|
|
|
|
|
|
|
|
|
|
|
pc := NewContext(nil, gSet, nil)
|
|
|
|
fs := lookupFlagSet("top-flag", ctx)
|
|
|
|
c := NewContext(nil, set, pc)
|
|
|
|
expect(t, fs, parentCtx.flagSet)
|
|
|
|
|
|
|
|
|
|
|
|
c.Set("int", "1")
|
|
|
|
fs = lookupFlagSet("local-flag", ctx)
|
|
|
|
expect(t, c.Int("int"), 1)
|
|
|
|
expect(t, fs, ctx.flagSet)
|
|
|
|
expect(t, c.GlobalInt("int"), 5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
c.GlobalSet("int", "1")
|
|
|
|
if fs := lookupFlagSet("frob", ctx); fs != nil {
|
|
|
|
expect(t, c.Int("int"), 1)
|
|
|
|
t.Fail()
|
|
|
|
expect(t, c.GlobalInt("int"), 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|