Documentation ¶
Overview ¶
Package reflag provides reflect based helpers for flagex.
Index ¶
Constants ¶
const ( // KeyJSON identifies a JSON tag in a struct field. KeyJSON = "json" // KeyReflag identifies a reflag tag in a struct field. KeyReflag = "reflag" // KeyKey identifies a key for a flag in a struct field. KeyKey = "key" // KeyShort identifies a shortkey for a flag in a struct field. KeyShort = "short" // KeyHelp identifies help for a flag in a struct field. KeyHelp = "help" // KeyParamHelp identifies parameter help for a flag in a struct field. KeyParamHelp = "paramhelp" )
Variables ¶
var ( // ErrReflag is the base error from reflag package. ErrReflag = errorex.New("reflag") // ErrConvert is returned when an arg was not convertable to a value. ErrConvert = ErrReflag.WrapFormat("error converting arg '%s' value '%s' to '%s'") // ErrUnmarshal is returned when an arg was not unmarshalable to a value. ErrUnmarshal = ErrReflag.WrapFormat("error unmarshaling arg '%s' value '%s' to '%s'") // ErrNotFound is returned when applying to to a struct with a field not defined in flags. ErrNotFound = ErrReflag.WrapFormat("no flags defined for field '%s'") // ErrParam is returned when an invalid parameter is encountered. ErrParam = ErrReflag.Wrap("invalid parameter") )
Functions ¶
func FromStruct ¶
FromStruct creates Flags from struct v and returns it or an error. See Struct for more details.
func Struct ¶
Struct takes a struct v, creates flags from v's exported fields, parses flags from args, sets v fields values to parsed values then returns flags or an error if one occured.
Struct v can have nested structs but any pointer types inside at any level must have been allocated prior to this call. It makes sense to allocate v fully, pass it to Struct() to have any field values changed by parsing args then to check v's values. To check if a field was changed use flags's Parsed() function.
Struct prefers json tags from v's Fields to specify defined flag names. If none, field names are lowercased and defined in flags as such. First letter of derived key is used for shortkey, until uniques are exhausted. Shortkey definitions can differ in Struct calls if there would be duplicates due to key names because of the random nature of go's map iteration. All unparsed fields are always ommitted when setting field values.
Syntax for specifying array, slice or map values is described by StringToInterface function from reflectex which this function uses to convert string values to v's field values.
Types ¶
This section is empty.