GenericFlag.ValueFromContext() as convenient accessor
This commit is contained in:
parent
2f92fc644c
commit
8bd5fb2390
@ -104,6 +104,11 @@ func (f GenericFlag) Apply(set *flag.FlagSet) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValueFromContext returns the flag’s value in the given Context.
|
||||
func (f *GenericFlag) ValueFromContext(ctx *Context) interface{} {
|
||||
return ctx.Generic(f.Name)
|
||||
}
|
||||
|
||||
// Generic looks up the value of a local GenericFlag, returns
|
||||
// nil if not found
|
||||
func (c *Context) Generic(name string) interface{} {
|
||||
|
@ -1173,6 +1173,14 @@ func TestGenericFlagApply_SetsAllNames(t *testing.T) {
|
||||
expect(t, err, nil)
|
||||
}
|
||||
|
||||
func TestGenericFlagValueFromContext(t *testing.T) {
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
set.Var(&Parser{"abc", "def"}, "myflag", "doc")
|
||||
ctx := NewContext(nil, set, nil)
|
||||
f := &GenericFlag{Name: "myflag"}
|
||||
expect(t, f.ValueFromContext(ctx), &Parser{"abc", "def"})
|
||||
}
|
||||
|
||||
func TestParseMultiString(t *testing.T) {
|
||||
_ = (&App{
|
||||
Flags: []Flag{
|
||||
|
Loading…
Reference in New Issue
Block a user