Merge commit 'c3fccc0' into v3-porting

main
Dan Buch 2 years ago
commit d80f366fee
Signed by: meatballhat
GPG Key ID: A12F782281063434

@ -113,6 +113,9 @@ type App struct {
UseShortOptionHandling bool UseShortOptionHandling bool
// Enable suggestions for commands and flags // Enable suggestions for commands and flags
Suggest bool Suggest bool
// Allows global flags set by libraries which use flag.XXXVar(...) directly
// to be parsed through this library
AllowExtFlags bool
didSetup bool didSetup bool
@ -199,13 +202,15 @@ func (a *App) Setup() {
a.ErrWriter = os.Stderr a.ErrWriter = os.Stderr
} }
// add global flags added by other packages if a.AllowExtFlags {
flag.VisitAll(func(f *flag.Flag) { // add global flags added by other packages
// skip test flags flag.VisitAll(func(f *flag.Flag) {
if !strings.HasPrefix(f.Name, ignoreFlagPrefix) { // skip test flags
a.Flags = append(a.Flags, &extFlag{f}) if !strings.HasPrefix(f.Name, ignoreFlagPrefix) {
} a.Flags = append(a.Flags, &extFlag{f})
}) }
})
}
var newCommands []*Command var newCommands []*Command

@ -654,6 +654,7 @@ func TestApp_FlagsFromExtPackage(t *testing.T) {
}() }()
a := &App{ a := &App{
AllowExtFlags: true,
Flags: []Flag{ Flags: []Flag{
&StringFlag{ &StringFlag{
Name: "carly", Name: "carly",
@ -677,6 +678,28 @@ func TestApp_FlagsFromExtPackage(t *testing.T) {
if someint != 10 { if someint != 10 {
t.Errorf("Expected 10 got %d for someint", someint) 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) { func TestApp_Setup_defaultsReader(t *testing.T) {

@ -322,6 +322,9 @@ type App struct {
UseShortOptionHandling bool UseShortOptionHandling bool
// Enable suggestions for commands and flags // Enable suggestions for commands and flags
Suggest bool Suggest bool
// Allows global flags set by libraries which use flag.XXXVar(...) directly
// to be parsed through this library
AllowExtFlags bool
// Has unexported fields. // Has unexported fields.
} }

@ -322,6 +322,9 @@ type App struct {
UseShortOptionHandling bool UseShortOptionHandling bool
// Enable suggestions for commands and flags // Enable suggestions for commands and flags
Suggest bool Suggest bool
// Allows global flags set by libraries which use flag.XXXVar(...) directly
// to be parsed through this library
AllowExtFlags bool
// Has unexported fields. // Has unexported fields.
} }

Loading…
Cancel
Save