diff --git a/internal/cmd/flag.go b/internal/cmd/flag.go index 42626d9f1..61e44d132 100644 --- a/internal/cmd/flag.go +++ b/internal/cmd/flag.go @@ -5,6 +5,8 @@ import ( "fmt" "strconv" "strings" + + "github.com/AdguardTeam/golibs/stringutil" ) // uint32Value is an uint32 that can be defined as a flag for [flag.FlagSet]. @@ -65,8 +67,9 @@ type intSliceValue struct { // values is the pointer to a slice of integers to store parsed values. values *[]int - // wiped indicates if the default value has been overwritten. - wiped bool + // isSet is false until the corresponding flag is met for the first time. + // When the flag is found, the default value is overwritten with zero value. + isSet bool } // newIntSliceValue returns a pointer to intSliceValue with the given value. @@ -75,7 +78,7 @@ func newIntSliceValue(val []int, p *[]int) (out *intSliceValue) { return &intSliceValue{ values: p, - wiped: false, + isSet: false, } } @@ -93,8 +96,8 @@ func (i *intSliceValue) Set(s string) (err error) { return fmt.Errorf("parsing integer slice arg %q: %w", s, err) } - if !i.wiped { - i.wiped = true + if !i.isSet { + i.isSet = true *i.values = []int{} } @@ -106,15 +109,16 @@ func (i *intSliceValue) Set(s string) (err error) { // String implements the [flag.Value] interface for *intSliceValue. func (i *intSliceValue) String() (out string) { - for _, v := range *i.values { - if len(out) > 0 { - out += "," + sb := &strings.Builder{} + for idx, v := range *i.values { + if idx > 0 { + stringutil.WriteToBuilder(sb, ",") } - out += strconv.Itoa(v) + stringutil.WriteToBuilder(sb, strconv.Itoa(v)) } - return out + return sb.String() } // stringSliceValue represent a struct with a slice of strings that can be @@ -123,8 +127,9 @@ type stringSliceValue struct { // values is the pointer to a slice of string to store parsed values. values *[]string - // wiped indicates if the default value has been overwritten. - wiped bool + // isSet is false until the corresponding flag is met for the first time. + // When the flag is found, the default value is overwritten with zero value. + isSet bool } // newStringSliceValue returns a pointer to stringSliceValue with the given @@ -134,7 +139,7 @@ func newStringSliceValue(val []string, p *[]string) (out *stringSliceValue) { return &stringSliceValue{ values: p, - wiped: false, + isSet: false, } } @@ -143,8 +148,8 @@ var _ flag.Value = (*stringSliceValue)(nil) // Set implements the [flag.Value] interface for *stringSliceValue. func (i *stringSliceValue) Set(s string) (err error) { - if !i.wiped { - i.wiped = true + if !i.isSet { + i.isSet = true *i.values = []string{} } @@ -155,5 +160,14 @@ func (i *stringSliceValue) Set(s string) (err error) { // String implements the [flag.Value] interface for *stringSliceValue. func (i *stringSliceValue) String() (out string) { - return fmt.Sprintf("%s", *i.values) + sb := &strings.Builder{} + for idx, v := range *i.values { + if idx > 0 { + stringutil.WriteToBuilder(sb, ",") + } + + stringutil.WriteToBuilder(sb, v) + } + + return sb.String() }