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.11.5
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.12.0
Choose a head ref
  • 2 commits
  • 6 files changed
  • 1 contributor

Commits on Aug 15, 2022

  1. Verified

    This commit was signed with the committer’s verified signature.
    kzaher Krunoslav Zaher
    Copy the full SHA
    b80ff3d View commit details
  2. Changes from code review

    dearchap committed Aug 15, 2022
    Copy the full SHA
    af3b50c View commit details
Showing with 72 additions and 75 deletions.
  1. +2 −53 flag.go
  2. +13 −1 flag_float64_slice.go
  3. +12 −1 flag_int64_slice.go
  4. +12 −1 flag_int_slice.go
  5. +15 −1 flag_string_slice.go
  6. +18 −18 flag_test.go
55 changes: 2 additions & 53 deletions flag.go
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@ import (
"io/ioutil"
"regexp"
"runtime"
"strconv"
"strings"
"syscall"
"time"
@@ -318,53 +317,6 @@ func stringifyFlag(f Flag) string {
fmt.Sprintf("%s\t%s", prefixedNames(df.Names(), placeholder), usageWithDefault))
}

func stringifyIntSliceFlag(f *IntSliceFlag) string {
var defaultVals []string
if f.Value != nil && len(f.Value.Value()) > 0 {
for _, i := range f.Value.Value() {
defaultVals = append(defaultVals, strconv.Itoa(i))
}
}

return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
}

func stringifyInt64SliceFlag(f *Int64SliceFlag) string {
var defaultVals []string
if f.Value != nil && len(f.Value.Value()) > 0 {
for _, i := range f.Value.Value() {
defaultVals = append(defaultVals, strconv.FormatInt(i, 10))
}
}

return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
}

func stringifyFloat64SliceFlag(f *Float64SliceFlag) string {
var defaultVals []string

if f.Value != nil && len(f.Value.Value()) > 0 {
for _, i := range f.Value.Value() {
defaultVals = append(defaultVals, strings.TrimRight(strings.TrimRight(fmt.Sprintf("%f", i), "0"), "."))
}
}

return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
}

func stringifyStringSliceFlag(f *StringSliceFlag) string {
var defaultVals []string
if f.Value != nil && len(f.Value.Value()) > 0 {
for _, s := range f.Value.Value() {
if len(s) > 0 {
defaultVals = append(defaultVals, strconv.Quote(s))
}
}
}

return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
}

