From 6fd5111300dcf49a4f26283666d67824700090e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=B9=BF?= Date: Fri, 15 Nov 2024 13:17:19 +0800 Subject: [PATCH] add help to flag set --- flag.go | 4 ++++ go.mod | 3 +++ test/main.go | 20 ++++++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 go.mod create mode 100644 test/main.go diff --git a/flag.go b/flag.go index 66984bf..03e8ed6 100644 --- a/flag.go +++ b/flag.go @@ -139,6 +139,10 @@ func NewFVSet(v interface{}) (*FVSet, error) { } } } + flags.BoolFunc("help", "Show help", func(s string) error { + flags.Usage() + return nil + }) return &FVSet{ Name: name, Value: &v, diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..fd35027 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.pyer.club/kingecg/command + +go 1.23.1 diff --git a/test/main.go b/test/main.go new file mode 100644 index 0000000..81cea16 --- /dev/null +++ b/test/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" + "os" + + "git.pyer.club/kingecg/command" +) + +func main() { + type targs struct { + Test string `flag_default:"test" flag_usage:"this is test"` + TestBool bool `flag_short:"b"` + TestInt int + } + sargs := &targs{} + v, _ := command.NewFVSet(sargs) + v.Parse(os.Args[1:]) + fmt.Println(sargs) +}