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

10
app.go
View File

@@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"sort"
"strings"
"time"
)
@@ -20,6 +21,7 @@ var (
errInvalidActionType = NewExitError("ERROR invalid Action type. "+
fmt.Sprintf("Must be `func(*Context`)` or `func(*Context) error). %s", contactSysadmin)+
fmt.Sprintf("See %s", appActionDeprecationURL), 2)
ignoreFlagPrefix = "test." // this is to ignore test flags when adding flags from other packages
SuggestFlag SuggestFlagFunc = suggestFlag
SuggestCommand SuggestCommandFunc = suggestCommand
@@ -197,6 +199,14 @@ 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})
}
})
var newCommands []*Command
for _, c := range a.Commands {