diff --git a/CHANGELOG.md b/CHANGELOG.md index b2a95f36..c8302ca0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ ## Changelog +### [13.15.0](https://kaos.sh/ek/13.15.1) + +- `[options]` Added method `Argument.Uint64` +- `[options]` Return type of method `Argument.Uint` set from `uint64` to `uint` + ### [13.15.0](https://kaos.sh/ek/13.15.0) - `[color]` Added `fmt.GoStringer` support for structs diff --git a/options/arguments.go b/options/arguments.go index e0a34f73..626a2cea 100644 --- a/options/arguments.go +++ b/options/arguments.go @@ -9,9 +9,11 @@ package options import ( "fmt" + "math" "strconv" "strings" + "github.com/essentialkaos/ek/v13/mathutil" "github.com/essentialkaos/ek/v13/path" ) @@ -164,9 +166,12 @@ func (a Argument) Is(value any) bool { case int64: v, err := a.Int64() return v == t && err == nil - case uint64: + case uint: v, err := a.Uint() return v == t && err == nil + case uint64: + v, err := a.Uint64() + return v == t && err == nil case float64: v, err := a.Float() return v == t && err == nil @@ -189,7 +194,13 @@ func (a Argument) Int64() (int64, error) { } // Uint converts argument to uint -func (a Argument) Uint() (uint64, error) { +func (a Argument) Uint() (uint, error) { + u, err := strconv.ParseUint(string(a), 10, 64) + return uint(mathutil.Min(u, math.MaxUint)), err +} + +// Uint converts argument to uint64 +func (a Argument) Uint64() (uint64, error) { return strconv.ParseUint(string(a), 10, 64) } diff --git a/options/options_test.go b/options/options_test.go index 1d9ae661..a36d600b 100644 --- a/options/options_test.go +++ b/options/options_test.go @@ -628,7 +628,7 @@ func (s *OptUtilSuite) TestArgumentsConversion(c *C) { c.Assert(a.Get(1).Is(12), Equals, false) c.Assert(a.Get(1).Is(int64(6)), Equals, true) c.Assert(a.Get(1).Is(int64(12)), Equals, false) - c.Assert(a.Get(1).Is(uint64(6)), Equals, true) + c.Assert(a.Get(1).Is(uint(6)), Equals, true) c.Assert(a.Get(1).Is(uint64(12)), Equals, false) c.Assert(a.Get(2).Is(2.67), Equals, true) c.Assert(a.Get(2).Is(3.14), Equals, false) @@ -656,10 +656,18 @@ func (s *OptUtilSuite) TestArgumentsConversion(c *C) { vu, err := a.Get(0).Uint() c.Assert(err, NotNil) c.Assert(err, ErrorMatches, `strconv.ParseUint: parsing "test": invalid syntax`) - c.Assert(vu, Equals, uint64(0)) + c.Assert(vu, Equals, uint(0)) vu, err = a.Get(1).Uint() c.Assert(err, IsNil) - c.Assert(vu, Equals, uint64(6)) + c.Assert(vu, Equals, uint(6)) + + vu64, err := a.Get(0).Uint64() + c.Assert(err, NotNil) + c.Assert(err, ErrorMatches, `strconv.ParseUint: parsing "test": invalid syntax`) + c.Assert(vu64, Equals, uint64(0)) + vu64, err = a.Get(1).Uint64() + c.Assert(err, IsNil) + c.Assert(vu64, Equals, uint64(6)) fv, err := a.Get(0).Float() c.Assert(err, NotNil) diff --git a/version.go b/version.go index 25ee0447..bba105b8 100644 --- a/version.go +++ b/version.go @@ -8,4 +8,4 @@ package ek // ////////////////////////////////////////////////////////////////////////////////// // // VERSION is current ek package version -const VERSION = "13.15.0" +const VERSION = "13.15.1"