From 6f0f872a042d530a607dbfc861e97a49e11f20a9 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Tue, 27 Aug 2024 15:16:03 +0200 Subject: [PATCH] Fix: Add support for int16 flags Signed-off-by: Knut Ahlers --- config.go | 8 +++++++- int_test.go | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/config.go b/config.go index 662bad7..c3af259 100644 --- a/config.go +++ b/config.go @@ -258,7 +258,7 @@ func execTags(in interface{}, fs *pflag.FlagSet) ([]afterFunc, error) { 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) if err != nil { if value != "" { @@ -375,6 +375,12 @@ func registerFlagInt(t reflect.Kind, fs *pflag.FlagSet, field interface{}, parts } else { 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: if len(parts) == 1 { fs.Int32Var(field.(*int32), parts[0], int32(vt), desc) diff --git a/int_test.go b/int_test.go index 7fe2bc4..f44a8dc 100644 --- a/int_test.go +++ b/int_test.go @@ -9,6 +9,7 @@ func TestIntParsing(t *testing.T) { args = []string{ "--int=1", "-i", "2", "--int8=3", "-8", "4", + "--int16=2", "-1", "3", "--int32=5", "-3", "6", "--int64=7", "-6", "8", } @@ -17,6 +18,8 @@ func TestIntParsing(t *testing.T) { TestP int `flag:"intp,i"` Test8 int8 `flag:"int8"` Test8P int8 `flag:"int8p,8"` + Test16 int16 `flag:"int16"` + Test16P int16 `flag:"int16p,1"` Test32 int32 `flag:"int32"` Test32P int32 `flag:"int32p,3"` Test64 int64 `flag:"int64"` @@ -34,6 +37,8 @@ func TestIntParsing(t *testing.T) { {cfg.TestP, 2}, {cfg.Test8, int8(3)}, {cfg.Test8P, int8(4)}, + {cfg.Test16, int16(2)}, + {cfg.Test16P, int16(3)}, {cfg.Test32, int32(5)}, {cfg.Test32P, int32(6)}, {cfg.Test64, int64(7)},