1
0
Fork 0
mirror of https://github.com/Luzifer/rconfig.git synced 2024-12-20 19:21:19 +00:00

Fix: Add support for int16 flags

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2024-08-27 15:16:03 +02:00
parent 7572b1fd7b
commit 6f0f872a04
Signed by: luzifer
SSH key fingerprint: SHA256:/xtE5lCgiRDQr8SLxHMS92ZBlACmATUmF1crK16Ks4E
2 changed files with 12 additions and 1 deletions

View file

@ -258,7 +258,7 @@ func execTags(in interface{}, fs *pflag.FlagSet) ([]afterFunc, error) {
valField.SetBool(v) valField.SetBool(v)
} }
case reflect.Int, reflect.Int8, reflect.Int32, reflect.Int64: case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
vt, err := strconv.ParseInt(value, 10, 64) vt, err := strconv.ParseInt(value, 10, 64)
if err != nil { if err != nil {
if value != "" { if value != "" {
@ -375,6 +375,12 @@ func registerFlagInt(t reflect.Kind, fs *pflag.FlagSet, field interface{}, parts
} else { } else {
fs.Int8VarP(field.(*int8), parts[0], parts[1], int8(vt), desc) fs.Int8VarP(field.(*int8), parts[0], parts[1], int8(vt), desc)
} }
case reflect.Int16:
if len(parts) == 1 {
fs.Int16Var(field.(*int16), parts[0], int16(vt), desc)
} else {
fs.Int16VarP(field.(*int16), parts[0], parts[1], int16(vt), desc)
}
case reflect.Int32: case reflect.Int32:
if len(parts) == 1 { if len(parts) == 1 {
fs.Int32Var(field.(*int32), parts[0], int32(vt), desc) fs.Int32Var(field.(*int32), parts[0], int32(vt), desc)

View file

@ -9,6 +9,7 @@ func TestIntParsing(t *testing.T) {
args = []string{ args = []string{
"--int=1", "-i", "2", "--int=1", "-i", "2",
"--int8=3", "-8", "4", "--int8=3", "-8", "4",
"--int16=2", "-1", "3",
"--int32=5", "-3", "6", "--int32=5", "-3", "6",
"--int64=7", "-6", "8", "--int64=7", "-6", "8",
} }
@ -17,6 +18,8 @@ func TestIntParsing(t *testing.T) {
TestP int `flag:"intp,i"` TestP int `flag:"intp,i"`
Test8 int8 `flag:"int8"` Test8 int8 `flag:"int8"`
Test8P int8 `flag:"int8p,8"` Test8P int8 `flag:"int8p,8"`
Test16 int16 `flag:"int16"`
Test16P int16 `flag:"int16p,1"`
Test32 int32 `flag:"int32"` Test32 int32 `flag:"int32"`
Test32P int32 `flag:"int32p,3"` Test32P int32 `flag:"int32p,3"`
Test64 int64 `flag:"int64"` Test64 int64 `flag:"int64"`
@ -34,6 +37,8 @@ func TestIntParsing(t *testing.T) {
{cfg.TestP, 2}, {cfg.TestP, 2},
{cfg.Test8, int8(3)}, {cfg.Test8, int8(3)},
{cfg.Test8P, int8(4)}, {cfg.Test8P, int8(4)},
{cfg.Test16, int16(2)},
{cfg.Test16P, int16(3)},
{cfg.Test32, int32(5)}, {cfg.Test32, int32(5)},
{cfg.Test32P, int32(6)}, {cfg.Test32P, int32(6)},
{cfg.Test64, int64(7)}, {cfg.Test64, int64(7)},