Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import (
"github.com/stretchr/testify/require"
)

func TestArgNotSet(t *testing.T) {
arg := &StringArg{
Name: "sa",
Value: "foo",
}

require.Equal(t, "foo", arg.Get())
}

func TestArgsFloatTypes(t *testing.T) {
cmd := buildMinimalTestCommand()
var fval float64
Expand Down
10 changes: 3 additions & 7 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,10 @@ func (cmd *Command) handleExitCoder(ctx context.Context, err error) error {
}

func (cmd *Command) argsWithDefaultCommand(oldArgs Args) Args {
if cmd.DefaultCommand != "" {
rawArgs := append([]string{cmd.DefaultCommand}, oldArgs.Slice()...)
newArgs := &stringSliceArgs{v: rawArgs}
rawArgs := append([]string{cmd.DefaultCommand}, oldArgs.Slice()...)
newArgs := &stringSliceArgs{v: rawArgs}

return newArgs
}

return oldArgs
return newArgs
}

// Root returns the Command at the root of the graph
Expand Down
5 changes: 0 additions & 5 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@ func (f FlagsByName) Len() int {
}

func (f FlagsByName) Less(i, j int) bool {
if len(f[j].Names()) == 0 {
return false
} else if len(f[i].Names()) == 0 {
return true
}
return lexicographicLess(f[i].Names()[0], f[j].Names()[0])
}

Expand Down
3 changes: 2 additions & 1 deletion flag_bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ func (b boolValue) Create(val bool, p *bool, c BoolConfig) Value {

// ToString formats the bool value
func (b boolValue) ToString(value bool) string {
return strconv.FormatBool(value)
b.destination = &value
return b.String()
}

// Below functions are to satisfy the flag.Value interface
Expand Down
7 changes: 5 additions & 2 deletions flag_duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ func (d durationValue) Create(val time.Duration, p *time.Duration, c NoConfig) V
}

func (d durationValue) ToString(val time.Duration) string {
return fmt.Sprintf("%v", val)
d = durationValue(val)
return d.String()
}

// Below functions are to satisfy the flag.Value interface
Expand All @@ -34,7 +35,9 @@ func (d *durationValue) Set(s string) error {

func (d *durationValue) Get() any { return time.Duration(*d) }

func (d *durationValue) String() string { return (*time.Duration)(d).String() }
func (d *durationValue) String() string {
return fmt.Sprintf("%v", time.Duration(*d))
}

func (cmd *Command) Duration(name string) time.Duration {
if v, ok := cmd.Value(name).(time.Duration); ok {
Expand Down
3 changes: 2 additions & 1 deletion flag_float.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func (f floatValue[T]) Create(val T, p *T, c NoConfig) Value {
}

func (f floatValue[T]) ToString(b T) string {
return strconv.FormatFloat(float64(b), 'g', -1, int(unsafe.Sizeof(T(0))*8))
f.val = &b
return f.String()
}

// Below functions are to satisfy the flag.Value interface
Expand Down
6 changes: 2 additions & 4 deletions flag_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ func (f genericValue) Create(val Value, p *Value, c NoConfig) Value {
}

func (f genericValue) ToString(b Value) string {
if b != nil {
return b.String()
}
return ""
f.val = b
return f.String()
}

// Below functions are to satisfy the flag.Value interface
Expand Down
7 changes: 2 additions & 5 deletions flag_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ func (i intValue[T]) Create(val T, p *T, c IntegerConfig) Value {
}

func (i intValue[T]) ToString(b T) string {
if i.base == 0 {
i.base = 10
}

return strconv.FormatInt(int64(b), i.base)
i.val = &b
return i.String()
}

// Below functions are to satisfy the flag.Value interface
Expand Down
18 changes: 7 additions & 11 deletions flag_slice_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ func (i *SliceBase[T, C, VC]) Set(value string) error {

// String returns a readable representation of this value (for usage defaults)
func (i *SliceBase[T, C, VC]) String() string {
v := i.Value()
var t T
if reflect.TypeOf(t).Kind() == reflect.String {
return fmt.Sprintf("%v", v)
var defaultVals []string
var v VC
for _, s := range *i.slice {
defaultVals = append(defaultVals, v.ToString(s))
}
return fmt.Sprintf("%T{%s}", v, i.ToString(v))
return strings.Join(defaultVals, ", ")
}

// Serialize allows SliceBase to fulfill Serializer
Expand All @@ -110,10 +110,6 @@ func (i *SliceBase[T, C, VC]) Get() interface{} {
}

func (i SliceBase[T, C, VC]) ToString(t []T) string {
var defaultVals []string
var v VC
for _, s := range t {
defaultVals = append(defaultVals, v.ToString(s))
}
return strings.Join(defaultVals, ", ")
i.slice = &t
return i.String()
}
10 changes: 4 additions & 6 deletions flag_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ func (s stringValue) Create(val string, p *string, c StringConfig) Value {
}

func (s stringValue) ToString(val string) string {
if val == "" {
return val
}
return fmt.Sprintf("%q", val)
s.destination = &val
return s.String()
}

// Below functions are to satisfy the flag.Value interface
Expand All @@ -49,8 +47,8 @@ func (s *stringValue) Set(val string) error {
func (s *stringValue) Get() any { return *s.destination }

func (s *stringValue) String() string {
if s.destination != nil {
return *s.destination
if s.destination != nil && *s.destination != "" {
return fmt.Sprintf("%q", *s.destination)
}
return ""
}
Expand Down
34 changes: 34 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,29 @@ func TestFlagActionFromEnv(t *testing.T) {
assert.Equal(t, x, 42)
}

func TestParseShortOptionBoolError(t *testing.T) {
cmd := buildMinimalTestCommand()
cmd.UseShortOptionHandling = true
cmd.Flags = []Flag{
&BoolFlag{Name: "debug", Aliases: []string{"d"}},
&BoolFlag{Name: "verbose", Aliases: []string{"v"}},
}

err := cmd.Run(buildTestContext(t), []string{"run", "-vd=notabool"})
assert.Error(t, err, "expected error parsing invalid bool")
}

func TestParseShortOptionIntError(t *testing.T) {
cmd := buildMinimalTestCommand()
cmd.Flags = []Flag{
&IntFlag{Name: "port", Aliases: []string{"p"}},
&BoolFlag{Name: "debug", Aliases: []string{"d"}},
}

err := cmd.Run(buildTestContext(t), []string{"run", "-dp=notanint"})
assert.Error(t, err, "expected error parsing invalid int")
}

func TestParseMultiString(t *testing.T) {
_ = (&Command{
Flags: []Flag{
Expand Down Expand Up @@ -3294,6 +3317,17 @@ func TestFileHint(t *testing.T) {
assert.Equal(t, "bar [/tmp/foo.txt]", withFileHint("/tmp/foo.txt", "bar"))
}

func TestHasFlags(t *testing.T) {
flagToCheck := &StringFlag{Name: "foo"}
flags := []Flag{
&StringFlag{Name: "bar"},
&Int64Flag{Name: "baz"},
flagToCheck,
}

assert.True(t, hasFlag(flags, flagToCheck))
}

func TestFlagsByName(t *testing.T) {
flags := []Flag{
&StringFlag{
Expand Down
5 changes: 3 additions & 2 deletions flag_timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ func (t timestampValue) ToString(b time.Time) string {
if b.IsZero() {
return ""
}
return fmt.Sprintf("%v", b)
t.timestamp = &b
return t.String()
}

// Below functions are to satisfy the Value interface
Expand Down Expand Up @@ -122,7 +123,7 @@ func (t *timestampValue) Set(value string) error {

// String returns a readable representation of this value (for usage defaults)
func (t *timestampValue) String() string {
return fmt.Sprintf("%#v", t.timestamp)
return fmt.Sprintf("%v", t.timestamp)
}

// Get returns the flag structure
Expand Down
8 changes: 2 additions & 6 deletions flag_uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,8 @@ func (i uintValue[T]) Create(val T, p *T, c IntegerConfig) Value {
}

func (i uintValue[T]) ToString(b T) string {
base := i.base
if base == 0 {
base = 10
}

return strconv.FormatUint(uint64(b), base)
i.val = &b
return i.String()
}

// Below functions are to satisfy the flag.Value interface
Expand Down