8000 Merge branch 'main' into issue_1550 · urfave/cli@a0343df · GitHub
[go: up one dir, main page]

Skip to content

Commit a0343df

Browse files
Merge branch 'main' into issue_1550
2 parents 6f52cd5 + d0aeb4d commit a0343df

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

app.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ 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
119+
// Treat all flags as normal arguments if true
120+
SkipFlagParsing bool
116121

117122
didSetup bool
118123

@@ -199,13 +204,15 @@ func (a *App) Setup() {
199204
a.ErrWriter = os.Stderr
200205
}
201206

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-
})
207+
if a.AllowExtFlags {
208+
// add global flags added by other packages
209+
flag.VisitAll(func(f *flag.Flag) {
210+
// skip test flags
211+
if !strings.HasPrefix(f.Name, ignoreFlagPrefix) {
212+
a.Flags = append(a.Flags, &extFlag{f})
213+
}
214+
})
215+
}
209216

210217
var newCommands []*Command
211218

@@ -280,6 +287,7 @@ func (a *App) newRootCommand() *Command {
280287
HelpName: a.HelpName,
281288
CustomHelpTemplate: a.CustomAppHelpTemplate,
282289
categories: a.categories,
290+
SkipFlagParsing: a.SkipFlagParsing,
283291
isRoot: true,
284292
}
285293
}

app_test.go

Lines changed: 41 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) {
@@ -790,6 +813,24 @@ func TestApp_CommandWithNoFlagBeforeTerminator(t *testing.T) {
790813
expect(t, args.Get(2), "notAFlagAtAll")
791814
}
792815

816+
func TestApp_SkipFlagParsing(t *testing.T) {
817+
var args Args
818+
819+
app := &App{
820+
SkipFlagParsing: true,
821+
Action: func(c *Context) error {
822+
args = c.Args()
823+
return nil
824+
},
825+
}
826+
827+
_ = app.Run([]string{"", "--", "my-arg", "notAFlagAtAll"})
828+
829+
expect(t, args.Get(0), "--")
830+
expect(t, args.Get(1), "my-arg")
831+
expect(t, args.Get(2), "notAFlagAtAll")
832+
}
833+
793834
func TestApp_VisibleCommands(t *testing.T) {
794835
app := &App{
795836
Commands: []*Command{

godoc-current.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@ 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
330+
// Treat all flags as normal arguments if true
331+
SkipFlagParsing bool
327332

328333
// Has unexported fields.
329334
}

testdata/godoc-v2.x.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@ 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
330+
// Treat all flags as normal arguments if true
331+
SkipFlagParsing bool
327332

328333
// Has unexported fields.
329334
}

0 commit comments

Comments
 (0)
0