diff --git a/README.md b/README.md index c2fddce..61f6aa8 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) @@ -21,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. @@ -48,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 @@ -71,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 341f54b..ec0bd13 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module fortio.org/cli go 1.20 require ( + 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 95ab0ea..05dc42c 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +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= 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.