anyflag

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

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

Go to latest
Published: Jan 5, 2024 License: Apache-2.0 Imports: 5 Imported by: 3

README

Anyflag

Build Status Go Report Card

Anyflag is an implementation of Cobra pflag.Value and pflag.SliceValue interfaces using Go Generics.

To bind your custom type to a flag, all you have to do is specify the value type and parser function, and you are done, no boilerplate.

It supports any type including, but not limited to: enums, maps, slices, structs, struct pointers.

It also supports defining custom String() functions to redact passwords, see redact example.

Installation

go get github.com/mmatczuk/anyflag

Examples

This example shows how anytype can be used for JSON encoded maps.

func parseJSONMap(val string) (map[string]interface{}, error) {
	var m map[string]interface{}
	return m, json.Unmarshal([]byte(val), &m)
}

func newCommand() *cobra.Command {
	var m map[string]interface{}

	cmd := &cobra.Command{
		Use: "json-map",
		Run: func(cmd *cobra.Command, args []string) {
			fmt.Fprintln(cmd.OutOrStdout(), m)
		},
	}

	fs := cmd.Flags()
	value := anyflag.NewValue[map[string]interface{}](nil, &m, parseJSONMap)

	fs.VarP(value, "map", "", "map")

	return cmd
}

func main() {
	newCommand().Execute()
}

More examples can be found in examples directory.

License

This project is based on spf13/pflag licensed under the BSD 3-Clause "New" or "Revised" License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnumParser

func EnumParser[T fmt.Stringer](all ...T) func(val string) (T, error)

EnumParser returns parse function for string based enums.

func StringerParser

func StringerParser[T ints](index []uint8, name string) func(val string) (T, error)

StringerParser returns parse function for enums processed with stringer tool.

Types

type SliceValue

type SliceValue[T any] struct {
	// contains filtered or unexported fields
}

SliceValue is a generic pflag.SliceValue for a slice of T.

func NewSliceValue

func NewSliceValue[T any](val []T, p *[]T, parse func(val string) (T, error)) *SliceValue[T]

NewSliceValue returns a new SliceValue[T] with the given value, pointer to a slice of T, and a parse function.

func NewSliceValueWithRedact

func NewSliceValueWithRedact[T any](val []T, p *[]T, parse func(val string) (T, error), redact func(T) string) *SliceValue[T]

NewSliceValueWithRedact returns a new SliceValue[T] and additionally sets custom String() function for T. Redact primary purpose is to redact passwords to prevent them from leaking in logs.

func (*SliceValue[T]) Append

func (s *SliceValue[T]) Append(val string) error

func (*SliceValue[T]) GetSlice

func (s *SliceValue[T]) GetSlice() []string

func (*SliceValue[T]) Replace

func (s *SliceValue[T]) Replace(val []string) error

func (*SliceValue[T]) Set

func (s *SliceValue[T]) Set(val string) error

func (*SliceValue[T]) String

func (s *SliceValue[T]) String() string

func (*SliceValue[T]) Type

func (s *SliceValue[T]) Type() string

func (*SliceValue[T]) Unredacted

func (s *SliceValue[T]) Unredacted() pflag.Value

Unredacted returns a copy of SliceValue[T] without redact function.

type Value

type Value[T any] struct {
	// contains filtered or unexported fields
}

Value is a generic pflag.Value for a T.

func NewValue

func NewValue[T any](val T, p *T, parse func(val string) (T, error)) *Value[T]

NewValue returns a new Value[T] with the given value, pointer to a T, and a parse function.

func NewValueWithRedact

func NewValueWithRedact[T any](val T, p *T, parse func(val string) (T, error), redact func(T) string) *Value[T]

NewValueWithRedact returns a new Value[T] and additionally sets custom String() function. Redact primary purpose is to redact passwords to prevent them from leaking in logs.

func (*Value[T]) Set

func (v *Value[T]) Set(val string) error

func (*Value[T]) String

func (v *Value[T]) String() string

func (*Value[T]) Type

func (v *Value[T]) Type() string

func (*Value[T]) Unredacted

func (v *Value[T]) Unredacted() pflag.Value

Unredacted returns a copy of Value[T] without redact function.

Directories

Path Synopsis
examples
url

Jump to

Keyboard shortcuts

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