Documentation
¶
Index ¶
- Constants
- func AssignToVar[E SingleParsingValue](s string, ptrVar *any, fn ToType[E]) error
- func AssignToVarWithOption[E SingleParsingValue](s string, opt Option, ptrVar *any, fn ToTypeWithOption[E]) error
- func ErrorCode(code int, err error) error
- func ErrorCodeMsg(code int, msg string, args ...any) error
- func ErrorCodef(code int, err error, msg string, args ...any) error
- func Exit2ArgsOnError[E1, E2 any](e1 E1, e2 E2, err error) (E1, E2)
- func ExitOnError[E any](e E, err error) E
- func GetErrorCode(err error) int
- func GetFile[E ~string | ~[]byte | *os.File](txt string) (E, error)
- func NextArg[E SingleParsingValue](state *ParserState, valParser ToType[E]) (E, error)
- func NextCustomArg[E any](state *ParserState, valParser ToTypeWithOption[E]) (E, error)
- func NextMapArg[M map[K]V, K, V constraints.Primitive](state *ParserState, keyParser ToType[K], valParser ToType[V]) (M, error)
- func NextSliceArg[R []E, E constraints.Primitive](state *ParserState, valParser ToType[E]) (R, error)
- func ParseArgs(option Option) ([]string, error)
- func PrintError(err error, exit bool)
- func ToBool[B ~bool](txt string) (B, error)
- func ToFloat[N constraints.Float](txt string) (N, error)
- func ToMap[M ~map[K]V, K, V constraints.Primitive](opt *Option, kp ToType[K], vp ToType[V]) func(string) (M, error)
- func ToMapWithSeparator[M ~map[K]V, K, V constraints.Primitive](s, sliceSep, mapSep string, kp ToType[K], vp ToType[V]) (M, error)
- func ToSigned[N constraints.Signed](txt string) (N, error)
- func ToSlice[E constraints.Primitive](s, sep string, fn func(string) (E, error)) ([]E, error)
- func ToSliceByte(txt string, opt Option) ([]byte, error)
- func ToString[S ~string](txt string) (S, error)
- func ToUnsigned[N constraints.Unsigned](txt string) (N, error)
- type Option
- type ParserState
- type SingleParsingValue
- type ToType
- type ToTypeWithOption
Constants ¶
const UnknownErrorCode = 0xffffff
UnknownErrorCode reserve to be used when there is an error that isn't `parseError` and calling to `PrintError` required immedately exit.
Variables ¶
This section is empty.
Functions ¶
func AssignToVar ¶
func AssignToVar[E SingleParsingValue](s string, ptrVar *any, fn ToType[E]) error
AssignToVar is helper that assign `E` value to the `ptrVar` if no error during call to `fn` otherwise the error is returned
func AssignToVarWithOption ¶
func AssignToVarWithOption[E SingleParsingValue](s string, opt Option, ptrVar *any, fn ToTypeWithOption[E]) error
AssignToVarWithOption is helper that assign `E` value to the `ptrVar` if no error during call to `fn` otherwise the error is returned
func ErrorCodeMsg ¶
ErrorCodeMsg create new error with an error `code` and message `msg`
func ErrorCodef ¶
ErrorCodef create new error with an error `code` and message `msg`
func Exit2ArgsOnError ¶
Exit2ArgsOnError check if `err` is not nil, it will then call to `PrintError` with the argument `exit` set to true. If `err` is nil then it simply return `e1` and `e2`.
func ExitOnError ¶
ExitOnError check if `err` is not nil, it will then call to `PrintError` with the argument `exit` set to true. If `err` is nil then it simply return `e`.
func GetErrorCode ¶
GetErrorCode return error code from the `err` if the `err` is `parseError` instance otherwise it's always return 0.
func GetFile ¶
GetFile parsing the given `txt` into specific type parameter `E`. The `opt` provide more control for open option such read, write or append. e.g. [email protected] the `r` stand for read, `w` stand for write, `a` stand for append, `c` stand for create and `t` stand for truncate. Similar to Go open file option, it can specific 1 or more. e.g. [email protected] mean file is open for read and append only. This additional option (rwac) is only apply to the type parameter `*os.File` only.
func NextArg ¶
func NextArg[E SingleParsingValue](state *ParserState, valParser ToType[E]) (E, error)
NextArg return next argument in the command line.
func NextCustomArg ¶
func NextCustomArg[E any](state *ParserState, valParser ToTypeWithOption[E]) (E, error)
NextArgCustom return next argument in the command line.
func NextMapArg ¶
func NextMapArg[M map[K]V, K, V constraints.Primitive](state *ParserState, keyParser ToType[K], valParser ToType[V]) (M, error)
NextMapArg return next argument in the command line as a map value.
func NextSliceArg ¶
func NextSliceArg[R []E, E constraints.Primitive](state *ParserState, valParser ToType[E]) (R, error)
NextSliceArg return next argument in the command line as a slice value.
func ToFloat ¶
func ToFloat[N constraints.Float](txt string) (N, error)
ToUnsigned parse an input `txt` string to a floating-point `N`
func ToMap ¶
func ToMap[M ~map[K]V, K, V constraints.Primitive](opt *Option, kp ToType[K], vp ToType[V]) func(string) (M, error)
ToMap return a parser to parse the input `txt` string to a map type
func ToMapWithSeparator ¶
func ToMapWithSeparator[M ~map[K]V, K, V constraints.Primitive](s, sliceSep, mapSep string, kp ToType[K], vp ToType[V]) (M, error)
ToMapWithSeparator parse the input argument into map base on the give seperation
func ToSigned ¶
func ToSigned[N constraints.Signed](txt string) (N, error)
ToSigned parse an input `txt` string to a signed integer `N`
func ToSlice ¶
ToSlice convert the input text `s` base on the given `sep` separator into a slice of number, parsing by the given function `fn`
func ToSliceByte ¶
ToSliceByte convert string input (encoded or not encoded) into byte slice. The `txt` should contain leading encode algorithm for specific encoding text such as `base32`, `base64`, `hex`. e.g. base64:ZXhhbXBsZQ==.
func ToUnsigned ¶
func ToUnsigned[N constraints.Unsigned](txt string) (N, error)
ToUnsigned parse an input `txt` string to a unsigned integer `N`
Types ¶
type Option ¶
type Option struct {
// A value use to separate input argument into multiple slice
SliceSeparator string
// A value use to separate input argument into map or other value
// such as encoding like base64, hex ...etc, it use in
// combination with `Separator` when type is map
SecondarySeparator string
// OnFlag is when parser encouter argument that start with either `--` or `-`.
// It return an error code and the error during parsing otherwise a 0 and nil
// is returned.
OnFlag func(state *ParserState) error
// OnArgument is call when parser found that the argument segment
// is not a flag or flag value. If the function return false then
// the argument will append to remaining input argument. The unknown
// argument.
OnArgument func(name string, index int) bool
}
Option setting parameter during handling parsing arguments
type ParserState ¶
type ParserState struct {
// contains filtered or unexported fields
}
ParserState is a ParserState of the parser
func (*ParserState) GetFlagName ¶
func (s *ParserState) GetFlagName() string
GetFlagName implement ParserState
type SingleParsingValue ¶
type SingleParsingValue interface {
constraints.Primitive | ~[]byte | *os.File
}
SingleParsingValue a type parameter that include primitive type, slice of byte and an *os.File type.
type ToType ¶
type ToType[E SingleParsingValue] func(string) (E, error)
ToType a function that help parsing string into actual type