Merge pull request #297 from ston1th/master

Added destination scan support for flags
This commit is contained in:
Jesse Szwedko 2015-11-15 12:34:56 -08:00
commit 93b4ad6d1f
3 changed files with 168 additions and 22 deletions

View File

@ -158,6 +158,32 @@ app.Action = func(c *cli.Context) {
... ...
``` ```
You can also set a destination variable for a flag, to which the content will be scanned.
``` go
...
var language string
app.Flags = []cli.Flag {
cli.StringFlag{
Name: "lang",
Value: "english",
Usage: "language for the greeting",
Destination: &language,
},
}
app.Action = func(c *cli.Context) {
name := "someone"
if len(c.Args()) > 0 {
name = c.Args()[0]
}
if language == "spanish" {
println("Hola", name)
} else {
println("Hello", name)
}
}
...
```
See full list of flags at http://godoc.org/github.com/codegangsta/cli See full list of flags at http://godoc.org/github.com/codegangsta/cli
#### Alternate Names #### Alternate Names

30
flag.go
View File

@ -240,6 +240,7 @@ type BoolFlag struct {
Name string Name string
Usage string Usage string
EnvVar string EnvVar string
Destination *bool
} }
// String returns a readable representation of this value (for usage defaults) // String returns a readable representation of this value (for usage defaults)
@ -264,6 +265,10 @@ func (f BoolFlag) Apply(set *flag.FlagSet) {
} }
eachName(f.Name, func(name string) { eachName(f.Name, func(name string) {
if f.Destination != nil {
set.BoolVar(f.Destination, name, val, f.Usage)
return
}
set.Bool(name, val, f.Usage) set.Bool(name, val, f.Usage)
}) })
} }
@ -278,6 +283,7 @@ type BoolTFlag struct {
Name string Name string
Usage string Usage string
EnvVar string EnvVar string
Destination *bool
} }
// String returns a readable representation of this value (for usage defaults) // String returns a readable representation of this value (for usage defaults)
@ -302,6 +308,10 @@ func (f BoolTFlag) Apply(set *flag.FlagSet) {
} }
eachName(f.Name, func(name string) { eachName(f.Name, func(name string) {
if f.Destination != nil {
set.BoolVar(f.Destination, name, val, f.Usage)
return
}
set.Bool(name, val, f.Usage) set.Bool(name, val, f.Usage)
}) })
} }
@ -316,6 +326,7 @@ type StringFlag struct {
Value string Value string
Usage string Usage string
EnvVar string EnvVar string
Destination *string
} }
// String returns the usage // String returns the usage
@ -345,6 +356,10 @@ func (f StringFlag) Apply(set *flag.FlagSet) {
} }
eachName(f.Name, func(name string) { eachName(f.Name, func(name string) {
if f.Destination != nil {
set.StringVar(f.Destination, name, f.Value, f.Usage)
return
}
set.String(name, f.Value, f.Usage) set.String(name, f.Value, f.Usage)
}) })
} }
@ -360,6 +375,7 @@ type IntFlag struct {
Value int Value int
Usage string Usage string
EnvVar string EnvVar string
Destination *int
} }
// String returns the usage // String returns the usage
@ -383,6 +399,10 @@ func (f IntFlag) Apply(set *flag.FlagSet) {
} }
eachName(f.Name, func(name string) { eachName(f.Name, func(name string) {
if f.Destination != nil {
set.IntVar(f.Destination, name, f.Value, f.Usage)
return
}
set.Int(name, f.Value, f.Usage) set.Int(name, f.Value, f.Usage)
}) })
} }
@ -398,6 +418,7 @@ type DurationFlag struct {
Value time.Duration Value time.Duration
Usage string Usage string
EnvVar string EnvVar string
Destination *time.Duration
} }
// String returns a readable representation of this value (for usage defaults) // String returns a readable representation of this value (for usage defaults)
@ -421,6 +442,10 @@ func (f DurationFlag) Apply(set *flag.FlagSet) {
} }
eachName(f.Name, func(name string) { eachName(f.Name, func(name string) {
if f.Destination != nil {
set.DurationVar(f.Destination, name, f.Value, f.Usage)
return
}
set.Duration(name, f.Value, f.Usage) set.Duration(name, f.Value, f.Usage)
}) })
} }
@ -436,6 +461,7 @@ type Float64Flag struct {
Value float64 Value float64
Usage string Usage string
EnvVar string EnvVar string
Destination *float64
} }
// String returns the usage // String returns the usage
@ -458,6 +484,10 @@ func (f Float64Flag) Apply(set *flag.FlagSet) {
} }
eachName(f.Name, func(name string) { eachName(f.Name, func(name string) {
if f.Destination != nil {
set.Float64Var(f.Destination, name, f.Value, f.Usage)
return
}
set.Float64(name, f.Value, f.Usage) set.Float64(name, f.Value, f.Usage)
}) })
} }

