Skip to content

Commit

Permalink
cmd: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Sep 9, 2024
1 parent f8a4e2a commit 8978f06
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions internal/cmd/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down Expand Up @@ -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.
Expand All @@ -75,7 +78,7 @@ func newIntSliceValue(val []int, p *[]int) (out *intSliceValue) {

return &intSliceValue{
values: p,
wiped: false,
isSet: false,
}
}

Expand All @@ -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{}
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -134,7 +139,7 @@ func newStringSliceValue(val []string, p *[]string) (out *stringSliceValue) {

return &stringSliceValue{
values: p,
wiped: false,
isSet: false,
}
}

Expand All @@ -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{}
}

Expand All @@ -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()
}

0 comments on commit 8978f06

Please sign in to comment.