gflag

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

README

gflag

Yet another CLI flags library that reuses the great package github.com/spf13/pflag, with an interface built on go generics.

About CLI flags

There are quite many existing CLI flag handling libraries. The most popular one is (by a mile), github.com/spf13/pflag (forked from githb.com/ogier/pflag).

It is used by other very popular packages spf13/viper and spf13/cobra.

The second most popular (by number of imports by public repositories) is github.com/jessevdk/go-flags.

... to be continued.

Programmatic approach

Declarative approach

Declarative approaches are implemented thanks to golang's struct tags.

... to be continued.

Documentation

Overview

Package gflag exposes generic types to deal with flags.

the new types can be readily used as extensions to the github.com/spf13/flag package.

Most of the current pflag functionality can be obtained from gflag using generic types.

There are a few exceptions though: * []byte semantics as base64-encoded string is not available yet (will be, as part of the extensions package). * int semantics a increment/decrement count is not avaiable directly (it is available in the extensions package) * map semantics (StringToXXX family of pflag types) are not supported yet.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FlaggablePrimitives

type FlaggablePrimitives interface {
	constraints.Integer |
		constraints.Float |
		~string |
		~bool |
		~[]byte // aka: ~[]uint8 |
}

FlaggablePrimitives is a type constraint that holds all primitive types supported by pflag.

Exception: complex types are not supported.

type FlaggableTypes

type FlaggableTypes interface {
	time.Duration |
		net.IP |
		net.IPNet |
		net.IPMask |

		~struct{}
}

FlaggableTypes is a type constraint that holds all types supported by pflag, besides primitive types.

type SliceValue

type SliceValue[T FlaggablePrimitives | FlaggableTypes] struct {
	Value *[]T
	// contains filtered or unexported fields
}

SliceValue is a generic type that implements github.com/spf13/pflag.Value and SliceValue.

func NewFlagSliceValue

func NewFlagSliceValue[T FlaggablePrimitives | FlaggableTypes](addr *[]T, defaultValue []T) *SliceValue[T]

NewFlagSliceValue constructs a generic flag compatible with github.com/spf13/pflag.SliceValue.

Since the flag type is inferred from the underlying data type, some flexibility allowed by pflag is not always possible at this point.

For example, when T = []string, NewFlagSliceValue adopts the semantics of the pflag.StringSlice (with comma-separated values), whereas pflag also supports a StringArray flag.

func (*SliceValue[T]) Append

func (m *SliceValue[T]) Append(strValue string) error

Append a single element to the SliceValue, from its string representation.

func (*SliceValue[T]) GetSlice

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

GetSlice return a []string representation of the slice values.

func (*SliceValue[T]) MarshalFlag

func (m *SliceValue[T]) MarshalFlag() (string, error)

MarshalFlag implements go-flags Marshaller interface

func (*SliceValue[T]) Replace

func (m *SliceValue[T]) Replace(strValues []string) error

func (*SliceValue[T]) Set

func (m *SliceValue[T]) Set(strValue string) error

Set knows how to config a string representation of the Value into a type T.

func (*SliceValue[T]) String

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

func (*SliceValue[T]) Type

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

func (*SliceValue[T]) UnmarshalFlag

func (m *SliceValue[T]) UnmarshalFlag(value string) error

UnmarshalFlag implements go-flags Unmarshaller interface

type Value

type Value[T FlaggablePrimitives | FlaggableTypes] struct {
	Value       *T
	NoOptDefVal string
}

Value is a generic type that implements github.com/spf13/pflag.Value.

func NewFlagValue

func NewFlagValue[T FlaggablePrimitives | FlaggableTypes](addr *T, defaultValue T) *Value[T]

NewFlagValue constructs a generic flag compatible with github.com/spf13/pflag.Value.

Since the flag type is inferred from the underlying data type, some flexibility allowed by pflag is not always possible at this point.

For example, when T = []byte, NewFlagValue adopts the semantics of the pflag.BytesHex flag, whereas pflag aslo supports a BytesBase64 flag.

Similarly, when T = int, we adopt the semantics of pflag.Int and not pflag.Count.

func (*Value[T]) MarshalFlag

func (m *Value[T]) MarshalFlag() (string, error)

MarshalFlag implements go-flags Marshaller interface

func (*Value[T]) Set

func (m *Value[T]) Set(strValue string) error

Set knows how to config a string representation of the Value into a type T.

func (*Value[T]) String

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

String knows how to yield a string representation of type T.

func (*Value[T]) Type

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

func (*Value[T]) UnmarshalFlag

func (m *Value[T]) UnmarshalFlag(value string) error

UnmarshalFlag implements go-flags Unmarshaller interface

Directories

Path Synopsis
Package extensions provides extensions to the github.com/spf13/pflag package.
Package extensions provides extensions to the github.com/spf13/pflag package.

Jump to

Keyboard shortcuts

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