func stringifySliceFlag(usage string, names, defaultVals []string) string {
placeholder, usage := unquoteUsage(usage)
if placeholder == "" {
@@ -377,11 +329,8 @@ func stringifySliceFlag(usage string, names, defaultVals []string) string {
}

usageWithDefault := strings.TrimSpace(fmt.Sprintf("%s%s", usage, defaultVal))
multiInputString := "(accepts multiple inputs)"
if usageWithDefault != "" {
multiInputString = "\t" + multiInputString
}
return fmt.Sprintf("%s\t%s%s", prefixedNames(names, placeholder), usageWithDefault, multiInputString)
pn := prefixedNames(names, placeholder)
return fmt.Sprintf("%s [ %s ]\t%s", pn, pn, usageWithDefault)
}

func hasFlag(flags []Flag, fl Flag) bool {
14 changes: 13 additions & 1 deletion flag_float64_slice.go
Original file line number Diff line number Diff line change
@@ -83,7 +83,7 @@ func (f *Float64Slice) Get() interface{} {
// String returns a readable representation of this value
// (for usage defaults)
func (f *Float64SliceFlag) String() string {
return withEnvHint(f.GetEnvVars(), stringifyFloat64SliceFlag(f))
return withEnvHint(f.GetEnvVars(), f.stringify())
}

// TakesValue returns true if the flag takes a value, otherwise false
@@ -169,6 +169,18 @@ func (f *Float64SliceFlag) Get(ctx *Context) []float64 {
return ctx.Float64Slice(f.Name)
}

func (f *Float64SliceFlag) stringify() string {
var defaultVals []string

if f.Value != nil && len(f.Value.Value()) > 0 {
for _, i := range f.Value.Value() {
defaultVals = append(defaultVals, strings.TrimRight(strings.TrimRight(fmt.Sprintf("%f", i), "0"), "."))
}
}

return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
}

// Float64Slice looks up the value of a local Float64SliceFlag, returns
// nil if not found
func (cCtx *Context) Float64Slice(name string) []float64 {
13 changes: 12 additions & 1 deletion flag_int64_slice.go
Original file line number Diff line number Diff line change
@@ -84,7 +84,7 @@ func (i *Int64Slice) Get() interface{} {
// String returns a readable representation of this value
// (for usage defaults)
func (f *Int64SliceFlag) String() string {
return withEnvHint(f.GetEnvVars(), stringifyInt64SliceFlag(f))
return withEnvHint(f.GetEnvVars(), f.stringify())
}

// TakesValue returns true of the flag takes a value, otherwise false
@@ -168,6 +168,17 @@ func (f *Int64SliceFlag) Get(ctx *Context) []int64 {
return ctx.Int64Slice(f.Name)
}

func (f *Int64SliceFlag) stringify() string {
var defaultVals []string
if f.Value != nil && len(f.Value.Value()) > 0 {
for _, i := range f.Value.Value() {
defaultVals = append(defaultVals, strconv.FormatInt(i, 10))
}
}

return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
}

// Int64Slice looks up the value of a local Int64SliceFlag, returns
// nil if not found
func (cCtx *Context) Int64Slice(name string) []int64 {
13 changes: 12 additions & 1 deletion flag_int_slice.go
Original file line number Diff line number Diff line change
@@ -95,7 +95,7 @@ func (i *IntSlice) Get() interface{} {
// String returns a readable representation of this value
// (for usage defaults)
func (f *IntSliceFlag) String() string {
return withEnvHint(f.GetEnvVars(), stringifyIntSliceFlag(f))
return withEnvHint(f.GetEnvVars(), f.stringify())
}

// TakesValue returns true of the flag takes a value, otherwise false
@@ -179,6 +179,17 @@ func (f *IntSliceFlag) Get(ctx *Context) []int {
return ctx.IntSlice(f.Name)
}

func (f *IntSliceFlag) stringify() string {
var defaultVals []string
if f.Value != nil && len(f.Value.Value()) > 0 {
for _, i := range f.Value.Value() {
defaultVals = append(defaultVals, strconv.Itoa(i))
}
}

return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
}

// IntSlice looks up the value of a local IntSliceFlag, returns
// nil if not found
func (cCtx *Context) IntSlice(name string) []int {
16 changes: 15 additions & 1 deletion flag_string_slice.go
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import (
"encoding/json"
"flag"
"fmt"
"strconv"
"strings"
)

@@ -73,7 +74,7 @@ func (s *StringSlice) Get() interface{} {
// String returns a readable representation of this value
// (for usage defaults)
func (f *StringSliceFlag) String() string {
return withEnvHint(f.GetEnvVars(), stringifyStringSliceFlag(f))
return withEnvHint(f.GetEnvVars(), f.stringify())
}

// TakesValue returns true of the flag takes a value, otherwise false
@@ -157,6 +158,19 @@ func (f *StringSliceFlag) Get(ctx *Context) []string {
return ctx.StringSlice(f.Name)
}

func (f *StringSliceFlag) stringify() string {
var defaultVals []string
if f.Value != nil && len(f.Value.Value()) > 0 {
for _, s := range f.Value.Value() {
if len(s) > 0 {
defaultVals = append(defaultVals, strconv.Quote(s))
}
}
}

return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
}

// StringSlice looks up the value of a local StringSliceFlag, returns
// nil if not found
func (cCtx *Context) StringSlice(name string) []string {
36 changes: 18 additions & 18 deletions flag_test.go
Original file line number Diff line number Diff line change
@@ -558,11 +558,11 @@ var stringSliceFlagTests = []struct {
value *StringSlice
expected string
}{
{"foo", nil, NewStringSlice(""), "--foo value\t(accepts multiple inputs)"},
{"f", nil, NewStringSlice(""), "-f value\t(accepts multiple inputs)"},
{"f", nil, NewStringSlice("Lipstick"), "-f value\t(default: \"Lipstick\")\t(accepts multiple inputs)"},
{"test", nil, NewStringSlice("Something"), "--test value\t(default: \"Something\")\t(accepts multiple inputs)"},
{"dee", []string{"d"}, NewStringSlice("Inka", "Dinka", "dooo"), "--dee value, -d value\t(default: \"Inka\", \"Dinka\", \"dooo\")\t(accepts multiple inputs)"},
{"foo", nil, NewStringSlice(""), "--foo value [ --foo value ]\t"},
{"f", nil, NewStringSlice(""), "-f value [ -f value ]\t"},
{"f", nil, NewStringSlice("Lipstick"), "-f value [ -f value ]\t(default: \"Lipstick\")"},
{"test", nil, NewStringSlice("Something"), "--test value [ --test value ]\t(default: \"Something\")"},
{"dee", []string{"d"}, NewStringSlice("Inka", "Dinka", "dooo"), "--dee value, -d value [ --dee value, -d value ]\t(default: \"Inka\", \"Dinka\", \"dooo\")"},
}

func TestStringSliceFlagHelpOutput(t *testing.T) {
@@ -911,9 +911,9 @@ var intSliceFlagTests = []struct {
value *IntSlice
expected string
}{
{"heads", nil, NewIntSlice(), "--heads value\t(accepts multiple inputs)"},
{"H", nil, NewIntSlice(), "-H value\t(accepts multiple inputs)"},
{"H", []string{"heads"}, NewIntSlice(9, 3), "-H value, --heads value\t(default: 9, 3)\t(accepts multiple inputs)"},
{"heads", nil, NewIntSlice(), "--heads value [ --heads value ]\t"},
{"H", nil, NewIntSlice(), "-H value [ -H value ]\t"},
{"H", []string{"heads"}, NewIntSlice(9, 3), "-H value, --heads value [ -H value, --heads value ]\t(default: 9, 3)"},
}

func TestIntSliceFlagHelpOutput(t *testing.T) {
@@ -1008,10 +1008,10 @@ var int64SliceFlagTests = []struct {
value *Int64Slice
expected string
}{
{"heads", nil, NewInt64Slice(), "--heads value\t(accepts multiple inputs)"},
{"H", nil, NewInt64Slice(), "-H value\t(accepts multiple inputs)"},
{"heads", nil, NewInt64Slice(), "--heads value [ --heads value ]\t"},
{"H", nil, NewInt64Slice(), "-H value [ -H value ]\t"},
{"heads", []string{"H"}, NewInt64Slice(int64(2), int64(17179869184)),
"--heads value, -H value\t(default: 2, 17179869184)\t(accepts multiple inputs)"},
"--heads value, -H value [ --heads value, -H value ]\t(default: 2, 17179869184)"},
}

func TestInt64SliceFlagHelpOutput(t *testing.T) {
@@ -1169,10 +1169,10 @@ var float64SliceFlagTests = []struct {
value *Float64Slice
expected string
}{
{"heads", nil, NewFloat64Slice(), "--heads value\t(accepts multiple inputs)"},
{"H", nil, NewFloat64Slice(), "-H value\t(accepts multiple inputs)"},
{"heads", nil, NewFloat64Slice(), "--heads value [ --heads value ]\t"},
{"H", nil, NewFloat64Slice(), "-H value [ -H value ]\t"},
{"heads", []string{"H"}, NewFloat64Slice(0.1234, -10.5),
"--heads value, -H value\t(default: 0.1234, -10.5)\t(accepts multiple inputs)"},
"--heads value, -H value [ --heads value, -H value ]\t(default: 0.1234, -10.5)"},
}

func TestFloat64SliceFlagHelpOutput(t *testing.T) {
@@ -2398,25 +2398,25 @@ func TestFlagDefaultValue(t *testing.T) {
name: "stringSclice",
flag: &StringSliceFlag{Name: "flag", Value: NewStringSlice("default1", "default2")},
toParse: []string{"--flag", "parsed"},
expect: `--flag value (default: "default1", "default2") (accepts multiple inputs)`,
expect: `--flag value [ --flag value ] (default: "default1", "default2")`,
},
{
name: "float64Sclice",
flag: &Float64SliceFlag{Name: "flag", Value: NewFloat64Slice(1.1, 2.2)},
toParse: []string{"--flag", "13.3"},
expect: `--flag value (default: 1.1, 2.2) (accepts multiple inputs)`,
expect: `--flag value [ --flag value ] (default: 1.1, 2.2)`,
},
{
name: "int64Sclice",
flag: &Int64SliceFlag{Name: "flag", Value: NewInt64Slice(1, 2)},
toParse: []string{"--flag", "13"},
expect: `--flag value (default: 1, 2) (accepts multiple inputs)`,
expect: `--flag value [ --flag value ] (default: 1, 2)`,
},
{
name: "intSclice",
flag: &IntSliceFlag{Name: "flag", Value: NewIntSlice(1, 2)},
toParse: []string{"--flag", "13"},
expect: `--flag value (default: 1, 2) (accepts multiple inputs)`,
expect: `--flag value [ --flag value ] (default: 1, 2)`,
},
{
name: "string",