lazyflag

package module
v0.0.0-...-978ead0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

README

Example

Flag Use Example
package main

import (
	"errors"
	"fmt"
	"os"
	"strings"

	lazyflag "github.com/oswaldoooo/lazy-flag"
)

type Pair struct {
	Key   string
	Value string
}

func (p *Pair) UnmarshalText(text []byte) error {
	pairs := strings.Split(string(text), "-")
	if len(pairs) != 2 {
		return errors.New("pair is incorrect")
	}
	p.Key = pairs[0]
	p.Value = pairs[1]
	return nil
}

func main() {
	type Iface struct {
		Name             string
		InterfaceAddress string
	}
  // load p with short flag
	fmt.Println(lazyflag.Default.LoadAsBool(lazyflag.Short, "p"))
	var p Pair
	fmt.Println(lazyflag.Default.LoadAs(lazyflag.Long, "pair", &p))
	fmt.Println(p)
	data, err := lazyflag.LoadAsSlice[*Pair](lazyflag.Default, lazyflag.Long, "pairs")
	if err != nil {
		fmt.Fprintln(os.Stderr, "error", err)
		return
	}
	for _, d := range data {
		fmt.Println(d)
	}
	var ifa Iface
  // bind Iface object with flag
	err = lazyflag.Default.Bind(&ifa)
	if err != nil {
		fmt.Fprintln(os.Stderr, "error", err)
		return
	}
	fmt.Println(ifa)
}
String Loader Example
package lazyflag_test

import (
	"testing"

	lazyflag "github.com/oswaldoooo/lazy-flag"
)

type Getter map[string]string

func (g Getter) Get(k string) string {
	if v, ok := g[k]; ok {
		return v
	}
	return ""
}

func TestStringLoad(t *testing.T) {
	var (
		gg  = Getter{"name": "jesko", "ip_address": "127.0.0.1", "name_info": "this is info"}
		obj struct {
			Name      string
			IpAddress string
			NInfo     string `json:"name_info"`
		}
	)
	err := lazyflag.StringLoad(gg.Get, lazyflag.NewLoaderAttr("", nil), &obj)
	if err != nil {
		t.Fatal("error", err)
	}
	t.Log(obj)
}

Documentation

Index

Constants

View Source
const (
	NotFound       strerror = "not found resource"
	ErrDuplicate   strerror = "duplicate error"
	TypeNotSupport strerror = "type not support"
)
View Source
const (
	Long     = 1
	Short    = 2
	Required = 4
)

found mode

Variables

View Source
var Default = NewLoader()

Functions

func LoadAsSlice

func LoadAsSlice[T encoding.TextUnmarshaler](l *Loader, mode int, key string) (result []T, err error)

func ObjectBind

func ObjectBind(src map[string]any, dst any, attr *LoaderAttr, alias Alias) error

func StringLoad

func StringLoad(getter func(string) string, loader *LoaderAttr, v any, alias Alias) error

Types

type Alias

type Alias map[string]string

func (Alias) Get

func (a Alias) Get(key string) string

type Loader

type Loader struct {
	// contains filtered or unexported fields
}

func NewLoader

func NewLoader() *Loader

func NewLoaderFromStringSlice

func NewLoaderFromStringSlice(args []string) (*Loader, error)

func (*Loader) Bind

func (l *Loader) Bind(v any) error

func (*Loader) LazyLoad

func (l *Loader) LazyLoad(mode int, key string) []string

func (*Loader) LoadAs

func (l *Loader) LoadAs(mode int, key string, u encoding.TextUnmarshaler) error

func (*Loader) LoadAsBool

func (l *Loader) LoadAsBool(mode int, key string) bool

func (*Loader) LoadAsFloat64

func (l *Loader) LoadAsFloat64(mode int, key string, bitsize int) (float64, error)

func (*Loader) LoadAsInt

func (l *Loader) LoadAsInt(mode int, key string, bitsize int) (int64, error)

func (*Loader) LoadAsUint

func (l *Loader) LoadAsUint(mode int, key string, bitsize int) (uint64, error)

func (*Loader) LoadString

func (l *Loader) LoadString(mode int, key string) (string, error)

func (*Loader) Parse

func (l *Loader) Parse(args []string) error

func (*Loader) SetAttr

func (l *Loader) SetAttr(tagname string, wordTranslate func(string) string)

if the arg value is zero, the target attribute will not be set

type LoaderAttr

type LoaderAttr struct {
	// contains filtered or unexported fields
}

func NewLoaderAttr

func NewLoaderAttr(tagname string, wordTranslate func(string) string) *LoaderAttr

Jump to

Keyboard shortcuts

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