Skip to content

Commit

Permalink
Merge branch 'main' into issue_1550
Browse files Browse the repository at this point in the history
  • Loading branch information
Edelweiss-Snow committed Nov 4, 2022
2 parents 6f52cd5 + d0aeb4d commit a0343df
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
22 changes: 15 additions & 7 deletions app.go
Expand Up @@ -113,6 +113,11 @@ 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
// Treat all flags as normal arguments if true
SkipFlagParsing bool

didSetup bool

Expand Down Expand Up @@ -199,13 +204,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

Expand Down Expand Up @@ -280,6 +287,7 @@ func (a *App) newRootCommand() *Command {
HelpName: a.HelpName,
CustomHelpTemplate: a.CustomAppHelpTemplate,
categories: a.categories,
SkipFlagParsing: a.SkipFlagParsing,
isRoot: true,
}
}
Expand Down
41 changes: 41 additions & 0 deletions app_test.go
Expand Up @@ -654,6 +654,7 @@ func TestApp_FlagsFromExtPackage(t *testing.T) {
}()

a := &App{
AllowExtFlags: true,
Flags: []Flag{
&StringFlag{
Name: "carly",
Expand All @@ -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) {
Expand Down Expand Up @@ -790,6 +813,24 @@ func TestApp_CommandWithNoFlagBeforeTerminator(t *testing.T) {
expect(t, args.Get(2), "notAFlagAtAll")
}

func TestApp_SkipFlagParsing(t *testing.T) {
var args Args

app := &App{
SkipFlagParsing: true,
Action: func(c *Context) error {
args = c.Args()
return nil
},
}

_ = app.Run([]string{"", "--", "my-arg", "notAFlagAtAll"})

expect(t, args.Get(0), "--")
expect(t, args.Get(1), "my-arg")
expect(t, args.Get(2), "notAFlagAtAll")
}

func TestApp_VisibleCommands(t *testing.T) {
app := &App{
Commands: []*Command{
Expand Down
5 changes: 5 additions & 0 deletions godoc-current.txt
Expand Up @@ -324,6 +324,11 @@ 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
// Treat all flags as normal arguments if true
SkipFlagParsing bool

// Has unexported fields.
}
Expand Down
5 changes: 5 additions & 0 deletions testdata/godoc-v2.x.txt
Expand Up @@ -324,6 +324,11 @@ 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
// Treat all flags as normal arguments if true
SkipFlagParsing bool

// Has unexported fields.
}
Expand Down

0 comments on commit a0343df

Please sign in to comment.