PathFlag.ValueFromContext() as convenient accessor
This commit is contained in:
parent
6d7f859008
commit
660184dd92
@ -96,6 +96,11 @@ func (f *PathFlag) Apply(set *flag.FlagSet) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ValueFromContext returns the flag’s value in the given Context.
|
||||||
|
func (f *PathFlag) ValueFromContext(ctx *Context) string {
|
||||||
|
return ctx.Path(f.Name)
|
||||||
|
}
|
||||||
|
|
||||||
// Path looks up the value of a local PathFlag, returns
|
// Path looks up the value of a local PathFlag, returns
|
||||||
// "" if not found
|
// "" if not found
|
||||||
func (c *Context) Path(name string) string {
|
func (c *Context) Path(name string) string {
|
||||||
|
@ -501,6 +501,14 @@ func TestPathFlagApply_SetsAllNames(t *testing.T) {
|
|||||||
expect(t, v, "/path/to/file/PATH")
|
expect(t, v, "/path/to/file/PATH")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPathFlagValueFromContext(t *testing.T) {
|
||||||
|
set := flag.NewFlagSet("test", 0)
|
||||||
|
set.String("myflag", "/my/path", "doc")
|
||||||
|
ctx := NewContext(nil, set, nil)
|
||||||
|
f := &PathFlag{Name: "myflag"}
|
||||||
|
expect(t, f.ValueFromContext(ctx), "/my/path")
|
||||||
|
}
|
||||||
|
|
||||||
var envHintFlagTests = []struct {
|
var envHintFlagTests = []struct {
|
||||||
name string
|
name string
|
||||||
env string
|
env string
|
||||||
|
Loading…
Reference in New Issue
Block a user