bindflags

package module
v0.0.0-...-c737de7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2025 License: Unlicense Imports: 10 Imported by: 0

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
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",
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TagName = "flag"

Functions

func BindFlags

func BindFlags(f *flag.FlagSet, a any, group ...string) error

func BindPFlags

func BindPFlags(flag *pflag.FlagSet, a any, group ...string) error

BindPFlags binds the struct member field to the cobra flag, and the struct input parameter must be a pointer type; Add a declaration to the tag of the field such as Key-value pair:Name string 'flag:"Name:name; shorthand:n; value:ss; usage:name of student"` No key: Name string 'flag:"name; n; ss; name of student"` Blend mode: "Name string 'flag:"Name:name; n; ss; name of student"`” Or define a custom type, and then implement the GetFlagTag interface for the type BindFlags 把结构体成员字段绑定到cobra FlagSet中,结构体入参必须是指针类型;在字段的tag加上声明如 键值对:Name string `flag:"Name:name;shorthand:n;value:ss;usage:name of student"` 无键值:Name string `flag:"name;n;ss;name of student"` 混合模式: “Name string `flag:"Name:name;n;ss;name of student"`” 再或者 定义一个自定义类型,然后给类型实现 GetFlagTag 接口

func MustBindFlags

func MustBindFlags(f *flag.FlagSet, a any, group ...string)

func MustBindPFlags

func MustBindPFlags(flag *pflag.FlagSet, a any)

Types

type FlagTag

type FlagTag struct {
	Name  string
	Value string
	Usage string
}

func (*FlagTag) GetName

func (f *FlagTag) GetName() string

func (*FlagTag) GetUsage

func (f *FlagTag) GetUsage() string

func (*FlagTag) GetValue

func (f *FlagTag) GetValue() string

type GetFlagTag

type GetFlagTag interface {
	GetFlagTag() IFlagTag
}

type GetPFlagTag

type GetPFlagTag interface {
	GetPFlagTag() IpFlagTag
}

type IFlagTag

type IFlagTag interface {
	GetName() string
	GetValue() string
	GetUsage() string
}

type IpFlagTag

type IpFlagTag interface {
	GetName() string
	GetShorthand() string
	GetValue() string
	GetUsage() string
}

type PFlagTag

type PFlagTag struct {
	Name      string
	Shorthand string
	Value     string
	Usage     string
}

func (*PFlagTag) GetName

func (f *PFlagTag) GetName() string

func (*PFlagTag) GetShorthand

func (f *PFlagTag) GetShorthand() string

func (*PFlagTag) GetUsage

func (f *PFlagTag) GetUsage() string

func (*PFlagTag) GetValue

func (f *PFlagTag) GetValue() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL