Skip to content

Commit fb3a9ce

Browse files
committed
FIx: Allow ext flags to be opt-out by default rather than opt-in
1 parent ae8d932 commit fb3a9ce

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

app.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ type App struct {
113113
UseShortOptionHandling bool
114114
// Enable suggestions for commands and flags
115115
Suggest bool
116+
// Allows global flags set by libraries which use flag.XXXVar(...) directly
117+
// to be parsed through this library
118+
AllowExtFlags bool
116119

117120
didSetup bool
118121

@@ -199,13 +202,15 @@ func (a *App) Setup() {
199202
a.ErrWriter = os.Stderr
200203
}
201204

202-
// add global flags added by other packages
203-
flag.VisitAll(func(f *flag.Flag) {
204-
// skip test flags
205-
if !strings.HasPrefix(f.Name, ignoreFlagPrefix) {
206-
a.Flags = append(a.Flags, &extFlag{f})
207-
}
208-
})
205+
if a.AllowExtFlags {
206+
// add global flags added by other packages
207+
flag.VisitAll(func(f *flag.Flag) {
208+
// skip test flags
209+
if !strings.HasPrefix(f.Name, ignoreFlagPrefix) {
210+
a.Flags = append(a.Flags, &extFlag{f})
211+
}
212+
})
213+
}
209214

210215
var newCommands []*Command
211216

app_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@ func TestApp_FlagsFromExtPackage(t *testing.T) {
654654
}()
655655

656656
a := &App{
657+
AllowExtFlags: true,
657658
Flags: []Flag{
658659
&StringFlag{
659660
Name: "carly",
@@ -677,6 +678,28 @@ func TestApp_FlagsFromExtPackage(t *testing.T) {
677678
if someint != 10 {
678679
t.Errorf("Expected 10 got %d for someint", someint)
679680
}
681+
682+
a = &App{
683+
Flags: []Flag{
684+
&StringFlag{
685+
Name: "carly",
686+
Aliases: []string{"c"},
687+
Required: false,
688+
},
689+
&BoolFlag{
690+
Name: "jimbob",
691+
Aliases: []string{"j"},
692+
Required: false,
693+
Value: true,
694+
},
695+
},
696+
}
697+
698+
// this should return an error since epflag shouldnt be registered
699+
err = a.Run([]string{"foo", "-c", "cly", "--epflag", "10"})
700+
if err == nil {
701+
t.Error("Expected error")
702+
}
680703
}
681704

682705
func TestApp_Setup_defaultsReader(t *testing.T) {

godoc-current.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ type App struct {
324324
UseShortOptionHandling bool
325325
// Enable suggestions for commands and flags
326326
Suggest bool
327+
// Allows global flags set by libraries which use flag.XXXVar(...) directly
328+
// to be parsed through this library
329+
AllowExtFlags bool
327330

328331
// Has unexported fields.
329332
}

testdata/godoc-v2.x.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ type App struct {
324324
UseShortOptionHandling bool
325325
// Enable suggestions for commands and flags
326326
Suggest bool
327+
// Allows global flags set by libraries which use flag.XXXVar(...) directly
328+
// to be parsed through this library
329+
AllowExtFlags bool
327330

328331
// Has unexported fields.
329332
}

0 commit comments

Comments
 (0)