Skip to content

Commit

Permalink
Add support for go1.15 complex128 type (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
cornfeedhobo authored Dec 2, 2020
1 parent 6ce2386 commit 6ad51b0
Show file tree
Hide file tree
Showing 4 changed files with 511 additions and 0 deletions.
118 changes: 118 additions & 0 deletions complex128.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build go1.15

package pflag

import "strconv"

// -- complex128 Value
type complex128Value complex128

func newComplex128Value(val complex128, p *complex128) *complex128Value {
*p = val
return (*complex128Value)(p)
}

func (f *complex128Value) Set(s string) error {
v, err := strconv.ParseComplex(s, 128)
*f = complex128Value(v)
return err
}

func (f *complex128Value) Type() string {
return "complex128"
}

func (f *complex128Value) String() string { return strconv.FormatComplex(complex128(*f), 'g', -1, 128) }

func complex128Conv(sval string) (interface{}, error) {
return strconv.ParseComplex(sval, 128)
}

// GetComplex128 return the complex128 value of a flag with the given name
func (f *FlagSet) GetComplex128(name string) (complex128, error) {
val, err := f.getFlagType(name, "complex128", complex128Conv)
if err != nil {
return 0, err
}
return val.(complex128), nil
}

// MustGetComplex128 is like GetComplex128, but panics on error.
func (f *FlagSet) MustGetComplex128(name string) complex128 {
val, err := f.GetComplex128(name)
if err != nil {
panic(err)
}
return val
}

// Complex128Var defines a complex128 flag with specified name, default value, and usage string.
// The argument p points to a complex128 variable in which to store the value of the flag.
func (f *FlagSet) Complex128Var(p *complex128, name string, value complex128, usage string) {
f.Complex128VarP(p, name, "", value, usage)
}

// Complex128VarP is like Complex128Var, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Complex128VarP(p *complex128, name, shorthand string, value complex128, usage string) {
f.VarP(newComplex128Value(value, p), name, shorthand, usage)
}

// Complex128VarS is like Complex128Var, but accepts a shorthand letter that can be used after a single dash, alone.
func (f *FlagSet) Complex128VarS(p *complex128, name, shorthand string, value complex128, usage string) {
f.VarS(newComplex128Value(value, p), name, shorthand, usage)
}

// Complex128Var defines a complex128 flag with specified name, default value, and usage string.
// The argument p points to a complex128 variable in which to store the value of the flag.
func Complex128Var(p *complex128, name string, value complex128, usage string) {
CommandLine.Complex128Var(p, name, value, usage)
}

// Complex128VarP is like Complex128Var, but accepts a shorthand letter that can be used after a single dash.
func Complex128VarP(p *complex128, name, shorthand string, value complex128, usage string) {
CommandLine.Complex128VarP(p, name, shorthand, value, usage)
}

// Complex128VarS is like Complex128Var, but accepts a shorthand letter that can be used after a single dash, alone.
func Complex128VarS(p *complex128, name, shorthand string, value complex128, usage string) {
CommandLine.Complex128VarS(p, name, shorthand, value, usage)
}

// Complex128 defines a complex128 flag with specified name, default value, and usage string.
// The return value is the address of a complex128 variable that stores the value of the flag.
func (f *FlagSet) Complex128(name string, value complex128, usage string) *complex128 {
return f.Complex128P(name, "", value, usage)
}

// Complex128P is like Complex128, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Complex128P(name, shorthand string, value complex128, usage string) *complex128 {
p := new(complex128)
f.Complex128VarP(p, name, shorthand, value, usage)
return p
}

// Complex128S is like Complex128, but accepts a shorthand letter that can be used after a single dash, alone.
func (f *FlagSet) Complex128S(name, shorthand string, value complex128, usage string) *complex128 {
p := new(complex128)
f.Complex128VarS(p, name, shorthand, value, usage)
return p
}

// Complex128 defines a complex128 flag with specified name, default value, and usage string.
// The return value is the address of a complex128 variable that stores the value of the flag.
func Complex128(name string, value complex128, usage string) *complex128 {
return CommandLine.Complex128(name, value, usage)
}

// Complex128P is like Complex128, but accepts a shorthand letter that can be used after a single dash.
func Complex128P(name, shorthand string, value complex128, usage string) *complex128 {
return CommandLine.Complex128P(name, shorthand, value, usage)
}

// Complex128S is like Complex128, but accepts a shorthand letter that can be used after a single dash, alone.
func Complex128S(name, shorthand string, value complex128, usage string) *complex128 {
return CommandLine.Complex128S(name, shorthand, value, usage)
}
200 changes: 200 additions & 0 deletions complex128_slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build go1.15

package pflag

import (
"fmt"
"strconv"
"strings"
)

// -- complex128Slice Value
type complex128SliceValue struct {
value *[]complex128
changed bool
}

func newComplex128SliceValue(val []complex128, p *[]complex128) *complex128SliceValue {
isv := new(complex128SliceValue)
isv.value = p
*isv.value = val
return isv
}

func (s *complex128SliceValue) Set(val string) error {
ss := strings.Split(val, ",")
out := make([]complex128, len(ss))
for i, d := range ss {
var err error
out[i], err = strconv.ParseComplex(d, 128)
if err != nil {
return err
}

}
if !s.changed {
*s.value = out
} else {
*s.value = append(*s.value, out...)
}
s.changed = true
return nil
}

func (s *complex128SliceValue) Type() string {
return "complex128Slice"
}

