reflag

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2020 License: MIT Imports: 7 Imported by: 0

README

Description

Package reflag provides reflect based helpers for flagex.

Documentation

Overview

Package reflag provides reflect based helpers for flagex.

Index

Constants

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

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

func FromStruct(v interface{}) (*flagex.Flags, error)

FromStruct creates Flags from struct v and returns it or an error. See Struct for more details.

func Struct

func Struct(v interface{}, args []string) (*flagex.Flags, error)

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.

func ToStruct

func ToStruct(v interface{}, flags *flagex.Flags) error

ToStruct applies Flags (prefferably gotten with FromStruct) to a struct (preferrably one used with FromStruct). Flags should be Parse()'d with no error. See Struct for more details.

Types

This section is empty.

Jump to

Keyboard shortcuts

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