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.23.2
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.3
Choose a head ref
  • 8 commits
  • 9 files changed
  • 6 contributors

Commits on Oct 31, 2022

  1. Feature:(issue_1550):Add support Int64Slice by read toml config file

    Feature:(issue_1550):add test flag test function
    Edelweiss-Snow committed Oct 31, 2022

    Verified

    This commit was signed with the committer’s verified signature.
    kzaher Krunoslav Zaher
    Copy the full SHA
    47d325e View commit details
  2. make v2approve

    万韬 committed Oct 31, 2022
    Copy the full SHA
    76769ba View commit details
  3. make v2approve

    万韬 committed Oct 31, 2022
    Copy the full SHA
    b11badc View commit details
  4. Copy the full SHA
    6f52cd5 View commit details

Commits on Nov 4, 2022

  1. Copy the full SHA
    bc62fff View commit details
  2. Copy the full SHA
    a0343df View commit details
  3. Merge pull request #1565 from remiposo/avoid_duplication_of_help_comm…

    …ands
    
    Fix: Avoid duplication of help commands
    dearchap authored Nov 4, 2022
    Copy the full SHA
    e194a18 View commit details
  4. Merge pull request #1551 from Edelweiss-Snow/issue_1550

    Feature:(issue_1550):Add support Int64Slice by read toml config file
    dearchap authored Nov 4, 2022
    Copy the full SHA
    190e5b6 View commit details
Showing with 135 additions and 1 deletion.
  1. +31 −0 altsrc/flag.go
  2. +23 −0 altsrc/flag_test.go
  3. +1 −0 altsrc/input_source_context.go
  4. +23 −0 altsrc/json_source_context.go
  5. +29 −0 altsrc/map_input_source.go
  6. +11 −0 altsrc/map_input_source_test.go
  7. +8 −0 godoc-current.txt
  8. +1 −1 help.go
  9. +8 −0 testdata/godoc-v2.x.txt
31 changes: 31 additions & 0 deletions altsrc/flag.go
Original file line number Diff line number Diff line change
@@ -148,6 +148,37 @@ func (f *IntSliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceC
return nil
}

// ApplyInputSourceValue applies a Int64Slice value if required
func (f *Int64SliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
return nil
}
for _, name := range f.Int64SliceFlag.Names() {
if !isc.isSet(name) {
continue
}
value, err := isc.Int64Slice(name)
if err != nil {
return err
}
if value == nil {
continue
}
var sliceValue = *(cli.NewInt64Slice(value...))
for _, n := range f.Names() {
underlyingFlag := f.set.Lookup(n)
if underlyingFlag == nil {
continue
}
underlyingFlag.Value = &sliceValue
}
if f.Destination != nil {
f.Destination.Set(sliceValue.Serialize())
}
}
return nil
}

// ApplyInputSourceValue applies a Bool value to the flagSet if required
func (f *BoolFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
23 changes: 23 additions & 0 deletions altsrc/flag_test.go
Original file line number Diff line number Diff line change
@@ -257,6 +257,29 @@ func TestIntSliceApplyInputSourceMethodEnvVarSet(t *testing.T) {
refute(t, c.IntSlice("test"), []int{3, 4})
}

func TestInt64SliceFlagApplyInputSourceValue(t *testing.T) {
dest := cli.NewInt64Slice()
tis := testApplyInputSource{
Flag: NewInt64SliceFlag(&cli.Int64SliceFlag{Name: "test", Destination: dest}),
FlagName: "test",
MapValue: []interface{}{int64(1), int64(2)},
}
c := runTest(t, tis)
expect(t, c.Int64Slice("test"), []int64{1, 2})
expect(t, dest.Value(), []int64{1, 2})

// reset dest
dest = cli.NewInt64Slice()
tis = testApplyInputSource{
Flag: NewInt64SliceFlag(&cli.Int64SliceFlag{Name: "test", Destination: dest}),
FlagName: "test",
MapValue: []interface{}{int64(1), int64(2)},
}
c = runRacyTest(t, tis)
refute(t, c.IntSlice("test"), []int64{1, 2})
refute(t, dest.Value(), []int64{1, 2})
}

func TestBoolApplyInputSourceMethodSet(t *testing.T) {
tis := testApplyInputSource{
Flag: NewBoolFlag(&cli.BoolFlag{Name: "test"}),
1 change: 1 addition & 0 deletions altsrc/input_source_context.go
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ type InputSourceContext interface {
String(name string) (string, error)
StringSlice(name string) ([]string, error)
IntSlice(name string) ([]int, error)
Int64Slice(name string) ([]int64, error)
Generic(name string) (cli.Generic, error)
Bool(name string) (bool, error)

23 changes: 23 additions & 0 deletions altsrc/json_source_context.go
Original file line number Diff line number Diff line change
@@ -160,6 +160,29 @@ func (x *jsonSource) IntSlice(name string) ([]int, error) {
}
}

func (x *jsonSource) Int64Slice(name string) ([]int64, error) {
i, err := x.getValue(name)
if err != nil {
return nil, err
}
switch v := i.(type) {
default:
return nil, fmt.Errorf("unexpected type %T for %q", i, name)
case []int64:
return v, nil
case []interface{}:
c := []int64{}
for _, s := range v {
if i2, ok := s.(int64); ok {
c = append(c, i2)
} else {
return c, fmt.Errorf("unexpected item type %T in %T for %q", s, c, name)
}
}
return c, nil
}
}

func (x *jsonSource) Generic(name string) (cli.Generic, error) {
i, err := x.getValue(name)
if err != nil {
29 changes: 29 additions & 0 deletions altsrc/map_input_source.go
Original file line number Diff line number Diff line change
@@ -207,6 +207,35 @@ func (fsm *MapInputSource) IntSlice(name string) ([]int, error) {
return intSlice, nil
}

// Int64Slice returns an []int64 from the map if it exists otherwise returns nil
func (fsm *MapInputSource) Int64Slice(name string) ([]int64, error) {
otherGenericValue, exists := fsm.valueMap[name]
if !exists {
otherGenericValue, exists = nestedVal(name, fsm.valueMap)
if !exists {
return nil, nil
}
}

otherValue, isType := otherGenericValue.([]interface{})
if !isType {
return nil, incorrectTypeForFlagError(name, "[]interface{}", otherGenericValue)
}

var int64Slice = make([]int64, 0, len(otherValue))
for i, v := range otherValue {
int64Value, isType := v.(int64)

if !isType {
return nil, incorrectTypeForFlagError(fmt.Sprintf("%s[%d]", name, i), "int", v)
}

int64Slice = append(int64Slice, int64Value)
}

return int64Slice, nil
}

// Generic returns an cli.Generic from the map if it exists otherwise returns nil
func (fsm *MapInputSource) Generic(name string) (cli.Generic, error) {
otherGenericValue, exists := fsm.valueMap[name]
11 changes: 11 additions & 0 deletions altsrc/map_input_source_test.go
Original file line number Diff line number Diff line change
@@ -22,3 +22,14 @@ func TestMapDuration(t *testing.T) {
_, err = inputSource.Duration("duration_of_int_type")
refute(t, nil, err)
}

func TestMapInputSource_Int64Slice(t *testing.T) {
inputSource := NewMapInputSource(
"test",
map[interface{}]interface{}{
"test_num": []interface{}{int64(1), int64(2), int64(3)},
})
d, err := inputSource.Int64Slice("test_num")
expect(t, []int64{1, 2, 3}, d)
expect(t, nil, err)
}
8 changes: 8 additions & 0 deletions godoc-current.txt
Original file line number Diff line number Diff line change
@@ -2437,6 +2437,7 @@ type InputSourceContext interface {
String(name string) (string, error)
StringSlice(name string) ([]string, error)
IntSlice(name string) ([]int, error)
Int64Slice(name string) ([]int64, error)
Generic(name string) (cli.Generic, error)
Bool(name string) (bool, error)

@@ -2494,6 +2495,9 @@ func (f *Int64SliceFlag) Apply(set *flag.FlagSet) error
Apply saves the flagSet for later usage calls, then calls the wrapped
Int64SliceFlag.Apply

func (f *Int64SliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error
ApplyInputSourceValue applies a Int64Slice value if required

type IntFlag struct {
*cli.IntFlag
// Has unexported fields.
@@ -2554,6 +2558,10 @@ func (fsm *MapInputSource) Generic(name string) (cli.Generic, error)
func (fsm *MapInputSource) Int(name string) (int, error)
Int returns an int from the map if it exists otherwise returns 0

func (fsm *MapInputSource) Int64Slice(name string) ([]int64, error)
Int64Slice returns an []int64 from the map if it exists otherwise returns
nil

func (fsm *MapInputSource) IntSlice(name string) ([]int, error)
IntSlice returns an []int from the map if it exists otherwise returns nil

2 changes: 1 addition & 1 deletion help.go
Original file line number Diff line number Diff line change
@@ -246,7 +246,7 @@ func ShowCommandHelp(ctx *Context, command string) error {
}
for _, c := range commands {
if c.HasName(command) {
if !ctx.App.HideHelpCommand && !c.HasName(helpName) && len(c.Subcommands) != 0 {
if !ctx.App.HideHelpCommand && !c.HasName(helpName) && len(c.Subcommands) != 0 && c.Command(helpName) == nil {
c.Subcommands = append(c.Subcommands, helpCommandDontUse)
}
if !ctx.App.HideHelp && HelpFlag != nil {
8 changes: 8 additions & 0 deletions testdata/godoc-v2.x.txt
Original file line number Diff line number Diff line change
@@ -2437,6 +2437,7 @@ type InputSourceContext interface {
String(name string) (string, error)
StringSlice(name string) ([]string, error)
IntSlice(name string) ([]int, error)
Int64Slice(name string) ([]int64, error)
Generic(name string) (cli.Generic, error)
Bool(name string) (bool, error)

@@ -2494,6 +2495,9 @@ func (f *Int64SliceFlag) Apply(set *flag.FlagSet) error
Apply saves the flagSet for later usage calls, then calls the wrapped
Int64SliceFlag.Apply

func (f *Int64SliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error
ApplyInputSourceValue applies a Int64Slice value if required

type IntFlag struct {
*cli.IntFlag
// Has unexported fields.
@@ -2554,6 +2558,10 @@ func (fsm *MapInputSource) Generic(name string) (cli.Generic, error)
func (fsm *MapInputSource) Int(name string) (int, error)
Int returns an int from the map if it exists otherwise returns 0

func (fsm *MapInputSource) Int64Slice(name string) ([]int64, error)
Int64Slice returns an []int64 from the map if it exists otherwise returns
nil

func (fsm *MapInputSource) IntSlice(name string) ([]int, error)
IntSlice returns an []int from the map if it exists otherwise returns nil