View File

@ -305,6 +305,24 @@ func TestParseMultiString(t *testing.T) {
}).Run([]string{"run", "-s", "10"}) }).Run([]string{"run", "-s", "10"})
} }
func TestParseDestinationString(t *testing.T) {
var dest string
a := App{
Flags: []Flag{
StringFlag{
Name: "dest",
Destination: &dest,
},
},
Action: func(ctx *Context) {
if dest != "10" {
t.Errorf("expected destination String 10")
}
},
}
a.Run([]string{"run", "--dest", "10"})
}
func TestParseMultiStringFromEnv(t *testing.T) { func TestParseMultiStringFromEnv(t *testing.T) {
os.Clearenv() os.Clearenv()
os.Setenv("APP_COUNT", "20") os.Setenv("APP_COUNT", "20")
@ -412,6 +430,24 @@ func TestParseMultiInt(t *testing.T) {
a.Run([]string{"run", "-s", "10"}) a.Run([]string{"run", "-s", "10"})
} }
func TestParseDestinationInt(t *testing.T) {
var dest int
a := App{
Flags: []Flag{
IntFlag{
Name: "dest",
Destination: &dest,
},
},
Action: func(ctx *Context) {
if dest != 10 {
t.Errorf("expected destination Int 10")
}
},
}
a.Run([]string{"run", "--dest", "10"})
}
func TestParseMultiIntFromEnv(t *testing.T) { func TestParseMultiIntFromEnv(t *testing.T) {
os.Clearenv() os.Clearenv()
os.Setenv("APP_TIMEOUT_SECONDS", "10") os.Setenv("APP_TIMEOUT_SECONDS", "10")
@ -521,6 +557,24 @@ func TestParseMultiFloat64(t *testing.T) {
a.Run([]string{"run", "-s", "10.2"}) a.Run([]string{"run", "-s", "10.2"})
} }
func TestParseDestinationFloat64(t *testing.T) {
var dest float64
a := App{
Flags: []Flag{
Float64Flag{
Name: "dest",
Destination: &dest,
},
},
Action: func(ctx *Context) {
if dest != 10.2 {
t.Errorf("expected destination Float64 10.2")
}
},
}
a.Run([]string{"run", "--dest", "10.2"})
}
func TestParseMultiFloat64FromEnv(t *testing.T) { func TestParseMultiFloat64FromEnv(t *testing.T) {
os.Clearenv() os.Clearenv()
os.Setenv("APP_TIMEOUT_SECONDS", "15.5") os.Setenv("APP_TIMEOUT_SECONDS", "15.5")
@ -576,6 +630,24 @@ func TestParseMultiBool(t *testing.T) {
a.Run([]string{"run", "--serve"}) a.Run([]string{"run", "--serve"})
} }
func TestParseDestinationBool(t *testing.T) {
var dest bool
a := App{
Flags: []Flag{
BoolFlag{
Name: "dest",
Destination: &dest,
},
},
Action: func(ctx *Context) {
if dest != true {
t.Errorf("expected destination Bool true")
}
},
}
a.Run([]string{"run", "--dest"})
}
func TestParseMultiBoolFromEnv(t *testing.T) { func TestParseMultiBoolFromEnv(t *testing.T) {
os.Clearenv() os.Clearenv()
os.Setenv("APP_DEBUG", "1") os.Setenv("APP_DEBUG", "1")
@ -631,6 +703,24 @@ func TestParseMultiBoolT(t *testing.T) {
a.Run([]string{"run", "--serve"}) a.Run([]string{"run", "--serve"})
} }
func TestParseDestinationBoolT(t *testing.T) {
var dest bool
a := App{
Flags: []Flag{
BoolTFlag{
Name: "dest",
Destination: &dest,
},
},
Action: func(ctx *Context) {
if dest != true {
t.Errorf("expected destination BoolT true")
}
},
}
a.Run([]string{"run", "--dest"})
}
func TestParseMultiBoolTFromEnv(t *testing.T) { func TestParseMultiBoolTFromEnv(t *testing.T) {
os.Clearenv() os.Clearenv()
os.Setenv("APP_DEBUG", "0") os.Setenv("APP_DEBUG", "0")