https://github.com/li-giegie/bindflags
Quickly bind the structure to a cobra-cli comoand or pflag flagset 快速将结构绑定到 cobra-cli comoand 或 pflag 标志集
https://github.com/li-giegie/bindflags
bindflag cobra go pflag struct
Last synced: 3 months ago
JSON representation
Quickly bind the structure to a cobra-cli comoand or pflag flagset 快速将结构绑定到 cobra-cli comoand 或 pflag 标志集
- Host: GitHub
- URL: https://github.com/li-giegie/bindflags
- Owner: Li-giegie
- License: unlicense
- Created: 2025-05-11T11:39:07.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-11T18:05:14.000Z (about 1 year ago)
- Last Synced: 2025-05-13T00:55:17.603Z (about 1 year ago)
- Topics: bindflag, cobra, go, pflag, struct
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bindflags
Quickly bind the structure to a cobra-cli comoand or pflag flagset
快速将结构绑定到 cobra-cli comoand 或 pflag 标志集
# Usage
```
go get github.com/Li-giegie/bindflags
```
```go
func TestBindFlags(t *testing.T) {
f := pflag.NewFlagSet("test", pflag.ContinueOnError)
s := new(student)
MustBindFlags(f, s)
f.PrintDefaults()
// -a, --age int age
// -d, --desc string desc
// -n, --name string name (default "ss")
// -s, --sex sex (default true)
//PASS
}
type student struct {
Name string `flag:"Name:name;shorthand:n;value:ss;usage:name of student"`
Age int `flag:"age;a;0;usage:age of student"`
Sex bool `flag:"sex;s;true;sex"`
Desc studentDesc
}
type studentDesc string
func (s studentDesc) GetFlagTag() IFlagTag {
return &FlagTag{
Name: "desc",
Shorthand: "d",
Value: "",
Usage: "student description",
}
}
```