From ecd1a9ce4a5241e6b71bf8ffcf4a4164617c66f9 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Thu, 11 Sep 2025 13:42:14 -0700 Subject: [PATCH 1/3] Update README with duration flag information --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index c2fddce..133d5fc 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ You can see real use example in a tool like [multicurl](https://github.com/forti It also supports (sub)commands style (where there is a word/command before the flags and remaining arguments, [fortio](https://github.com/fortio/fortio) uses that mode). +If you have durations in flags, do use the 0 dependencies [fortio.org/duration Flag](https://pkg.go.dev/fortio.org/duration#Flag) along this package. + ## Tool Example Client/Tool example (no dynamic flag url or config) [sampleTool](sampleTool/main.go) From 6cc649a91beb4bf6c47e0f1b29d3a1c745a24557 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Thu, 11 Sep 2025 13:52:55 -0700 Subject: [PATCH 2/3] Adding duration flag example. but stepped on bug with default value not showing --- go.mod | 1 + go.sum | 2 ++ sampleTool/main.go | 4 +++- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 341f54b..874b70b 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module fortio.org/cli go 1.20 require ( + fortio.org/duration v1.0.0 fortio.org/log v1.17.2 fortio.org/version v1.0.4 golang.org/x/crypto/x509roots/fallback v0.0.0-20250203165127-fa5273e46196 diff --git a/go.sum b/go.sum index 95ab0ea..787a798 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +fortio.org/duration v1.0.0 h1:zEd2LaP3ZzXGm+aRsY0KRRAzf5E7vYAWkrkcE/Jvabc= +fortio.org/duration v1.0.0/go.mod h1:RuBVqdcCKRwMmI8WIdVq8kd7ngQPCIe6G7AU0NC0XDw= fortio.org/log v1.17.2 h1:JPX/ApDXDoGzsNtXw0AJI4ai6tl9wHp4Ch6bVs1OK0Y= fortio.org/log v1.17.2/go.mod h1:1V7bPfFI7ZVTdtN9DnUCAN0ilEMs5VgKjHIDRO7Mjzk= fortio.org/struct2env v0.4.2 h1:Xh7HlS9vf2ZdRvRfmoGIasNDO8t6z36M713utVODRCo= diff --git a/sampleTool/main.go b/sampleTool/main.go index 238864c..54759f8 100644 --- a/sampleTool/main.go +++ b/sampleTool/main.go @@ -4,18 +4,20 @@ import ( "flag" "fortio.org/cli" + "fortio.org/duration" "fortio.org/log" ) func main() { myFlag := flag.String("myflag", "default", "my flag") doWait := flag.Bool("wait", false, "wait for ^C before exiting") + durationExample := duration.Flag("duration", 1*duration.Day, "example of `duration` flag with days support") cli.MinArgs = 2 cli.MaxArgs = 4 cli.Main() // Will have either called cli.ExitFunction or everything is valid // Next line output won't show when passed -quiet - log.Infof("Info test, -myflag is %q", *myFlag) + log.Infof("Info test, -myflag is %q and duration is %v", *myFlag, duration.Duration(*durationExample)) // This always shows log.Printf("Hello world, version %s, args %v", cli.ShortVersion, flag.Args()) // This shows and is colorized and structured, unless loglevel is set to critical. From 291a2b3ba46a619e876eca7854031060864dc2b9 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Thu, 11 Sep 2025 14:23:39 -0700 Subject: [PATCH 3/3] Add to readme some more, bump version that works with non 0 default --- README.md | 10 +++++++--- go.mod | 2 +- go.sum | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 133d5fc..61f6aa8 100644 --- a/README.md +++ b/README.md @@ -23,16 +23,18 @@ import ( "os" "fortio.org/cli" + "fortio.org/duration" "fortio.org/log" ) func main() { myFlag := flag.String("myflag", "default", "my flag") + durationExample := duration.Flag("duration", 1*duration.Day, "example of `duration` flag with days support") cli.MinArgs = 2 cli.MaxArgs = 4 cli.Main() // Will have either called cli.ExitFunction or everything is valid // Next line output won't show when passed -quiet - log.Infof("Info test, -myflag is %q", *myFlag) + log.Infof("Info test, -myflag is %q and duration is %v", *myFlag, duration.Duration(*durationExample)) // This always shows log.Printf("Hello world, version %s, args %v", cli.ShortVersion, flag.Args()) // This shows and is colorized and structured, unless loglevel is set to critical. @@ -50,6 +52,8 @@ sampleTool 1.2.0 usage: or 1 of the special arguments sampleTool {help|version|buildinfo} flags: + -duration duration + example of duration flag with days support (default 1d) -logger-force-color Force color output even if stderr isn't a terminal -logger-no-color @@ -73,8 +77,8 @@ or normal case: Old style, no colors: ```bash -$ sampleTool -logger-no-color a b -17:20:41 [I] Info test, -myflag is "default" +$ sampleTool -logger-no-color a b -duration 1w3h +17:20:41 [I] Info test, -myflag is "default" and duration is 1w3h 17:20:41 Hello world, version dev, args [a b] 17:20:41 [E] Error test, myflag="default", num_args="2", args="[a b]" ``` diff --git a/go.mod b/go.mod index 874b70b..ec0bd13 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module fortio.org/cli go 1.20 require ( - fortio.org/duration v1.0.0 + fortio.org/duration v1.0.1 fortio.org/log v1.17.2 fortio.org/version v1.0.4 golang.org/x/crypto/x509roots/fallback v0.0.0-20250203165127-fa5273e46196 diff --git a/go.sum b/go.sum index 787a798..05dc42c 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -fortio.org/duration v1.0.0 h1:zEd2LaP3ZzXGm+aRsY0KRRAzf5E7vYAWkrkcE/Jvabc= -fortio.org/duration v1.0.0/go.mod h1:RuBVqdcCKRwMmI8WIdVq8kd7ngQPCIe6G7AU0NC0XDw= +fortio.org/duration v1.0.1 h1:/VJLQBvMz3wes4W9tuF0OQT6UYxTU7t4bZPfq6vwdGQ= +fortio.org/duration v1.0.1/go.mod h1:RuBVqdcCKRwMmI8WIdVq8kd7ngQPCIe6G7AU0NC0XDw= fortio.org/log v1.17.2 h1:JPX/ApDXDoGzsNtXw0AJI4ai6tl9wHp4Ch6bVs1OK0Y= fortio.org/log v1.17.2/go.mod h1:1V7bPfFI7ZVTdtN9DnUCAN0ilEMs5VgKjHIDRO7Mjzk= fortio.org/struct2env v0.4.2 h1:Xh7HlS9vf2ZdRvRfmoGIasNDO8t6z36M713utVODRCo=