Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: urfave/cli
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.22.0
Choose a base ref
...
head repository: urfave/cli
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.23.0
Choose a head ref
  • 1 commit
  • 3 files changed
  • 1 contributor

Commits on Oct 28, 2022

  1. 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
    dearchap authored Oct 28, 2022

    Verified

    This commit was signed with the committer’s verified signature.
    kzaher Krunoslav Zaher
    Copy the full SHA
    ae8d932 View commit details
Showing with 94 additions and 0 deletions.
  1. +10 −0 app.go
  2. +36 −0 app_test.go
  3. +48 −0 flag_ext.go
10 changes: 10 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
@@ -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 {
36 changes: 36 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
@@ -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()
48 changes: 48 additions & 0 deletions flag_ext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cli

import "flag"

type extFlag struct {
f *flag.Flag
}

func (e *extFlag) Apply(fs *flag.FlagSet) error {
fs.Var(e.f.Value, e.f.Name, e.f.Usage)
return nil
}

func (e *extFlag) Names() []string {
return []string{e.f.Name}
}

func (e *extFlag) IsSet() bool {
return false
}

func (e *extFlag) String() string {
return FlagStringer(e)
}

func (e *extFlag) IsVisible() bool {
return true
}

func (e *extFlag) TakesValue() bool {
return false
}

func (e *extFlag) GetUsage() string {
return e.f.Usage
}

func (e *extFlag) GetValue() string {
return e.f.Value.String()
}

func (e *extFlag) GetDefaultText() string {
return e.f.DefValue
}

func (e *extFlag) GetEnvVars() []string {
return nil
}