diff --git a/context.go b/context.go index c542a67..93c000c 100644 --- a/context.go +++ b/context.go @@ -141,6 +141,11 @@ func (c *Context) NumFlags() int { return c.flagSet.NFlag() } +// Set sets a context flag to a value. +func (c *Context) Set(name, value string) error { + return c.flagSet.Set(name, value) +} + // Determines if the flag was actually set func (c *Context) IsSet(name string) bool { if c.setFlags == nil { diff --git a/context_test.go b/context_test.go index cbad304..20970b2 100644 --- a/context_test.go +++ b/context_test.go @@ -202,3 +202,12 @@ func TestContext_GlobalFlagsInSubcommands(t *testing.T) { expect(t, subcommandRun, true) expect(t, parentFlag, true) } + +func TestContext_Set(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Int("int", 5, "an int") + c := NewContext(nil, set, nil) + + c.Set("int", "1") + expect(t, c.Int("int"), 1) +}