-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// `go run cmd.go --a2.f2 "KK"` | ||
// output: | ||
// | ||
// { | ||
// "F1": "x86xm", | ||
// "F2": "KK", | ||
// "F3": 87, | ||
// "DBUrl": ":27071", | ||
// "LogFile": "stdout", | ||
// "Debug": true, | ||
// "Name": "test", | ||
// "Short": "s", | ||
// "Age": 18, | ||
// "Usage": "usage", | ||
// "KeepTime": 1000000000, | ||
// "NoUse": "" | ||
// } | ||
// | ||
// `go run cmd.go -h` | ||
// output: | ||
// | ||
// Flags: | ||
// --a3.f3 int f3 (default 87) | ||
// --age int age (default 18) | ||
// --c.f1 string f1 (default "x86xm") | ||
// --dburl string dburl (default ":27071") | ||
// --debug enable debug model,false to disable; ,please (default true) | ||
// --f2 string f2 (default "ZH") | ||
// -h, --help help for test | ||
// --keep duration (default 1s) | ||
// --logfile string udp|udp:UdpAddr|FilePath|redirect:x (default "stdout") | ||
// --name string name (default "test") | ||
// --short string short (default "s") | ||
// --usage string usage (default "usage") | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/mars315/autoflags" | ||
"github.com/spf13/cobra" | ||
"time" | ||
) | ||
|
||
func main() { | ||
v := new(Flag) | ||
v.A3 = new(A3) | ||
rootCmd := &cobra.Command{ | ||
Use: "test auto read flag", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Printf("%s\n", v) | ||
}, | ||
} | ||
|
||
if err := autoflags.BindAndExecute(rootCmd, v, | ||
autoflags.WithTagNameOption("mapstructure"), | ||
autoflags.WithIgnoreUntaggedFieldsOption(false), | ||
autoflags.WithSquashOption(false)); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
type A1 struct { | ||
F1 string `mapstructure:"f1,desc:f1,default:x86xm"` | ||
} | ||
|
||
type A2 struct { | ||
F2 string `mapstructure:"f2,desc:f2,default:ZH"` | ||
} | ||
|
||
type A3 struct { | ||
F3 int `mapstructure:"f3,desc:f3,default:87"` | ||
} | ||
|
||
type Flag struct { | ||
A1 `mapstructure:"c"` | ||
A2 `mapstructure:",squash"` | ||
*A3 | ||
DBUrl string `mapstructure:"dburl, desc:dburl, default::27071"` | ||
LogFile string `mapstructure:"logfile, default:stdout, desc:udp|udp:UdpAddr|FilePath|redirect:x"` | ||
Debug bool `mapstructure:"debug, default:true, desc:enable debug model\\,false to disable; \\,please"` // "\\" before "," | ||
Name string `mapstructure:",desc:name, default:test"` | ||
Short string `mapstructure:"short, desc:short, default:s"` | ||
Age int `mapstructure:"age, desc:age, default:18"` | ||
Usage string `mapstructure:"usage, desc:usage, default:usage"` | ||
KeepTime time.Duration `mapstructure:"keep,omitempty, default:1s""` | ||
NoUse string `mapstructure:"-"` | ||
} | ||
|
||
func (f *Flag) String() string { | ||
str, _ := json.MarshalIndent(f, "", "\t") | ||
return string(str) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/mars315/autoflags" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type Config struct { | ||
Name string `flag:"name,short:N,default:default name,desc:your name"` | ||
Age int `flag:"age,short:A,default:18,desc:your age"` | ||
} | ||
|
||
// main.go | ||
// `go run main.go -h` | ||
// output: | ||
// Flags: | ||
// -A, --age int your age (default 18) | ||
// -h, --help help for this command | ||
// -N, --name string your name (default "default name") | ||
// | ||
// `go run cmd.go --age 133` | ||
// output: main.Config{Name:"default name", Age:133} | ||
// | ||
|
||
func main() { | ||
var cfg Config | ||
rootCmd := &cobra.Command{ | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Printf("%#v\n", cfg) | ||
}, | ||
} | ||
if err := autoflags.BindAndExecute(rootCmd, &cfg); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright © 2023 mars315 <[email protected]>. | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file. | ||
// | ||
|
||
package builtin | ||
|
||
type ( | ||
// UnsignedInteger . | ||
UnsignedInteger interface { | ||
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ||
} | ||
|
||
// SignedInteger . | ||
SignedInteger interface { | ||
~int | ~int8 | ~int16 | ~int32 | ~int64 | ||
} | ||
|
||
// Integer . | ||
Integer interface { | ||
UnsignedInteger | SignedInteger | ||
} | ||
|
||
// Float . | ||
Float interface { | ||
~float32 | ~float64 | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright © 2023 mars315 <[email protected]>. | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file. | ||
// | ||
|
||
package stringx | ||
|
||
import ( | ||
"github.com/mars315/autoflags/lib/builtin" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func ToBool(s string) bool { | ||
if len(s) == 0 { | ||
return false | ||
} | ||
|
||
s = strings.ToLower(s) | ||
return s == "true" || s == "t" | ||
} | ||
|
||
// Atof string to float64 | ||
func Atof[T builtin.Float](v string) T { | ||
vF64, _ := strconv.ParseFloat(v, 64) | ||
return T(vF64) | ||
} | ||
|
||
// Atoi string to signed integer | ||
func Atoi[T builtin.SignedInteger](v string) T { | ||
vInt, _ := strconv.ParseInt(v, 10, 64) | ||
return T(vInt) | ||
} | ||
|
||
// AtoSlice string to signed integer slice | ||
func AtoSlice[T builtin.SignedInteger](s string, sep string) []T { | ||
ss := strings.Split(s, sep) | ||
l := make([]T, 0, len(ss)) | ||
for _, v := range ss { | ||
l = append(l, Atoi[T](v)) | ||
} | ||
return l | ||
} | ||
|
||
// Split Like strings.Split, but remove the spaces from each string. | ||
func Split(s0, sep string) []string { | ||
s := strings.TrimSpace(s0) | ||
l := strings.Split(s, sep) | ||
|
||
r := l[:0] | ||
for _, str := range l { | ||
r = append(r, strings.TrimSpace(str)) | ||
} | ||
|
||
return r | ||
} |