From 74e7586c36630dcdb39de7ee5c44e977f0b36803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=B9=BF?= Date: Sat, 16 Nov 2024 15:19:59 +0800 Subject: [PATCH] reorg code and add Readme --- README.md | 46 +++++++++++++++++++++++++++- {test => example/test}/main.go | 0 {testcmd => example/testcmd}/main.go | 0 3 files changed, 45 insertions(+), 1 deletion(-) rename {test => example/test}/main.go (100%) rename {testcmd => example/testcmd}/main.go (100%) diff --git a/README.md b/README.md index 0024a1b..27ed55b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,47 @@ # command -utils for command line app \ No newline at end of file +utils for command line app + +Provide two style for command line args parse: + +- use struct field tag +- use command + +## Usage + +use struct field tag + +```go +type targs struct { + Test string `flag_default:"test" flag_usage:"this is test"` + TestBool bool `flag_short:"b"` + TestInt int +} + sargs := &targs{} + v, _ := NewFVSet(sargs) + v.Usage() + err := Parse([]string{"--test", "test", "--test-bool", "--test-int", "1"}, v) +``` + +use command style + +```go +package main + +import ( + "fmt" + "os" + + "git.pyer.club/kingecg/command" +) + +func main() { + var cmd = command.NewCommand("test", "test command") + cmd.AddArg("arg1", "a", "arg1 description", "default value") + cmd.AddArg("arg2", "b", "arg2 description", "default value") + cmd.AddSubCommand("sub1", "sub1 description") + cmd.Parse(os.Args[1:]) + fmt.Println(cmd.GetGlobalOptions()) +} + +``` \ No newline at end of file diff --git a/test/main.go b/example/test/main.go similarity index 100% rename from test/main.go rename to example/test/main.go diff --git a/testcmd/main.go b/example/testcmd/main.go similarity index 100% rename from testcmd/main.go rename to example/testcmd/main.go