parseflags

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

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

Go to latest
Published: Jul 30, 2024 License: BSD-3-Clause Imports: 6 Imported by: 2

README

go-parseflags in a nutshell

go-parseflags is a Golang library that aims to simplify parsing simple flags. Instead of defining the flags in method calls like StringSliceVarP, define them inline using tags:

type Flags struct {
	MyFlag int64            `flag:"my-flag" description:"my custom flag"`
	MyMultipleFlag []string `flag:"other-flag" description:"my flag that can be specified multiple times"`
	// Add more flags for your program
}

var flags = Flags{
	MyFlag: 2, // default value
}

func init() {
	var flagset = parseflags.CreateFlagset(&flags)
	flagset.Parse(os.Args)
}

Longer explanation

As shown in the example above, you set the properties for your flags using tags on your fields. Currently supported tags are:

  • flag for the flag name (this is customizable, see FlagsetBuilder.NameTags)
  • description for the flag description
  • shorthand for a single character flag shorthand
  • hidden (if the tag exists, the flag is not shown in the help)
  • deprecated (content is a deprecation text that is printed when the flag is used)

By default, most of the primitive types (int*, uint*, float*, bool, string) are supported. You can add support for custom field types by implementing StringParsable in your type or by calling RegisterConverter with your type and a matching conversion method.

go-parseflag supports slices if the underlying type is supported via StringParsable or RegisterConverter.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateFlagset

func CreateFlagset(config interface{}) *flag.FlagSet
Example
testConfig := struct {
	A int64    `flag:"alpha" description:"an int value"`
	B []string `flag:"beta" description:"some strings"`
	C bool     `flag:"gamma" hidden:"true"`
}{
	B: []string{"defaultvalue"},
}

args := []string{"--alpha", "10", "--beta", "b", "--beta", "b2", "--gamma"}
flags := CreateFlagset(&testConfig)
flags.Parse(args)
fmt.Println(testConfig.A)
fmt.Println(testConfig.B)
fmt.Println(testConfig.C)
Output:

10
[b b2]
true

func RegisterConverter

func RegisterConverter(targetType interface{}, converter Converter)

func Type

func Type(pointer interface{}) string

Types

type Converter

type Converter func(string) (interface{}, error)
Example
testConfig := struct {
	A CustomConvertible   `flag:"alpha" description:"an int value with custom converter"`
	B []CustomConvertible `flag:"beta" description:"a slice of an int value with custom converter"`
}{
	A: CustomConvertible(1),
}

RegisterConverter(CustomConvertible(0), func(val string) (interface{}, error) {
	ival, err := strconv.Atoi(val)
	if err == nil {
		return CustomConvertible(ival), nil
	}
	return nil, err
})

args := []string{"--alpha", "10", "--beta", "5"}
flags := CreateFlagset(&testConfig)
flags.Parse(args)
fmt.Println(testConfig.A)
fmt.Println(testConfig.B)
Output:

10
[5]

type ElementFilter

type ElementFilter interface {
	Filter(field reflect.StructField) bool
}

type FlagsetBuilder

type FlagsetBuilder struct {
	Filter   ElementFilter
	NameTags []string
}

func NewBuilder

func NewBuilder() *FlagsetBuilder

func (*FlagsetBuilder) Build

func (b *FlagsetBuilder) Build(config interface{}) *flag.FlagSet

func (*FlagsetBuilder) RecurseReflectively

func (b *FlagsetBuilder) RecurseReflectively(config interface{}, callback func(name string, tag reflect.StructTag, value interface{}))

func (*FlagsetBuilder) SetFilter

func (b *FlagsetBuilder) SetFilter(filter ElementFilter) *FlagsetBuilder

func (*FlagsetBuilder) SetNameTags

func (b *FlagsetBuilder) SetNameTags(tags ...string) *FlagsetBuilder

type NamedType

type NamedType interface {
	Type() string
}

type StringParsable

type StringParsable interface {
	Set(value string) (err error)
}

Jump to

Keyboard shortcuts

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