Feature:(issue_269) Allow external package flag definitions (#1540)

* Feature:(issue_269) Add compatibility with external package flag definitions

* Add tests

* Add defer to remove global flag

* Use ptr to receiver for extFlag

* Add const for flag prefix to ignore
This commit is contained in:
dearchap
2022-10-28 09:17:13 -04:00
committed by GitHub
parent c344b46a29
commit ae8d932413
3 changed files with 94 additions and 0 deletions

View File

@@ -643,6 +643,42 @@ func TestApp_RunDefaultCommandWithFlags(t *testing.T) {
}
}
func TestApp_FlagsFromExtPackage(t *testing.T) {
var someint int
flag.IntVar(&someint, "epflag", 2, "ext package flag usage")
// Based on source code we can reset the global flag parsing this way
defer func() {
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
}()
a := &App{
Flags: []Flag{
&StringFlag{
Name: "carly",
Aliases: []string{"c"},
Required: false,
},
&BoolFlag{
Name: "jimbob",
Aliases: []string{"j"},
Required: false,
Value: true,
},
},
}
err := a.Run([]string{"foo", "-c", "cly", "--epflag", "10"})
if err != nil {
t.Error(err)
}
if someint != 10 {
t.Errorf("Expected 10 got %d for someint", someint)
}
}
func TestApp_Setup_defaultsReader(t *testing.T) {
app := &App{}
app.Setup()