PathFlag.ValueFromContext() as convenient accessor

This commit is contained in:
Tilo Prütz 2022-04-22 16:59:19 +02:00
parent 6d7f859008
commit 660184dd92
2 changed files with 13 additions and 0 deletions

View File

@ -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 {

View File

@ -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