Skip to content
Open
4 changes: 2 additions & 2 deletions bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
package pflag

import (
"bytes"
"strconv"
"strings"
"testing"
)

Expand Down Expand Up @@ -154,7 +154,7 @@ func TestImplicitFalse(t *testing.T) {
func TestInvalidValue(t *testing.T) {
var tristate triStateValue
f := setUpFlagSet(&tristate)
var buf bytes.Buffer
var buf strings.Builder
f.SetOutput(&buf)
err := f.Parse([]string{"--tristate=invalid"})
if err == nil {
Expand Down
4 changes: 2 additions & 2 deletions bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func bytesHexConv(sval string) (interface{}, error) {
return bin, nil
}

return nil, fmt.Errorf("invalid string being converted to Bytes: %s %s", sval, err)
return nil, fmt.Errorf("invalid string being converted to Bytes: %s %w", sval, err)
}

// GetBytesHex return the []byte value of a flag with the given name
Expand Down Expand Up @@ -178,7 +178,7 @@ func bytesBase64ValueConv(sval string) (interface{}, error) {
return bin, nil
}

return nil, fmt.Errorf("invalid string being converted to Bytes: %s %s", sval, err)
return nil, fmt.Errorf("invalid string being converted to Bytes: %s %w", sval, err)
}

// GetBytesBase64 return the []byte value of a flag with the given name
Expand Down
2 changes: 1 addition & 1 deletion count.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func newCountValue(val int, p *int) *countValue {
func (i *countValue) Set(s string) error {
// "+1" means that no specific value was passed, so increment
if s == "+1" {
*i = countValue(*i + 1)
*i++
return nil
}
v, err := strconv.ParseInt(s, 0, 0)
Expand Down
30 changes: 13 additions & 17 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package pflag

import (
"bytes"
"errors"
goflag "flag"
"fmt"
Expand Down Expand Up @@ -430,7 +429,7 @@ func (f *FlagSet) Set(name, value string) error {
} else {
flagName = fmt.Sprintf("--%s", flag.Name)
}
return fmt.Errorf("invalid argument %q for %q flag: %v", value, flagName, err)
return fmt.Errorf("invalid argument %q for %q flag: %w", value, flagName, err)
}

if !flag.Changed {
Expand Down Expand Up @@ -607,7 +606,7 @@ func wrapN(i, slop int, s string) (string, string) {
// caller). Pass `w` == 0 to do no wrapping
func wrap(i, w int, s string) string {
if w == 0 {
return strings.Replace(s, "\n", "\n"+strings.Repeat(" ", i), -1)
return strings.ReplaceAll(s, "\n", "\n"+strings.Repeat(" ", i))
}

// space between indent i and end of line width w into which
Expand All @@ -625,7 +624,7 @@ func wrap(i, w int, s string) string {
}
// If still not enough space then don't even try to wrap.
if wrap < 24 {
return strings.Replace(s, "\n", r, -1)
return strings.ReplaceAll(s, "\n", r)
}

// Try to avoid short orphan words on the final line, by
Expand All @@ -637,35 +636,33 @@ func wrap(i, w int, s string) string {
// Handle first line, which is indented by the caller (or the
// special case above)
l, s = wrapN(wrap, slop, s)
r = r + strings.Replace(l, "\n", "\n"+strings.Repeat(" ", i), -1)
r = r + strings.ReplaceAll(l, "\n", "\n"+strings.Repeat(" ", i))

// Now wrap the rest
for s != "" {
var t string

t, s = wrapN(wrap, slop, s)
r = r + "\n" + strings.Repeat(" ", i) + strings.Replace(t, "\n", "\n"+strings.Repeat(" ", i), -1)
r = r + "\n" + strings.Repeat(" ", i) + strings.ReplaceAll(t, "\n", "\n"+strings.Repeat(" ", i))
}

return r

}

// FlagUsagesWrapped returns a string containing the usage information
// for all flags in the FlagSet. Wrapped to `cols` columns (0 for no
// wrapping)
func (f *FlagSet) FlagUsagesWrapped(cols int) string {
buf := new(bytes.Buffer)

lines := make([]string, 0, len(f.formal))

maxlen := 0
var (
max, maxlen int
lines = make([]string, 0, len(f.formal))
)
f.VisitAll(func(flag *Flag) {
if flag.Hidden {
return
}

line := ""
var line string
if flag.Shorthand != "" && flag.ShorthandDeprecated == "" {
line = fmt.Sprintf(" -%s", flag.Shorthand)
if !flag.ShorthandOnly {
Expand Down Expand Up @@ -716,8 +713,11 @@ func (f *FlagSet) FlagUsagesWrapped(cols int) string {
}

lines = append(lines, line)
max += len(line)
})

buf := new(strings.Builder)
buf.Grow(max)
for _, line := range lines {
sidx := strings.Index(line, "\x00")
spacing := strings.Repeat(" ", maxlen-sidx)
Expand Down Expand Up @@ -1149,10 +1149,6 @@ func (f *FlagSet) parseAll(arguments []string, fn parseFunc) error {
}
f.parsed = true

if len(arguments) < 0 {
return nil
}

f.args = make([]string, 0, len(arguments))

err := f.parseArgs(arguments, fn)
Expand Down
27 changes: 12 additions & 15 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package pflag

import (
"bytes"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -673,7 +672,7 @@ func TestChangedHelper(t *testing.T) {
func replaceSeparators(name string, from []string, to string) string {
result := name
for _, sep := range from {
result = strings.Replace(result, sep, to, -1)
result = strings.ReplaceAll(result, sep, to)
}
// Type convert to indicate normalization has been done.
return result
Expand Down Expand Up @@ -899,7 +898,7 @@ func TestUserDefined(t *testing.T) {

func TestSetOutput(t *testing.T) {
var flags FlagSet
var buf bytes.Buffer
var buf strings.Builder
flags.SetOutput(&buf)
flags.Init("test", ContinueOnError)
flags.Parse([]string{"--unknown"})
Expand All @@ -910,7 +909,7 @@ func TestSetOutput(t *testing.T) {

func TestOutput(t *testing.T) {
var flags FlagSet
var buf bytes.Buffer
var buf strings.Builder
expect := "an example string"
flags.SetOutput(&buf)
fmt.Fprint(flags.Output(), expect)
Expand All @@ -927,8 +926,8 @@ func TestOutputExitOnError(t *testing.T) {
t.Fatal("this error should not be triggered")
return
}
mockStdout := bytes.NewBufferString("")
mockStderr := bytes.NewBufferString("")
mockStdout := new(strings.Builder)
mockStderr := new(strings.Builder)
cmd := exec.Command(os.Args[0], "-test.run="+t.Name())
cmd.Env = append(os.Environ(), "PFLAG_CRASH_TEST=1")
cmd.Stdout = mockStdout
Expand Down Expand Up @@ -1161,7 +1160,7 @@ func getDeprecatedFlagSet() *FlagSet {
func TestDeprecatedFlagInDocs(t *testing.T) {
f := getDeprecatedFlagSet()

out := new(bytes.Buffer)
out := new(strings.Builder)
f.SetOutput(out)
f.PrintDefaults()

Expand All @@ -1178,7 +1177,7 @@ func TestUnHiddenDeprecatedFlagInDocs(t *testing.T) {
}
flg.Hidden = false

out := new(bytes.Buffer)
out := new(strings.Builder)
f.SetOutput(out)
f.PrintDefaults()

Expand All @@ -1197,7 +1196,7 @@ func TestDeprecatedFlagShorthandInDocs(t *testing.T) {
f.BoolP(name, "n", true, "always true")
f.MarkShorthandDeprecated("noshorthandflag", fmt.Sprintf("use --%s instead", name))

out := new(bytes.Buffer)
out := new(strings.Builder)
f.SetOutput(out)
f.PrintDefaults()

Expand All @@ -1216,7 +1215,7 @@ func parseReturnStderr(t *testing.T, f *FlagSet, args []string) (string, error)
outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
go func() {
var buf bytes.Buffer
var buf strings.Builder
io.Copy(&buf, r)
outC <- buf.String()
}()
Expand Down Expand Up @@ -1300,7 +1299,7 @@ func TestHiddenFlagInUsage(t *testing.T) {
f.Bool("secretFlag", true, "shhh")
f.MarkHidden("secretFlag")

out := new(bytes.Buffer)
out := new(strings.Builder)
f.SetOutput(out)
f.PrintDefaults()

Expand Down Expand Up @@ -1365,7 +1364,7 @@ func (cv *customValue) Type() string { return "custom" }

func TestPrintDefaults(t *testing.T) {
fs := NewFlagSet("print defaults test", ContinueOnError)
var buf bytes.Buffer
var buf strings.Builder
fs.SetOutput(&buf)
fs.Bool("A", false, "for bootstrapping, allow 'any' type")
fs.Bool("Alongflagname", false, "disable bounds checking")
Expand Down Expand Up @@ -1401,8 +1400,6 @@ func TestPrintDefaults(t *testing.T) {
fs.PrintDefaults()
got := buf.String()
if got != defaultOutput {
fmt.Println("\n" + got)
fmt.Println("\n" + defaultOutput)
t.Errorf("got %q want %q\n", got, defaultOutput)
}
}
Expand Down Expand Up @@ -1449,7 +1446,7 @@ func TestVisitFlagOrder(t *testing.T) {

func TestUnquoteUsage(t *testing.T) {

var buf bytes.Buffer
var buf strings.Builder
var fs = NewFlagSet(t.Name(), ContinueOnError)
fs.SetOutput(&buf)
want := "Usage of TestUnquoteUsage:\n"
Expand Down
6 changes: 4 additions & 2 deletions ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func newIPValue(val net.IP, p *net.IP) *ipValue {

func (i *ipValue) String() string { return net.IP(*i).String() }
func (i *ipValue) Set(s string) error {
if s == "" {
return nil
}
ip := net.ParseIP(strings.TrimSpace(s))
if ip == nil {
return fmt.Errorf("failed to parse IP: %q", s)
Expand All @@ -32,8 +35,7 @@ func (i *ipValue) Type() string {
}

func ipConv(sval string) (interface{}, error) {
ip := net.ParseIP(sval)
if ip != nil {
if ip := net.ParseIP(sval); ip != nil {
return ip, nil
}
return nil, fmt.Errorf("invalid string being converted to IP address: %s", sval)
Expand Down
15 changes: 4 additions & 11 deletions ip_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,31 +75,24 @@ func (s *ipSliceValue) String() string {
return "[" + out + "]"
}

func (s *ipSliceValue) fromString(val string) (net.IP, error) {
return net.ParseIP(strings.TrimSpace(val)), nil
func (s *ipSliceValue) fromString(val string) net.IP {
return net.ParseIP(strings.TrimSpace(val))
}

func (s *ipSliceValue) toString(val net.IP) string {
return val.String()
}

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

func (s *ipSliceValue) Replace(val []string) error {
out := make([]net.IP, len(val))
for i, d := range val {
var err error
out[i], err = s.fromString(d)
if err != nil {
return err
}
out[i] = s.fromString(d)
}
*s.value = out
return nil
Expand Down
2 changes: 1 addition & 1 deletion ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestIP(t *testing.T) {
{"1.2.3.4", true, "1.2.3.4"},
{"127.0.0.1", true, "127.0.0.1"},
{"255.255.255.255", true, "255.255.255.255"},
{"", false, ""},
{"", true, "0.0.0.0"},
{"0", false, ""},
{"localhost", false, ""},
{"0.0.0", false, ""},
Expand Down
6 changes: 3 additions & 3 deletions printusage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
package pflag

import (
"bytes"
"io"
"strings"
"testing"
)

Expand All @@ -28,7 +28,7 @@ func setUpPFlagSet(buf io.Writer) *FlagSet {
}

func TestPrintUsage(t *testing.T) {
buf := bytes.Buffer{}
var buf strings.Builder
f := setUpPFlagSet(&buf)
f.PrintDefaults()
res := buf.String()
Expand Down Expand Up @@ -68,7 +68,7 @@ const expectedOutput2 = ` --long-form Some description
`

func TestPrintUsage_2(t *testing.T) {
buf := bytes.Buffer{}
var buf strings.Builder
f := setUpPFlagSet2(&buf)
res := f.FlagUsagesWrapped(80)
if res != expectedOutput2 {
Expand Down
3 changes: 1 addition & 2 deletions string_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package pflag

import (
"bytes"
"encoding/csv"
"strings"
)
Expand Down Expand Up @@ -32,7 +31,7 @@ func readAsCSV(val string) ([]string, error) {
}

func writeAsCSV(vals []string) (string, error) {
b := &bytes.Buffer{}
b := new(strings.Builder)
w := csv.NewWriter(b)
err := w.Write(vals)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions string_to_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package pflag

import (
"bytes"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -54,7 +53,7 @@ func (s *stringToIntValue) Type() string {
}

func (s *stringToIntValue) String() string {
var buf bytes.Buffer
var buf strings.Builder
i := 0
for k, v := range *s.value {
if i > 0 {
Expand Down
Loading