func (s *complex128SliceValue) String() string {
out := make([]string, len(*s.value))
for i, d := range *s.value {
out[i] = fmt.Sprintf("%f", d)
}
return "[" + strings.Join(out, ",") + "]"
}

func (s *complex128SliceValue) fromString(val string) (complex128, error) {
return strconv.ParseComplex(val, 128)
}

func (s *complex128SliceValue) toString(val complex128) string {
return fmt.Sprintf("%f", val)
}

func (s *complex128SliceValue) Append(val string) error {
i, err := s.fromString(val)
if err != nil {
return err
}
*s.value = append(*s.value, i)
return nil
}

func (s *complex128SliceValue) Replace(val []string) error {
out := make([]complex128, len(val))
for i, d := range val {
var err error
out[i], err = s.fromString(d)
if err != nil {
return err
}
}
*s.value = out
return nil
}

func (s *complex128SliceValue) GetSlice() []string {
out := make([]string, len(*s.value))
for i, d := range *s.value {
out[i] = s.toString(d)
}
return out
}

func complex128SliceConv(val string) (interface{}, error) {
val = strings.Trim(val, "[]")
// Empty string would cause a slice with one (empty) entry
if len(val) == 0 {
return []complex128{}, nil
}
ss := strings.Split(val, ",")
out := make([]complex128, len(ss))
for i, d := range ss {
var err error
out[i], err = strconv.ParseComplex(d, 128)
if err != nil {
return nil, err
}

}
return out, nil
}

// GetComplex128Slice return the []complex128 value of a flag with the given name
func (f *FlagSet) GetComplex128Slice(name string) ([]complex128, error) {
val, err := f.getFlagType(name, "complex128Slice", complex128SliceConv)
if err != nil {
return []complex128{}, err
}
return val.([]complex128), nil
}

// MustGetComplex128Slice is like GetComplex128Slice, but panics on error.
func (f *FlagSet) MustGetComplex128Slice(name string) []complex128 {
val, err := f.GetComplex128Slice(name)
if err != nil {
panic(err)
}
return val
}

// Complex128SliceVar defines a complex128Slice flag with specified name, default value, and usage string.
// The argument p points to a []complex128 variable in which to store the value of the flag.
func (f *FlagSet) Complex128SliceVar(p *[]complex128, name string, value []complex128, usage string) {
f.Complex128SliceVarP(p, name, "", value, usage)
}

// Complex128SliceVarP is like Complex128SliceVar, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Complex128SliceVarP(p *[]complex128, name, shorthand string, value []complex128, usage string) {
f.VarP(newComplex128SliceValue(value, p), name, shorthand, usage)
}

// Complex128SliceVarS is like Complex128SliceVar, but accepts a shorthand letter that can be used after a single dash, alone.
func (f *FlagSet) Complex128SliceVarS(p *[]complex128, name, shorthand string, value []complex128, usage string) {
f.VarS(newComplex128SliceValue(value, p), name, shorthand, usage)
}

// Complex128SliceVar defines a complex128[] flag with specified name, default value, and usage string.
// The argument p points to a complex128[] variable in which to store the value of the flag.
func Complex128SliceVar(p *[]complex128, name string, value []complex128, usage string) {
CommandLine.Complex128SliceVar(p, name, value, usage)
}

// Complex128SliceVarP is like Complex128SliceVar, but accepts a shorthand letter that can be used after a single dash.
func Complex128SliceVarP(p *[]complex128, name, shorthand string, value []complex128, usage string) {
CommandLine.Complex128SliceVarP(p, name, shorthand, value, usage)
}

// Complex128SliceVarS is like Complex128SliceVar, but accepts a shorthand letter that can be used after a single dash, alone.
func Complex128SliceVarS(p *[]complex128, name, shorthand string, value []complex128, usage string) {
CommandLine.Complex128SliceVarS(p, name, shorthand, value, usage)
}

// Complex128Slice defines a []complex128 flag with specified name, default value, and usage string.
// The return value is the address of a []complex128 variable that stores the value of the flag.
func (f *FlagSet) Complex128Slice(name string, value []complex128, usage string) *[]complex128 {
return f.Complex128SliceP(name, "", value, usage)
}

// Complex128SliceP is like Complex128Slice, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Complex128SliceP(name, shorthand string, value []complex128, usage string) *[]complex128 {
p := []complex128{}
f.Complex128SliceVarP(&p, name, shorthand, value, usage)
return &p
}

// Complex128SliceS is like Complex128Slice, but accepts a shorthand letter that can be used after a single dash, alone.
func (f *FlagSet) Complex128SliceS(name, shorthand string, value []complex128, usage string) *[]complex128 {
p := []complex128{}
f.Complex128SliceVarS(&p, name, shorthand, value, usage)
return &p
}

// Complex128Slice defines a []complex128 flag with specified name, default value, and usage string.
// The return value is the address of a []complex128 variable that stores the value of the flag.
func Complex128Slice(name string, value []complex128, usage string) *[]complex128 {
return CommandLine.Complex128Slice(name, value, usage)
}

// Complex128SliceP is like Complex128Slice, but accepts a shorthand letter that can be used after a single dash.
func Complex128SliceP(name, shorthand string, value []complex128, usage string) *[]complex128 {
return CommandLine.Complex128SliceP(name, shorthand, value, usage)
}

// Complex128SliceS is like Complex128Slice, but accepts a shorthand letter that can be used after a single dash, alone.
func Complex128SliceS(name, shorthand string, value []complex128, usage string) *[]complex128 {
return CommandLine.Complex128SliceS(name, shorthand, value, usage)
}
Loading

0 comments on commit 6ad51b0

Please sign in to comment.