sflag

package
v0.8.6 Latest Latest
Warning

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

Go to latest
Published: May 4, 2024 License: MIT Imports: 5 Imported by: 0

README

Usage

package main

import (
	"fmt"
	"time"

	"github.com/konoui/lipo/pkg/sflag"
)

func example1() {
	fs := sflag.NewFlagSet("simple usage")
	debug := fs.Bool("debug", "enable debug mode", sflag.WithShortName("d")) // allow -debug and -d
	values := fs.Strings("values", "multiple values")
	err := fs.Parse([]string{"-values", "a", "b", "c", "-d", "arg1", "arg2"})
	if err != nil {
		panic(err)
	}

	fmt.Println(fs.Args())    // [arg1 arg2]
	fmt.Println(debug.Get())  // true
	fmt.Println(values.Get()) // [a b c]
}

func example2() {
	fs := sflag.NewFlagSet("a custom flag definition")

	timeValue := sflag.NewValue(new(time.Duration), time.ParseDuration)
	timeFlag := sflag.Register(fs, timeValue, "time", "-time 20s", sflag.WithDenyDuplicate())

	p := new([]time.Duration)
	timesValue := sflag.NewValue(p, func(v string) ([]time.Duration, error) {
		pd, err := time.ParseDuration(v)
		if err != nil {
			return nil, err
		}
		*p = append(*p, pd)
		return *p, nil
	})
	timesFlag := sflag.Register(fs, timesValue, "times", "-times 20s -times 30h")

	err := fs.Parse([]string{"-time", "7200s", "-times", "20m", "-times", "30h"})
	if err != nil {
		panic(err)
	}

	fmt.Println(timeFlag.Get().Hours()) // 2
	fmt.Println(timesFlag.Get())        // [20m0s 30h0m0s]
}

func main() {
	example1()
	example2()
}

Documentation

Index

Constants

View Source
const (
	CapNoLimit = -1
)

Variables

This section is empty.

Functions

func UsageFunc

func UsageFunc(groups ...*Group) func() string

Types

type Flag

type Flag struct {
	Name      string
	ShortName string
	Usage     string
	// contains filtered or unexported fields
}

type FlagGetter added in v0.6.0

type FlagGetter interface {
	Flag() *Flag
}

type FlagOpt added in v0.8.0

type FlagOpt func(*Flag)

func WithDenyDuplicate

func WithDenyDuplicate() FlagOpt

WithDenyDuplicate Parse() returns an error if encountering duplicated flags are specified. This is used for a custom flag definition. Pre-defined flags(Bool/String etc) are enabled by default.

func WithShortName added in v0.7.0

func WithShortName(short string) FlagOpt

type FlagRef added in v0.3.0

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

FlagRef is a wrapper to access the Flag and the flag value.

func Register added in v0.7.0

func Register[T any](f *FlagSet, v *Value[T], name, usage string, opts ...FlagOpt) *FlagRef[T]

Register registers a Value with a Name and an Usage as a Flag This is used to define a custom flag type.

func (*FlagRef[T]) Flag added in v0.3.0

func (fr *FlagRef[T]) Flag() *Flag

Flag returns the Flag to access the flag name and the usage.

func (*FlagRef[T]) Get added in v0.6.0

func (fr *FlagRef[T]) Get() T

Get returns a typed flag value

type FlagSet

type FlagSet struct {
	Usage func() string
	// contains filtered or unexported fields
}

func NewFlagSet

func NewFlagSet(name string) *FlagSet

func (*FlagSet) Args

func (f *FlagSet) Args() []string

func (*FlagSet) Bool

func (f *FlagSet) Bool(name, usage string, opts ...FlagOpt) *FlagRef[bool]

Bool represents `-flag` NOT `-flag true/false`

func (*FlagSet) FixedStringFlags added in v0.3.0

func (f *FlagSet) FixedStringFlags(name, usage string, opts ...FlagOpt) *FlagRef[[][2]string]

FixedStringFlags represents `-flag <value1> <value2> -flag <value3> <value4> -flag ...` This is a reference implementation

func (*FlagSet) NewGroup

func (f *FlagSet) NewGroup(name string) *Group

func (*FlagSet) Parse

func (f *FlagSet) Parse(args []string) error

func (*FlagSet) String

func (f *FlagSet) String(name, usage string, opts ...FlagOpt) *FlagRef[string]

String represents `-flag <value>`

func (*FlagSet) StringFlags added in v0.3.0

func (f *FlagSet) StringFlags(name, usage string, opts ...FlagOpt) *FlagRef[[]string]

StringFlags represents `-flag <value1> -flag <value2> -flag <value3> -flag ...`

func (*FlagSet) Strings added in v0.3.0

func (f *FlagSet) Strings(name, usage string, opts ...FlagOpt) *FlagRef[[]string]

Strings represents `-flag <value1> <value2> <value3> ...`

type FlagType added in v0.2.0

type FlagType int
const (
	TypeRequired FlagType = iota + 1
	TypeOptional
)

func (FlagType) String added in v0.2.0

func (t FlagType) String() string

type Group

type Group struct {
	Name string
	// contains filtered or unexported fields
}

func LookupGroup

func LookupGroup(groups ...*Group) (*Group, error)

func (*Group) AddDescription

func (g *Group) AddDescription(s string) *Group

func (*Group) AddOptional added in v0.3.0

func (g *Group) AddOptional(fg FlagGetter) *Group

func (*Group) AddRequired added in v0.3.0

func (g *Group) AddRequired(fg FlagGetter) *Group

func (*Group) String added in v0.6.0

func (g *Group) String() string

func (*Group) Usage

func (g *Group) Usage() string

type Value

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

Value please use NewValue(s) function instead of the definition

func NewValue added in v0.8.0

func NewValue[T any](p *T, converter func(v string) (T, error)) *Value[T]

NewValue is used for a single value definition. e.g.) bool, string, int. When implementing `-flag value1 -flag value2 -flag value3`, it can be also useful.

func NewValues added in v0.8.0

func NewValues[T any](p *T, converter func(v string) (T, error), capper func() int) *Value[T]

NewValues is used for a slice definition. e.g. -flag value1 value2 value3

Jump to

Keyboard shortcuts

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