File tree Expand file tree Collapse file tree 2 files changed +21
-3
lines changed
Expand file tree Collapse file tree 2 files changed +21
-3
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package cli
33import (
44 "context"
55 "flag"
6+ "fmt"
67 "strings"
78)
89
@@ -46,10 +47,11 @@ func (cCtx *Context) NumFlags() int {
4647
4748// Set sets a context flag to a value.
4849func (cCtx * Context ) Set (name , value string ) error {
49- if cCtx .flagSet . Lookup (name ) = = nil {
50- cCtx . onInvalidFlag (name )
50+ if fs := cCtx .lookupFlagSet (name ); fs ! = nil {
51+ return fs . Set (name , value )
5152 }
52- return cCtx .flagSet .Set (name , value )
53+
54+ return fmt .Errorf ("no such flag -%s" , name )
5355}
5456
5557// IsSet determines if the flag was actually set
Original file line number Diff line number Diff line change @@ -643,3 +643,19 @@ func TestCheckRequiredFlags(t *testing.T) {
643643 })
644644 }
645645}
646+
647+ func TestContext_ParentContext_Set (t * testing.T ) {
648+ parentSet := flag .NewFlagSet ("parent" , flag .ContinueOnError )
649+ parentSet .String ("Name" , "" , "" )
650+
651+ context := NewContext (
652+ nil ,
653+ flag .NewFlagSet ("child" , flag .ContinueOnError ),
654+ NewContext (nil , parentSet , nil ),
655+ )
656+
657+ err := context .Set ("Name" , "aaa" )
658+ if err != nil {
659+ t .Errorf ("expect nil. set parent context flag return err: %s" , err )
660+ }
661+ }
You can’t perform that action at this time.
0 commit comments