Skip to content

Commit

Permalink
cmd: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Aug 30, 2024
1 parent 87aa2f5 commit 4b09664
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func addOption(flags *flag.FlagSet, fieldPtr any, o *commandLineOption) {
case *float32:
defineFlagVar(flags, newFloat32Value(*fieldPtr, fieldPtr), o)
case *[]int:
defineFlagVar(flags, newIntListValue(*fieldPtr, fieldPtr), o)
defineFlagVar(flags, newIntSliceValue(*fieldPtr, fieldPtr), o)
case *[]string:
defineFlagVar(flags, newStringsValue(*fieldPtr, fieldPtr), o)
case encoding.TextUnmarshaler:
Expand Down
22 changes: 11 additions & 11 deletions internal/cmd/flags.go → internal/cmd/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,29 @@ func (i *float32Value) String() (out string) {
return strconv.FormatFloat(float64(*i), 'f', 3, 32)
}

// intListValue is a slice of integers that can be defined as a flag for
// intSliceValue is a slice of integers that can be defined as a flag for
// [flag.FlagSet].
type intListValue []int
type intSliceValue []int

// newIntListValue returns a pointer to intListValue with the given value.
func newIntListValue(val []int, p *[]int) (out *intListValue) {
// newIntSliceValue returns a pointer to intSliceValue with the given value.
func newIntSliceValue(val []int, p *[]int) (out *intSliceValue) {
*p = val

return (*intListValue)(p)
return (*intSliceValue)(p)
}

// type check
var _ flag.Value = (*intListValue)(nil)
var _ flag.Value = (*intSliceValue)(nil)

// Set implements the [flag.Value] interface for *intListValue.
func (i *intListValue) Set(s string) (err error) {
// Set implements the [flag.Value] interface for *intSliceValue.
func (i *intSliceValue) Set(s string) (err error) {
values := strings.Split(s, ",")

var intVal int64
for _, v := range values {
intVal, err = strconv.ParseInt(v, 0, 32)
if err != nil {
return fmt.Errorf("parsing integer list arg %q: %w", s, err)
return fmt.Errorf("parsing integer slice arg %q: %w", s, err)
}

*i = append(*i, int(intVal))
Expand All @@ -90,8 +90,8 @@ func (i *intListValue) Set(s string) (err error) {
return nil
}

// String implements the [flag.Value] interface for *intListValue.
func (i *intListValue) String() (out string) {
// String implements the [flag.Value] interface for *intSliceValue.
func (i *intSliceValue) String() (out string) {
for _, v := range *i {
if len(out) > 0 {
out += ","
Expand Down

0 comments on commit 4b09664

Please sign in to comment.