PathFlag.ValueFromContext() as convenient accessor

main
Tilo Prütz 2 years ago
parent 6d7f859008
commit 660184dd92

@ -96,6 +96,11 @@ func (f *PathFlag) Apply(set *flag.FlagSet) error {
return nil
}
// ValueFromContext returns the flags 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
// "" if not found
func (c *Context) Path(name string) string {

@ -501,6 +501,14 @@ func TestPathFlagApply_SetsAllNames(t *testing.T) {
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 {
name string
env string

Loading…
Cancel
Save