parser

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
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 ErrorCode

func ErrorCode(code int, err error) error

ErrorCode create new error with an error `code`

func ErrorCodeMsg

func ErrorCodeMsg(code int, msg string, args ...any) error

ErrorCodeMsg create new error with an error `code` and message `msg`

func ErrorCodef

func ErrorCodef(code int, err error, msg string, args ...any) error

ErrorCodef create new error with an error `code` and message `msg`

func Exit2ArgsOnError

func Exit2ArgsOnError[E1, E2 any](e1 E1, e2 E2, err error) (E1, E2)

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

func ExitOnError[E any](e E, err error) E

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

func GetErrorCode(err error) int

GetErrorCode return error code from the `err` if the `err` is `parseError` instance otherwise it's always return 0.

func GetFile

func GetFile[E ~string | ~[]byte | *os.File](txt string) (E, error)

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 ParseArgs

func ParseArgs(option Option) ([]string, error)

ParseArgs parse os.Args

func PrintError

func PrintError(err error, exit bool)

PrintError print an error to stderr

func ToBool

func ToBool[B ~bool](txt string) (B, error)

ToBool convert an input `txt` string to it boolean type

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

func ToSlice[E constraints.Primitive](s, sep string, fn func(string) (E, error)) ([]E, error)

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

func ToSliceByte(txt string, opt Option) ([]byte, error)

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 ToString

func ToString[S ~string](txt string) (S, error)

ToString convert an input `txt` string to it alias type of parameter type `S`

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

type ToTypeWithOption

type ToTypeWithOption[E any] func(string, Option) (E, error)

ToTypeWithOption a function that help parsing string into actual type with a given option. The `Option` is a copy of option given to `ParserArgs`

Jump to

Keyboard shortcuts

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