Repository: urfave/cli
Pull request #1561 (2022-11-01 21:44:44)

FIx: Allow ext flags to be opt-out by default rather than opt-in
<!--
  This template provides some ideas of things to include in your PR description.
  To start, try providing a short summary of your changes in the Title above.
  If a section of the PR template does not apply to this PR, then delete that section.
 -->

## What type of PR is this?

_(REQUIRED)_

<!--
  Delete any of the following that do not apply:
 -->

- bug
- feature

## What this PR does / why we need it:

_(REQUIRED)_

<!--
  What goal is this change working towards?
  Provide a bullet pointed summary of how each file was changed.
  Briefly explain any decisions you made with respect to the changes.
  Include anything here that you didn't include in *Release Notes*
  above, such as changes to CI or changes to internal methods.
-->

## Which issue(s) this PR fixes:

_(REQUIRED)_

<!--
If this PR fixes one of more issues, list them here.
One line each, like so:

Fixes #123
Fixes #39
-->

## Special notes for your reviewer:

_(fill-in or delete this section)_

<!--
   Is there any particular feedback you would / wouldn't like?
   Which parts of the code should reviewers focus on?
-->

## Testing

make test
_(fill-in or delete this section)_

<!--
  Describe how you tested this change.
-->

## Release Notes

_(REQUIRED)_
<!--
  If this PR makes user facing changes, please describe them here. This
  description will be copied into the release notes/changelog, whenever the
  next version is released. Keep this section short, and focus on high level
  changes.

  Put your text between the block. To omit notes, use NONE within the block.
-->

```release-note

```



Patch:
diff --git a/app.go b/app.go
index 3fb5c16..effe205 100644
--- a/app.go
+++ b/app.go
@@ -113,6 +113,9 @@ type App struct {
 	UseShortOptionHandling bool
 	// Enable suggestions for commands and flags
 	Suggest bool
+	// Allows global flags set by libraries which use flag.XXXVar(...) directly
+	// to be parsed through this library
+	AllowExtFlags bool
 
 	didSetup bool
 
@@ -199,13 +202,15 @@ func (a *App) Setup() {
 		a.ErrWriter = os.Stderr
 	}
 
-	// add global flags added by other packages
-	flag.VisitAll(func(f *flag.Flag) {
-		// skip test flags
-		if !strings.HasPrefix(f.Name, ignoreFlagPrefix) {
-			a.Flags = append(a.Flags, &extFlag{f})
-		}
-	})
+	if a.AllowExtFlags {
+		// add global flags added by other packages
+		flag.VisitAll(func(f *flag.Flag) {
+			// skip test flags
+			if !strings.HasPrefix(f.Name, ignoreFlagPrefix) {
+				a.Flags = append(a.Flags, &extFlag{f})
+			}
+		})
+	}
 
 	var newCommands []*Command
 
diff --git a/godoc-current.txt b/godoc-current.txt
index fadb7f8..5b67f95 100644
--- a/godoc-current.txt
+++ b/godoc-current.txt
@@ -324,6 +324,9 @@ type App struct {
 	UseShortOptionHandling bool
 	// Enable suggestions for commands and flags
 	Suggest bool
+	// Allows global flags set by libraries which use flag.XXXVar(...) directly
+	// to be parsed through this library
+	AllowExtFlags bool
 
 	// Has unexported fields.
 }


Test patch:
diff --git a/app_test.go b/app_test.go
index d24fb63..e2b31e9 100644
--- a/app_test.go
+++ b/app_test.go
@@ -654,6 +654,7 @@ func TestApp_FlagsFromExtPackage(t *testing.T) {
 	}()
 
 	a := &App{
+		AllowExtFlags: true,
 		Flags: []Flag{
 			&StringFlag{
 				Name:     "carly",
@@ -677,6 +678,28 @@ func TestApp_FlagsFromExtPackage(t *testing.T) {
 	if someint != 10 {
 		t.Errorf("Expected 10 got %d for someint", someint)
 	}
+
+	a = &App{
+		Flags: []Flag{
+			&StringFlag{
+				Name:     "carly",
+				Aliases:  []string{"c"},
+				Required: false,
+			},
+			&BoolFlag{
+				Name:     "jimbob",
+				Aliases:  []string{"j"},
+				Required: false,
+				Value:    true,
+			},
+		},
+	}
+
+	// this should return an error since epflag shouldnt be registered
+	err = a.Run([]string{"foo", "-c", "cly", "--epflag", "10"})
+	if err == nil {
+		t.Error("Expected error")
+	}
 }
 
 func TestApp_Setup_defaultsReader(t *testing.T) {
diff --git a/testdata/godoc-v2.x.txt b/testdata/godoc-v2.x.txt
index fadb7f8..5b67f95 100644
--- a/testdata/godoc-v2.x.txt
+++ b/testdata/godoc-v2.x.txt
@@ -324,6 +324,9 @@ type App struct {
 	UseShortOptionHandling bool
 	// Enable suggestions for commands and flags
 	Suggest bool
+	// Allows global flags set by libraries which use flag.XXXVar(...) directly
+	// to be parsed through this library
+	AllowExtFlags bool
 
 	// Has unexported fields.
 }