Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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]"
```
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
4 changes: 3 additions & 1 deletion sampleTool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading