Documentation
¶
Index ¶
- Constants
- Variables
- func AddOption[T Arg](ap *ArgParser, arg T) T
- func AddPositional[T Arg](ap *ArgParser, arg T) T
- func Assert(assertion any, args ...any)
- func Center(s string, minWidth int) string
- func CheckResult(result *error) error
- func Clamp[T constraints.Ordered](value, min, max T) T
- func ConvertAssign(src any, dst any) error
- func DaysBetweenTimes(startDate, endDate time.Time) int
- func Ellipsis(s string, threshold int) string
- func Empty(s string) bool
- func EqualsCase(s1, s2 string, caseSensitive bool) bool
- func ExpandUsersAndVars(value string) string
- func ExpandVars(value string) string
- func FindOption[T Arg](ap *ArgParser, id int) T
- func FindPositional[T Arg](ap *ArgParser, id int) T
- func FormatDate(dt time.Time) string
- func FormatDateTime(dt time.Time) string
- func HasOneOfPrefixesCase(s string, prefixes []string, caseSensitive bool) bool
- func HasPrefixCase(s, prefix string, caseSensitive bool) bool
- func If[T any](condition bool, trueValue T) T
- func IfElse[T any](condition bool, trueValue T, falseValue T) T
- func IndexLastNotAny(s string, chars string) int
- func IndexNotAny(s string, chars string) int
- func IsEmpty(any any) bool
- func IsEmptyString(v string) bool
- func IsNil(any any) bool
- func LeftJustify(s string, minWidth int) string
- func LoadEnvAsList() []string
- func LoadEnvAsMap() map[string]string
- func LoadEnvBool(key string, defaultValue bool) bool
- func LoadEnvFile(filename string) error
- func LoadEnvFloat(key string, defaultValue float64) float64
- func LoadEnvInt(key string, defaultValue int) int
- func LoadEnvString(key string, defaultValue string) string
- func NewError(inner error, args ...any) error
- func PackDateTime(value time.Time) string
- func PadCenter(builder *strings.Builder, minWidth int) *strings.Builder
- func PadLeft(builder *strings.Builder, minWidth int) *strings.Builder
- func PadRight(builder *strings.Builder, minWidth int) *strings.Builder
- func Panic(args ...any)
- func PanicResult(result *error)
- func ParseDateTime(value string) (time.Time, error)
- func RandomChars(n int) (string, error)
- func ReorderTimes(startDate time.Time, endDate time.Time) (time.Time, time.Time)
- func RightJustify(s string, minWidth int) string
- func SetEnvBool(key string, value bool) error
- func SetEnvFloat(key string, value float64) error
- func SetEnvInt(key string, value int) error
- func SetEnvString(key string, value string) error
- func SetResult[T any](result *error, err error, value T) T
- func TimeAddDays(date time.Time, days int) time.Time
- func ToError(any any, args ...any) error
- func Truncate(s string, maxLength int) string
- func UnpackBool(value any, result *error) bool
- func UnpackDateTime(value any, result *error) time.Time
- func UnpackFloat64(value any, result *error) float64
- func UnpackInt(value any, result *error) int
- func UnpackList[T any, V any](list DataList, unpacker UnpackFunc[T, V], result *error) []T
- func UnpackMap[T any, V any](bag DataBag, unpacker UnpackFunc[T, V], result *error) map[string]T
- func UnpackString(value any, result *error) string
- func UnpackStringWithEnvReplaced(value any, result *error) string
- func UnsetEnv(key string) error
- type Arg
- type ArgParser
- func (ap *ArgParser) ActiveSubparser() *ArgParser
- func (ap *ArgParser) AddOption(arg Arg) error
- func (p *ArgParser) AddPositional(arg Arg) error
- func (ap *ArgParser) AddSubparser(sp *ArgParser) error
- func (ap *ArgParser) FindOption(id int) Arg
- func (ap *ArgParser) FindPositional(id int) Arg
- func (ap *ArgParser) HasPassthru() bool
- func (ap *ArgParser) ID() int
- func (ap *ArgParser) Names() []string
- func (ap *ArgParser) Parse() error
- func (ap *ArgParser) ParseWithArgs(args []string) error
- func (ap *ArgParser) Passthru() []string
- func (ap *ArgParser) SetTitle(title string) *ArgParser
- func (ap *ArgParser) SetUsage(usage string) *ArgParser
- func (ap *ArgParser) Title() string
- func (ap *ArgParser) Usage() string
- type DataBag
- func (bag DataBag) Bag(key string, result *error) DataBag
- func (bag DataBag) Bool(key string, result *error) bool
- func (bag DataBag) BoolWithDefault(key string, defaultValue bool) bool
- func (bag DataBag) Contains(key string) bool
- func (bag DataBag) DateTime(key string, result *error) time.Time
- func (bag DataBag) DateTimeWithDefault(key string, defaultValue time.Time) time.Time
- func (bag DataBag) Float64(key string, result *error) float64
- func (bag DataBag) Float64WithDefault(key string, defaultValue float64) float64
- func (bag DataBag) Get(index any) (any, error)
- func (bag DataBag) Int(key string, result *error) int
- func (bag DataBag) IntWithDefault(key string, defaultValue int) int
- func (bag DataBag) Len() int
- func (bag DataBag) List(key string, result *error) DataList
- func (bag DataBag) String(key string, result *error) string
- func (bag DataBag) StringWithDefault(key string, defaultValue string) string
- func (bag DataBag) StringWithEnvReplaced(key string, result *error) string
- type DataList
- func (list DataList) Bag(index int, result *error) DataBag
- func (list DataList) Bool(index int, result *error) bool
- func (list DataList) BoolWithDefault(index int, defaultValue bool) bool
- func (list DataList) DateTime(index int, result *error) time.Time
- func (list DataList) DateTimeWithDefault(index int, defaultValue time.Time) time.Time
- func (list DataList) Float64(index int, result *error) float64
- func (list DataList) Float64WithDefault(index int, defaultValue float64) float64
- func (list DataList) Get(index any) (any, error)
- func (list DataList) Int(index int, result *error) int
- func (list DataList) IntWithDefault(index int, defaultValue int) int
- func (list DataList) Len() int
- func (list DataList) List(index int, result *error) DataList
- func (list DataList) String(index int, result *error) string
- func (list DataList) StringWithDefault(index int, defaultValue string) string
- type Date
- type Event
- type IClosable
- type IConvertible
- type ID
- func NewFlakeID() (ID, error)
- func NewFlakeIDWithPrefix(prefix string) (ID, error)
- func NewKSUID() (ID, error)
- func NewKSUIDWithPrefix(prefix string) (ID, error)
- func NewMathID() ID
- func NewMathIDWithPrefix(prefix string) ID
- func NewNanoID(length int) (ID, error)
- func NewNanoIDWithPrefix(prefix string, length int) (ID, error)
- func NewObjectID() (ID, error)
- func NewObjectIDWithPrefix(prefix string) (ID, error)
- func NewSnowflakeID(nodeID int64) (ID, error)
- func NewSnowflakeIDWithPrefix(prefix string, nodeID int64) (ID, error)
- func NewULID() (ID, error)
- func NewULIDWithPrefix(prefix string) (ID, error)
- func NewUUID() (ID, error)
- func NewUUIDWithPrefix(prefix string) (ID, error)
- type IDataParcel
- type IEventHandler
- type IEventSender
- type IPackable
- type IResult
- type IStatusResult
- type NumericOption
- func (o *NumericOption) Accept(value string) Result[PARSING_RESULT]
- func (o *NumericOption) ID() int
- func (o *NumericOption) Init()
- func (o *NumericOption) Names() []string
- func (o *NumericOption) SetDefaultValue(value int) *NumericOption
- func (o *NumericOption) SetUsage(usage string) *NumericOption
- func (o *NumericOption) SetValueName(valueName string) *NumericOption
- func (o *NumericOption) SetValueRange(min, max int) *NumericOption
- func (o *NumericOption) SetValueRef(valueRef *int) *NumericOption
- func (o *NumericOption) UpdateValueRef()
- func (o *NumericOption) Usage() string
- func (o *NumericOption) UsageDetails(ap *ArgParser, arr *StringArray)
- func (o *NumericOption) Value() int
- type NumericOptionList
- func (o *NumericOptionList) Accept(value string) Result[PARSING_RESULT]
- func (o *NumericOptionList) ID() int
- func (o *NumericOptionList) Init()
- func (o *NumericOptionList) Names() []string
- func (o *NumericOptionList) SetCount(count int) *NumericOptionList
- func (o *NumericOptionList) SetCountRange(min, max int) *NumericOptionList
- func (o *NumericOptionList) SetUsage(usage string) *NumericOptionList
- func (o *NumericOptionList) SetValueName(valueName string) *NumericOptionList
- func (o *NumericOptionList) SetValueRange(min, max int) *NumericOptionList
- func (o *NumericOptionList) SetValueRef(valueRef *[]int) *NumericOptionList
- func (o *NumericOptionList) UpdateValueRef()
- func (o *NumericOptionList) Usage() string
- func (o *NumericOptionList) UsageDetails(ap *ArgParser, arr *StringArray)
- func (o *NumericOptionList) Values() []int
- type Optional
- type PARSING_RESULT
- type Pool
- func (p *Pool[KeyT, ValueT]) Clear()
- func (p *Pool[KeyT, ValueT]) Clone() *Pool[KeyT, ValueT]
- func (p *Pool[KeyT, ValueT]) Contains(key KeyT) bool
- func (p *Pool[KeyT, ValueT]) Delete(key KeyT)
- func (p *Pool[KeyT, ValueT]) ForEach(f func(key KeyT, value ValueT) bool)
- func (p *Pool[KeyT, ValueT]) Get(key KeyT) ValueT
- func (p *Pool[KeyT, ValueT]) Items() []struct{ ... }
- func (p *Pool[KeyT, ValueT]) Keys() []KeyT
- func (p *Pool[KeyT, ValueT]) Len() int
- func (p *Pool[KeyT, ValueT]) Map() map[KeyT]ValueT
- func (p *Pool[KeyT, ValueT]) Merge(other *Pool[KeyT, ValueT])
- func (p *Pool[KeyT, ValueT]) Set(key KeyT, value ValueT)
- func (p *Pool[KeyT, ValueT]) String() string
- func (p *Pool[KeyT, ValueT]) Values() []ValueT
- type PoolFactory
- type Positional
- func (o *Positional) Accept(value string) Result[PARSING_RESULT]
- func (o *Positional) ID() int
- func (o *Positional) Init()
- func (o *Positional) Names() []string
- func (o *Positional) SetUsage(usage string) *Positional
- func (o *Positional) SetValueName(valueName string) *Positional
- func (o *Positional) SetValueRef(valueRef *string) *Positional
- func (o *Positional) UpdateValueRef()
- func (o *Positional) Usage() string
- func (o *Positional) UsageDetails(_ *ArgParser, arr *StringArray)
- func (o *Positional) Value() string
- type PositionalList
- func (o *PositionalList) Accept(value string) Result[PARSING_RESULT]
- func (o *PositionalList) ID() int
- func (o *PositionalList) Init()
- func (o *PositionalList) Names() []string
- func (o *PositionalList) SetCount(count int) *PositionalList
- func (o *PositionalList) SetCountRange(min, max int) *PositionalList
- func (o *PositionalList) SetUsage(usage string) *PositionalList
- func (o *PositionalList) SetValueName(valueName string) *PositionalList
- func (o *PositionalList) SetValueRef(valueRef *[]string) *PositionalList
- func (o *PositionalList) UpdateValueRef()
- func (o *PositionalList) Usage() string
- func (o *PositionalList) UsageDetails(_ *ArgParser, arr *StringArray)
- func (o *PositionalList) Values() []string
- type Result
- func NewBooleanErrorResult(err error) Result[bool]
- func NewBooleanResult(result bool) Result[bool]
- func NewEmptyResult[T any]() Result[T]
- func NewErrorResult[T any](err error, args ...any) Result[T]
- func NewErrorResultFrom[T any, S any](another Result[S]) Result[T]
- func NewParsingResult(pr PARSING_RESULT) Result[PARSING_RESULT]
- func NewRejectedParsingResult(err error) Result[PARSING_RESULT]
- func NewResult[T any](value T) Result[T]
- func NewResultWithError[T any](value T, err error, args ...any) Result[T]
- func NewVoidErrorResult(err error) Result[bool]
- func NewVoidResult() Result[bool]
- type SIMPLE_TOKEN
- type SimpleReader
- type SimpleScanner
- type SimpleToken
- func (t *SimpleToken) IsCustom() bool
- func (t *SimpleToken) IsEOF() bool
- func (t *SimpleToken) IsIdentifier() bool
- func (t *SimpleToken) IsNumber() bool
- func (t *SimpleToken) IsSpecial(ch ...rune) bool
- func (t *SimpleToken) IsString() bool
- func (t *SimpleToken) SetChar(token SIMPLE_TOKEN, ch rune) *SimpleToken
- func (t *SimpleToken) SetString(token SIMPLE_TOKEN, literal string) *SimpleToken
- type StatusResult
- func (r *StatusResult[S, T]) Any() any
- func (r *StatusResult[S, T]) Error() error
- func (r *StatusResult[S, T]) Failed() bool
- func (r *StatusResult[S, T]) RecoverWithValue(status S, value T)
- func (r *StatusResult[S, T]) Status() S
- func (r *StatusResult[S, T]) Success() bool
- func (r *StatusResult[S, T]) Value() T
- type StringArray
- func (l *StringArray) Add(s string) *StringArray
- func (l *StringArray) AddIfNotEmptyf(s string, args ...any) *StringArray
- func (l *StringArray) AddIfTrue(c bool, s string, args ...any) *StringArray
- func (l *StringArray) Addf(s string, args ...any) *StringArray
- func (l *StringArray) Clear()
- func (l *StringArray) Join(sep string) string
- func (l *StringArray) Replace(old string, new string) *StringArray
- func (l *StringArray) ToArray() []string
- type StringBuilder
- type Switch
- func (o *Switch) Accept(value string) Result[PARSING_RESULT]
- func (o *Switch) ID() int
- func (o *Switch) Init()
- func (o *Switch) Names() []string
- func (o *Switch) SetDefault(defaultValue bool) *Switch
- func (o *Switch) SetUsage(usage string) *Switch
- func (o *Switch) SetValueRef(valueRef *bool) *Switch
- func (o *Switch) UpdateValueRef()
- func (o *Switch) Usage() string
- func (o *Switch) UsageDetails(ap *ArgParser, arr *StringArray)
- func (o *Switch) Value() bool
- type TextOption
- func (o *TextOption) Accept(value string) Result[PARSING_RESULT]
- func (o *TextOption) ID() int
- func (o *TextOption) Init()
- func (o *TextOption) Names() []string
- func (o *TextOption) SetDefaultValue(value string) *TextOption
- func (o *TextOption) SetUsage(usage string) *TextOption
- func (o *TextOption) SetValueName(valueName string) *TextOption
- func (o *TextOption) SetValueRef(valueRef *string) *TextOption
- func (o *TextOption) UpdateValueRef()
- func (o *TextOption) Usage() string
- func (o *TextOption) UsageDetails(ap *ArgParser, arr *StringArray)
- func (o *TextOption) Value() string
- type TextOptionList
- func (o *TextOptionList) Accept(value string) Result[PARSING_RESULT]
- func (o *TextOptionList) ID() int
- func (o *TextOptionList) Init()
- func (o *TextOptionList) Names() []string
- func (o *TextOptionList) SetCount(count int) *TextOptionList
- func (o *TextOptionList) SetCountRange(min, max int) *TextOptionList
- func (o *TextOptionList) SetUsage(usage string) *TextOptionList
- func (o *TextOptionList) SetValueName(valueName string) *TextOptionList
- func (o *TextOptionList) SetValueRef(valueRef *[]string) *TextOptionList
- func (o *TextOptionList) UpdateValueRef()
- func (o *TextOptionList) Usage() string
- func (o *TextOptionList) UsageDetails(ap *ArgParser, arr *StringArray)
- func (o *TextOptionList) Values() []string
- type UnpackFunc
- type VALUE_TYPE
Constants ¶
const ( ID_TIME_BYTES = 8 ID_SERIAL_BYTES = 8 )
const (
DEFAULT_NANOID_LENGTH = 12
)
const Nil = ID("")
Variables ¶
var ( ErrParseMissingValue = errors.New("missing argument value") ErrParseUnexpectedArgument = errors.New("unexpected argument: %s") ErrParseDuplicatedOptionID = errors.New("the option with the same id '%d' already exists") ErrParseDuplicatedOptionName = errors.New("the option with the same name '%s' already exists") ErrParseMissingOptionNameForID = errors.New("missing the name for option '%d'") ErrParseDuplicatedSubparserID = errors.New("the subparser with the same id '%d' already exists") ErrParseDuplicatedParserName = errors.New("the subparser with the same name '%s' already exists") ErrParseMissingParserNameForID = errors.New("missing the name for sub-parser '%d'") ErrParseArgumentNotFound = errors.New("argument '%s' not found") ErrParseNoArgumentAcceptValue = errors.New("no argument accepts the value '%s'") ErrParsingInvalidSwitchValue = errors.New("invalid switch value '%s'") ErrParsingOptionSetValueAgain = errors.New("cannot set value of option again: '%s'") ErrParsingNumericOptionValueOutOfRange = errors.New("numeric option value out of range: '%d'") ErrParsingNumericOptionInvalidInteger = errors.New("malformed numeric option value '%s'") ErrParsingListExceedMaximumCount = errors.New("list option exceeds max count: %d") )
argument parser errors
var ( ErrDataKeyNotExist = errors.New("data with key '%s' not exists") ErrDataIndexNotExist = errors.New("data with index %d not exists") ErrDataTypeMismatch = errors.New("data type mismatch, expect %s, got %T") )
var ( ErrNilArgument = errors.New("the argument is nil: %s") ErrEmptyArgument = errors.New("the argument is empty: %s") ErrInvalidArgument = errors.New("the argument is invalid") ErrNoEnoughArgument = errors.New("no enough argument") ErrNilPointer = errors.New("nil pointer is used") ErrInvalidCasting = errors.New("invalid type casting") )
Predefined common error messages
var DefaultDateTime time.Time = time.Time{}
var (
ErrConvertFail = errors.New("convert failed")
)
Errors that indicates the convertion fail.
Functions ¶
func AddOption ¶
AddOption adds an option into the parser with the generic type. Panic if the option could not be added.
func AddPositional ¶
AddPositional adds a positional argument into the parser with the generic type. Panic if the positional could not be added.
func CheckResult ¶
CheckResult is a helper function to check the result and return the error.
func Clamp ¶
func Clamp[T constraints.Ordered](value, min, max T) T
help functions Clamp 函数接受一个数值类型,并检查输入值是否在指定范围内。 如果输入值超出范围,则使用边界值。
func ConvertAssign ¶
ConvertAssign converts the src type to dst, and store into the dst.
func DaysBetweenTimes ¶
func EqualsCase ¶
EqualsCase returns true if two strings are same.
func ExpandUsersAndVars ¶
func ExpandVars ¶
func FindOption ¶
FindOption returns the option by id with the generic type. Panics if the option does not exist.
func FindPositional ¶
FindPositional returns the positional by id with the generic type. Panics if the positional does not exist.
func FormatDate ¶
func FormatDateTime ¶
func HasOneOfPrefixesCase ¶
HasOneOfPrefixesCase returns true if the string has the one of the give prefixes.
func HasPrefixCase ¶
HasPrefixCase returns true if the string has the given prefix.
func If ¶
check if the any is not nil, nil means both nil and empty, and the interface is pointed to a nil value.
func IfElse ¶
check if the any is not nil, nil means both nil and empty, and the interface is pointed to a nil value. this is a quick way to simulate the ?: operator in C/C++/Java.
func IndexLastNotAny ¶
func IndexNotAny ¶
func IsEmptyString ¶
func IsNil ¶
check if the any is nil, nil means both nil and empty, and the interface is pointed to a nil value. if the any is not nil, it will check the value of the any, if the value is nil, it will return true, otherwise false.
func LeftJustify ¶
func LoadEnvAsList ¶
func LoadEnvAsList() []string
func LoadEnvAsMap ¶
func LoadEnvBool ¶
func LoadEnvFile ¶
env file is typeically a file with name .env, it contains key-value pairs like:
KEY1=VALUE1 KEY2=VALUE2
the key/value pairs will be merged into the current environment. LoadEnvFile loads the env file with the given filename into the current environment.
func LoadEnvFloat ¶
func LoadEnvInt ¶
func LoadEnvString ¶
func PackDateTime ¶
func PanicResult ¶
func PanicResult(result *error)
PanicResult is a helper function to panic the error if the result is not nil.
func RandomChars ¶
func ReorderTimes ¶
func RightJustify ¶
func SetEnvBool ¶
func SetEnvFloat ¶
func SetEnvString ¶
func UnpackBool ¶
func UnpackFloat64 ¶
func UnpackList ¶
func UnpackList[T any, V any](list DataList, unpacker UnpackFunc[T, V], result *error) []T
UnpackList function unpacks the DataList to a []T. The function uses the unpack function to convert the value to the target type.
func UnpackMap ¶
UnpackMap function unpacks the DataBag to a map[string]T. The function uses the unpack function to convert the value to the target type.
func UnpackString ¶
Types ¶
type Arg ¶
type Arg interface { ID() int // returns the ID of the argument Names() []string // returns the names of the argument Usage() string // returns the usage of the argument Init() // initialize the argument to reset all default values UpdateValueRef() // update the value reference if the value reference is set Accept(value string) Result[PARSING_RESULT] // parse and accept the value of the argument UsageDetails(ap *ArgParser, list *StringArray) // format the usage details and store them into the string array }
Arg is the interface for all arguments (include Switches, Options, and Positionals).
type ArgParser ¶
type ArgParser struct {
// contains filtered or unexported fields
}
ArgParser defines the argument parser.
func NewArgParser ¶
NewArgParser creates a new argument parser with the given ID and names.
func (*ArgParser) ActiveSubparser ¶
ActiveSubparser returns the active subparser. Returns nil if no subparser is active (selected).
func (*ArgParser) AddOption ¶
AddOption adds an option into the parser. Any duplicated ID or name will cause an error.
func (*ArgParser) AddPositional ¶
AddPositional adds a positional argument into the parser. Any duplicated ID will cause an error.
func (*ArgParser) AddSubparser ¶
AddSubparser add a sub-parser into the parser. The sub-parser should have a unique id and at least one name.
func (*ArgParser) FindOption ¶
FindOption returns the option by id. Returns nil if the option does not exist.
func (*ArgParser) FindPositional ¶
FindPositional returns the positional by id. Returns nil if the positional does not exist.
func (*ArgParser) HasPassthru ¶
HasPassthru returns true if the passthru arguments are available.
func (*ArgParser) Names ¶
Names returns the parser names. Note, the names could be empty for the root parser.
func (*ArgParser) ParseWithArgs ¶
ParseWithArgs parse the given arguments.
func (*ArgParser) Passthru ¶
Passthru returns the passthru arguments. If no passthru arguments are available, returns an empty slice.
type DataBag ¶
DataBag is a wrapper of map[string]any, and the elements in the map are converted to appropriate types.
func PackMap ¶
PackMap function creates a DataBag from a map[string]any. The function recognized the interface IPackable, so the value that implements IPackable will be packed by calling the Pack method.
func (DataBag) BoolWithDefault ¶
func (DataBag) DateTimeWithDefault ¶
func (DataBag) Float64WithDefault ¶
func (DataBag) StringWithDefault ¶
type DataList ¶
type DataList []any
DataList is a wrapper of []any, and the elements in the list are converted to appropriate types.
func PackList ¶
PackList function creates a DataList from a []any. The function recognized the interface IPackable, so the value that implements IPackable will be packed by calling the Pack method.
func (DataList) BoolWithDefault ¶
func (DataList) DateTimeWithDefault ¶
func (DataList) Float64WithDefault ¶
func (DataList) IntWithDefault ¶
type Event ¶
type Event struct {
// contains filtered or unexported fields
}
func NewEventWithHandler ¶
func NewEventWithHandler(name string, handler IEventHandler) *Event
func (*Event) AddHandler ¶
func (event *Event) AddHandler(handler IEventHandler)
type IClosable ¶
type IClosable interface {
Close() error
}
IClosable defines the contract that the object is closable.
type IConvertible ¶
IConvertible interface allow the type to export the convert function to convert between the types.
type ID ¶
type ID string
general unique ID
func NewFlakeID ¶
func NewFlakeIDWithPrefix ¶
func NewKSUIDWithPrefix ¶
func NewMathIDWithPrefix ¶
func NewObjectID ¶
func NewObjectIDWithPrefix ¶
func NewSnowflakeID ¶
func NewULID ¶
NewULID generates a new ULID. ULID is a unique identifier that is used in distributed systems. The ULID is a 26-character string that consists of the following parts: 1. A 10-character timestamp value, representing the number of milliseconds since the Unix epoch. 2. A 16-character random value. The ULID is generated using the current time and a random value. Example: 01JAEZW7KJ0H1V8MF7K043YHAV
func NewULIDWithPrefix ¶
func NewUUIDWithPrefix ¶
type IDataParcel ¶
IDataParcel is the interface for the data parcel, which provides the methods to get the data by key or index, and the length of the data.
type IEventHandler ¶
type IEventSender ¶
type IEventSender interface { AddEventHandler(handler IEventHandler) Fire(eventName string, arg any) }
func NewEventSender ¶
func NewEventSender(supportedEventNames ...string) IEventSender
type IPackable ¶
type IPackable interface {
Pack() DataBag // Pack packs itself into DataBag.
}
IPackable defines the interface which supports packing itself into DataBag.
type IStatusResult ¶
type NumericOption ¶
type NumericOption struct {
// contains filtered or unexported fields
}
NumericOption defines a argument
func NewNumericOption ¶
func NewNumericOption(id int, names ...string) *NumericOption
NewNumericOption creates a new numeric option with the given ID and names.
func (*NumericOption) Accept ¶
func (o *NumericOption) Accept(value string) Result[PARSING_RESULT]
Accept parses and accepts the value of the option.
func (*NumericOption) Init ¶
func (o *NumericOption) Init()
Init initializes the option to reset the default value.
func (*NumericOption) SetDefaultValue ¶
func (o *NumericOption) SetDefaultValue(value int) *NumericOption
SetDefaultValue sets the default value of the option.
func (*NumericOption) SetUsage ¶
func (o *NumericOption) SetUsage(usage string) *NumericOption
SetUsage sets the usage of the option.
func (*NumericOption) SetValueName ¶
func (o *NumericOption) SetValueName(valueName string) *NumericOption
SetValueName sets the name of the value.
func (*NumericOption) SetValueRange ¶
func (o *NumericOption) SetValueRange(min, max int) *NumericOption
SetValueRange sets the range of the value.
func (*NumericOption) SetValueRef ¶
func (o *NumericOption) SetValueRef(valueRef *int) *NumericOption
SetValueRef sets the reference to the value of the option. The value referenced by valueRef will be set automatically
func (*NumericOption) UpdateValueRef ¶
func (o *NumericOption) UpdateValueRef()
UpdateValueRef updates the value reference if the value reference is set.
func (*NumericOption) UsageDetails ¶
func (o *NumericOption) UsageDetails(ap *ArgParser, arr *StringArray)
UsageDetails formats the usage details and stores them into the passed in string array.
func (*NumericOption) Value ¶
func (o *NumericOption) Value() int
Value returns the value of the option.
type NumericOptionList ¶
type NumericOptionList struct {
// contains filtered or unexported fields
}
NumericOptionList defines a list of numeric options
func NewNumericOptionList ¶
func NewNumericOptionList(id int, names ...string) *NumericOptionList
NewNumericOptionList creates a new numeric option list with the given ID and names.
func (*NumericOptionList) Accept ¶
func (o *NumericOptionList) Accept(value string) Result[PARSING_RESULT]
Accept parses and accepts the value of the option list.
func (*NumericOptionList) Init ¶
func (o *NumericOptionList) Init()
Init initializes the option list to reset the default values.
func (*NumericOptionList) SetCount ¶
func (o *NumericOptionList) SetCount(count int) *NumericOptionList
SetValueRef sets the reference to the values of the options. The values referenced by valueRef will be set automatically
func (*NumericOptionList) SetCountRange ¶
func (o *NumericOptionList) SetCountRange(min, max int) *NumericOptionList
SetCountRange sets the range of the count of the values.
func (*NumericOptionList) SetUsage ¶
func (o *NumericOptionList) SetUsage(usage string) *NumericOptionList
SetUsage sets the usage of the option list.
func (*NumericOptionList) SetValueName ¶
func (o *NumericOptionList) SetValueName(valueName string) *NumericOptionList
SetValueName sets the name of the value.
func (*NumericOptionList) SetValueRange ¶
func (o *NumericOptionList) SetValueRange(min, max int) *NumericOptionList
SetValueRange sets the range of the value.
func (*NumericOptionList) SetValueRef ¶
func (o *NumericOptionList) SetValueRef(valueRef *[]int) *NumericOptionList
SetValueRef sets the reference to the values of the options. The values referenced by valueRef will be set automatically
func (*NumericOptionList) UpdateValueRef ¶
func (o *NumericOptionList) UpdateValueRef()
Accept parses and accepts the value of the option list.
func (*NumericOptionList) UsageDetails ¶
func (o *NumericOptionList) UsageDetails(ap *ArgParser, arr *StringArray)
UsageDetails formats the usage details and stores them into the passed in string array.
func (*NumericOptionList) Values ¶
func (o *NumericOptionList) Values() []int
Values returns the values of the options.
type Optional ¶
type Optional[T any] struct { // contains filtered or unexported fields }
Optional defines a triple states trigger.
func NewOptional ¶
NewOptional creates a new triple states trigger.
func (*Optional[T]) Nil ¶
Nil returns true if the triple states has no value, or in the third state.
func (*Optional[T]) NotNil ¶
NotNil returns true if the triple states has value, either true or false.
func (*Optional[T]) SetNil ¶
func (t *Optional[T]) SetNil()
SetNil sets the triple states to the third state, no value state.
type PARSING_RESULT ¶
type PARSING_RESULT int
PARSING_RESULT defines the parsing result during the parse process.
const ( PR_CONTINUE PARSING_RESULT = iota // the argument is parsed successfully, move to next. PR_EXPECT_VALUE // the argument expects more values. PR_EXPECT_VALUE_OPTIONAL // the argument expects additional optional values. )
type Pool ¶
type Pool[KeyT comparable, ValueT any] struct { // contains filtered or unexported fields }
Pool is a thread-safe map that creates a new value for the pool if the key does not exist.
func NewPool ¶
func NewPool[KeyT comparable, ValueT any](factory PoolFactory[KeyT, ValueT]) *Pool[KeyT, ValueT]
type PoolFactory ¶
type PoolFactory[KeyT comparable, ValueT any] func(key KeyT) ValueT
PoolFactory is a function that creates a new value for the pool.
type Positional ¶
type Positional struct {
// contains filtered or unexported fields
}
Positional represents a positional argument like: <file>
func NewPositional ¶
func NewPositional(id int) *Positional
NewPositional creates a new positional argument with the given ID.
func (*Positional) Accept ¶
func (o *Positional) Accept(value string) Result[PARSING_RESULT]
Accept parses and accepts the value of the positional.
func (*Positional) Init ¶
func (o *Positional) Init()
Init initializes the positional to reset the default value.
func (*Positional) SetUsage ¶
func (o *Positional) SetUsage(usage string) *Positional
SetUsage sets the usage of the positional.
func (*Positional) SetValueName ¶
func (o *Positional) SetValueName(valueName string) *Positional
SetValueName sets the name of the value.
func (*Positional) SetValueRef ¶
func (o *Positional) SetValueRef(valueRef *string) *Positional
SetValueRef sets the reference to the value of the positional. The value referenced by valueRef will be set automatically
func (*Positional) UpdateValueRef ¶
func (o *Positional) UpdateValueRef()
UpdateValueRef updates the value reference if the value reference is set.
func (*Positional) UsageDetails ¶
func (o *Positional) UsageDetails(_ *ArgParser, arr *StringArray)
UsageDetails formats the usage details and stores them into the passed in string array.
func (*Positional) Value ¶
func (o *Positional) Value() string
Value returns the value of the positional.
type PositionalList ¶
type PositionalList struct {
// contains filtered or unexported fields
}
PositionalList represents a list of positional arguments like: <file> <file> ...
func NewPositionalList ¶
func NewPositionalList(id int) *PositionalList
NewPositionalList creates a new positional list with the given ID.
func (*PositionalList) Accept ¶
func (o *PositionalList) Accept(value string) Result[PARSING_RESULT]
Accept parses and accepts the value of the positional list.
func (*PositionalList) Init ¶
func (o *PositionalList) Init()
Init initializes the positional list to reset the default values.
func (*PositionalList) SetCount ¶
func (o *PositionalList) SetCount(count int) *PositionalList
SetCount sets the count of the values.
func (*PositionalList) SetCountRange ¶
func (o *PositionalList) SetCountRange(min, max int) *PositionalList
SetCountRange sets the range of the count of the values.
func (*PositionalList) SetUsage ¶
func (o *PositionalList) SetUsage(usage string) *PositionalList
SetUsage sets the usage of the positional list.
func (*PositionalList) SetValueName ¶
func (o *PositionalList) SetValueName(valueName string) *PositionalList
SetValueName sets the name of the value.
func (*PositionalList) SetValueRef ¶
func (o *PositionalList) SetValueRef(valueRef *[]string) *PositionalList
SetValueRef sets the reference to the values of the positionals. The values referenced by valueRef will be set automatically
func (*PositionalList) UpdateValueRef ¶
func (o *PositionalList) UpdateValueRef()
UpdateValueRef updates the value reference if the value reference is set.
func (*PositionalList) UsageDetails ¶
func (o *PositionalList) UsageDetails(_ *ArgParser, arr *StringArray)
UsageDetails formats the usage details and stores them into the passed in string array.
func (*PositionalList) Values ¶
func (o *PositionalList) Values() []string
Values returns the values of the positionals.
type Result ¶
type Result[T any] struct { // contains filtered or unexported fields }
func NewBooleanErrorResult ¶
func NewBooleanResult ¶
func NewEmptyResult ¶
func NewParsingResult ¶
func NewParsingResult(pr PARSING_RESULT) Result[PARSING_RESULT]
func NewRejectedParsingResult ¶
func NewRejectedParsingResult(err error) Result[PARSING_RESULT]
func NewVoidErrorResult ¶
func NewVoidResult ¶
func (*Result[T]) RecoverWithValue ¶
func (r *Result[T]) RecoverWithValue(value T)
type SIMPLE_TOKEN ¶
type SIMPLE_TOKEN int
const ( STOK_UNDEFINED SIMPLE_TOKEN = iota STOK_EOF // end of file/end of token stream STOK_IDENTIFIER // identifier STOK_NUMBER // integer number constant STOK_STRING // string constant STOK_SPECIAL // special character STOK_CUSTOM // custom token )
type SimpleReader ¶
type SimpleReader struct {
// contains filtered or unexported fields
}
func NewSimpleReader ¶
func NewSimpleReader(s string) *SimpleReader
func (*SimpleReader) Current ¶
func (r *SimpleReader) Current() rune
func (*SimpleReader) Next ¶
func (r *SimpleReader) Next() bool
func (*SimpleReader) Putback ¶
func (r *SimpleReader) Putback() bool
type SimpleScanner ¶
type SimpleScanner struct {
// contains filtered or unexported fields
}
func NewSimpleScanner ¶
func NewSimpleScanner(s string) *SimpleScanner
func (*SimpleScanner) NextToken ¶
func (s *SimpleScanner) NextToken() *SimpleToken
func (*SimpleScanner) PeekChar ¶
func (s *SimpleScanner) PeekChar() rune
func (*SimpleScanner) ReadChar ¶
func (s *SimpleScanner) ReadChar() rune
func (*SimpleScanner) ReadUntil ¶
func (s *SimpleScanner) ReadUntil(chars ...rune) *SimpleToken
type SimpleToken ¶
type SimpleToken struct { Token SIMPLE_TOKEN Literal string }
func (*SimpleToken) IsCustom ¶
func (t *SimpleToken) IsCustom() bool
func (*SimpleToken) IsEOF ¶
func (t *SimpleToken) IsEOF() bool
func (*SimpleToken) IsIdentifier ¶
func (t *SimpleToken) IsIdentifier() bool
func (*SimpleToken) IsNumber ¶
func (t *SimpleToken) IsNumber() bool
func (*SimpleToken) IsSpecial ¶
func (t *SimpleToken) IsSpecial(ch ...rune) bool
func (*SimpleToken) IsString ¶
func (t *SimpleToken) IsString() bool
func (*SimpleToken) SetChar ¶
func (t *SimpleToken) SetChar(token SIMPLE_TOKEN, ch rune) *SimpleToken
func (*SimpleToken) SetString ¶
func (t *SimpleToken) SetString(token SIMPLE_TOKEN, literal string) *SimpleToken
type StatusResult ¶
func NewErrorStatusResult ¶
func NewErrorStatusResult[S int, T any](err error) StatusResult[S, T]
func NewStatusResult ¶
func NewStatusResult[S int, T any](status S, value T) StatusResult[S, T]
func (*StatusResult[S, T]) Any ¶
func (r *StatusResult[S, T]) Any() any
func (*StatusResult[S, T]) Error ¶
func (r *StatusResult[S, T]) Error() error
func (*StatusResult[S, T]) Failed ¶
func (r *StatusResult[S, T]) Failed() bool
func (*StatusResult[S, T]) RecoverWithValue ¶
func (r *StatusResult[S, T]) RecoverWithValue(status S, value T)
func (*StatusResult[S, T]) Success ¶
func (r *StatusResult[S, T]) Success() bool
type StringArray ¶
type StringArray struct {
// contains filtered or unexported fields
}
func NewStringArray ¶
func NewStringArray() *StringArray
func (*StringArray) Add ¶
func (l *StringArray) Add(s string) *StringArray
func (*StringArray) AddIfNotEmptyf ¶
func (l *StringArray) AddIfNotEmptyf(s string, args ...any) *StringArray
func (*StringArray) AddIfTrue ¶
func (l *StringArray) AddIfTrue(c bool, s string, args ...any) *StringArray
func (*StringArray) Addf ¶
func (l *StringArray) Addf(s string, args ...any) *StringArray
func (*StringArray) Clear ¶
func (l *StringArray) Clear()
func (*StringArray) Join ¶
func (l *StringArray) Join(sep string) string
func (*StringArray) Replace ¶
func (l *StringArray) Replace(old string, new string) *StringArray
func (*StringArray) ToArray ¶
func (l *StringArray) ToArray() []string
type StringBuilder ¶
type StringBuilder struct {
// contains filtered or unexported fields
}
StringBuilder is a string builder which is enhanced from strings.Builder.
func NewStringBuilder ¶
func NewStringBuilder() *StringBuilder
func NewStringBuilderWith ¶
func NewStringBuilderWith(s string) *StringBuilder
func (*StringBuilder) String ¶
func (builder *StringBuilder) String() string
func (*StringBuilder) Write ¶
func (builder *StringBuilder) Write(s string, args ...any)
func (*StringBuilder) Writeln ¶
func (builder *StringBuilder) Writeln(args ...string)
type Switch ¶
type Switch struct {
// contains filtered or unexported fields
}
Switch represents a switch argument like: --verbose, -v
func (*Switch) Accept ¶
func (o *Switch) Accept(value string) Result[PARSING_RESULT]
Accept parses and accepts the value of the switch.
func (*Switch) Init ¶
func (o *Switch) Init()
Init initializes the switch to reset the default value.
func (*Switch) SetDefault ¶
SetDefault sets the default value of the switch.
func (*Switch) SetValueRef ¶
SetValueRef sets the reference to the value of the switch. The value referenced by valueRef will be set automatically when the switch is parsed successfully.
func (*Switch) UpdateValueRef ¶
func (o *Switch) UpdateValueRef()
UpdateValueRef updates the value reference if the value reference is set.
func (*Switch) UsageDetails ¶
func (o *Switch) UsageDetails(ap *ArgParser, arr *StringArray)
UsageDetails formats the usage details and stores them into the passed in string array.
type TextOption ¶
type TextOption struct {
// contains filtered or unexported fields
}
TextOption defines a text argument
func NewTextOption ¶
func NewTextOption(id int, names ...string) *TextOption
NewTextOption creates a new text option with the given ID and names.
func (*TextOption) Accept ¶
func (o *TextOption) Accept(value string) Result[PARSING_RESULT]
Accept parses and accepts the value of the option.
func (*TextOption) Init ¶
func (o *TextOption) Init()
Init initializes the option to reset the default value.
func (*TextOption) SetDefaultValue ¶
func (o *TextOption) SetDefaultValue(value string) *TextOption
SetDefaultValue sets the default value of the option.
func (*TextOption) SetUsage ¶
func (o *TextOption) SetUsage(usage string) *TextOption
SetUsage sets the usage of the option.
func (*TextOption) SetValueName ¶
func (o *TextOption) SetValueName(valueName string) *TextOption
SetValueName sets the name of the value.
func (*TextOption) SetValueRef ¶
func (o *TextOption) SetValueRef(valueRef *string) *TextOption
SetValueRef sets the reference to the value of the option. The value referenced by valueRef will be set automatically
func (*TextOption) UpdateValueRef ¶
func (o *TextOption) UpdateValueRef()
UpdateValueRef updates the value reference if the value reference is set.
func (*TextOption) UsageDetails ¶
func (o *TextOption) UsageDetails(ap *ArgParser, arr *StringArray)
UsageDetails formats the usage details and stores them into the passed in string array.
func (*TextOption) Value ¶
func (o *TextOption) Value() string
Value returns the value of the option.
type TextOptionList ¶
type TextOptionList struct {
// contains filtered or unexported fields
}
TextOptionList defines a list of text options
func NewTextOptionList ¶
func NewTextOptionList(id int, names ...string) *TextOptionList
NewTextOptionList creates a new text option list with the given ID and names.
func (*TextOptionList) Accept ¶
func (o *TextOptionList) Accept(value string) Result[PARSING_RESULT]
Accept parses and accepts the value of the option list.
func (*TextOptionList) Init ¶
func (o *TextOptionList) Init()
Init initializes the option list to reset the default values.
func (*TextOptionList) SetCount ¶
func (o *TextOptionList) SetCount(count int) *TextOptionList
SetCount sets the count of the values.
func (*TextOptionList) SetCountRange ¶
func (o *TextOptionList) SetCountRange(min, max int) *TextOptionList
SetCountRange sets the range of the count of the values.
func (*TextOptionList) SetUsage ¶
func (o *TextOptionList) SetUsage(usage string) *TextOptionList
SetUsage sets the usage of the option list.
func (*TextOptionList) SetValueName ¶
func (o *TextOptionList) SetValueName(valueName string) *TextOptionList
SetValueName sets the name of the value.
func (*TextOptionList) SetValueRef ¶
func (o *TextOptionList) SetValueRef(valueRef *[]string) *TextOptionList
SetValueRef sets the reference to the values of the options. The values referenced by valueRef will be set automatically
func (*TextOptionList) UpdateValueRef ¶
func (o *TextOptionList) UpdateValueRef()
UpdateValueRef updates the value reference if the value reference is set.
func (*TextOptionList) UsageDetails ¶
func (o *TextOptionList) UsageDetails(ap *ArgParser, arr *StringArray)
UsageDetails formats the usage details and stores them into the passed in string array.
func (*TextOptionList) Values ¶
func (o *TextOptionList) Values() []string
Values returns the values of the options.
type UnpackFunc ¶
UnpackFunc is the function type for unpacking the value to the target type. Where
- T: the target type the function unpacks to;
- V: the value type the function unpacks from.
type VALUE_TYPE ¶
type VALUE_TYPE int
VALUE_TYPE defines the type of the value which could be converted and handled in the DataBag or DataList. Note, multiple VALUE_TYPE can be combined together. The special type VT_OPTIONAL is used to indicate that the value is optional, and thus it cannot be used alone.
const ( VT_UNKNOWN VALUE_TYPE = 0 VT_BOOLEAN VALUE_TYPE = 0x00000001 VT_INTEGER VALUE_TYPE = 0x00000002 VT_FLOAT64 VALUE_TYPE = 0x00000004 VT_FLOAT32 VALUE_TYPE = 0x00000008 VT_STRING VALUE_TYPE = 0x00000010 VT_DATETIME VALUE_TYPE = 0x00000020 VT_LIST VALUE_TYPE = 0x01000000 VT_BAG VALUE_TYPE = 0x02000000 VT_OPTIONAL VALUE_TYPE = 0x80000000 // special type to indicate that the value is optional )
func GetType ¶
func GetType(value any) VALUE_TYPE
func (VALUE_TYPE) IsOptional ¶
func (t VALUE_TYPE) IsOptional() bool
func (VALUE_TYPE) String ¶
func (t VALUE_TYPE) String() string