-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
123 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
"time" | ||
|
||
"github.com/caarlos0/duration" | ||
) | ||
|
||
func newFlagParseError(err error) flagParseError { | ||
var reason, flag string | ||
s := err.Error() | ||
switch { | ||
case strings.HasPrefix(s, "flag needs an argument:"): | ||
reason = "Flag %s needs an argument." | ||
ps := strings.Split(s, "-") | ||
switch len(ps) { | ||
case 2: //nolint:gomnd | ||
flag = "-" + ps[len(ps)-1] | ||
case 3: //nolint:gomnd | ||
flag = "--" + ps[len(ps)-1] | ||
} | ||
case strings.HasPrefix(s, "unknown flag:"): | ||
reason = "Flag %s is missing." | ||
flag = strings.TrimPrefix(s, "unknown flag: ") | ||
case strings.HasPrefix(s, "invalid argument"): | ||
reason = "Flag %s have an invalid argument." | ||
re := regexp.MustCompile(`invalid argument ".*" for "(.*)" flag: .*`) | ||
parts := re.FindStringSubmatch(s) | ||
if len(parts) > 1 { | ||
flag = parts[1] | ||
} | ||
default: | ||
reason = s | ||
} | ||
return flagParseError{ | ||
err: err, | ||
reason: reason, | ||
flag: flag, | ||
} | ||
} | ||
|
||
type flagParseError struct { | ||
err error | ||
reason string | ||
flag string | ||
} | ||
|
||
func (f flagParseError) Error() string { | ||
return f.err.Error() | ||
} | ||
|
||
func (f flagParseError) ReasonFormat() string { | ||
return f.reason | ||
} | ||
|
||
func (f flagParseError) Flag() string { | ||
return f.flag | ||
} | ||
|
||
func newDurationFlag(val time.Duration, p *time.Duration) *durationFlag { | ||
*p = val | ||
return (*durationFlag)(p) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var flagParseErrorTests = []struct { | ||
in string | ||
flag string | ||
reason string | ||
}{ | ||
{ | ||
"unknown flag: --nope", | ||
"--nope", | ||
"Flag %s is missing.", | ||
}, | ||
{ | ||
"flag needs an argument: --delete", | ||
"--delete", | ||
"Flag %s needs an argument.", | ||
}, | ||
{ | ||
"flag needs an argument: 'd' in -d", | ||
"-d", | ||
"Flag %s needs an argument.", | ||
}, | ||
{ | ||
`invalid argument "20dd" for "--delete-older-than" flag: time: unknown unit "dd" in duration "20dd"`, | ||
"--delete-older-than", | ||
"Flag %s have an invalid argument.", | ||
}, | ||
{ | ||
`invalid argument "sdfjasdl" for "--max-tokens" flag: strconv.ParseInt: parsing "sdfjasdl": invalid syntax`, | ||
"--max-tokens", | ||
"Flag %s have an invalid argument.", | ||
}, | ||
{ | ||
`invalid argument "nope" for "-r, --raw" flag: strconv.ParseBool: parsing "nope": invalid syntax`, | ||
"-r, --raw", | ||
"Flag %s have an invalid argument.", | ||
}, | ||
} | ||
|
||
func TestFlagParseError(t *testing.T) { | ||
for _, tf := range flagParseErrorTests { | ||
t.Run(tf.in, func(t *testing.T) { | ||
err := newFlagParseError(fmt.Errorf(tf.in)) | ||
require.Equal(t, tf.flag, err.Flag()) | ||
require.Equal(t, tf.reason, err.ReasonFormat()) | ||
require.Equal(t, tf.in, err.Error()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters