Documentation ¶
Overview ¶
Package psetter contains a collection of useful types that can be used to set parameter values of a program.
Each type satisfies the param.Setter interface. These can be used to supply the second argument of a PSet Add or AddByPos method - the action associated with the parameter. When the parameter is found while parsing the params the appropriate Set method will be called.
A typical Setter is used to set the value of a parameter to the program. For example below, a bool variable
exitOnErrors
is set to true by the Bool object's Set method if the parameter
exit-on-error
is found among the command line arguments:
var exitOnErrors bool ps, err := paramset.New() p := ps.Add("exit-on-errors", psetter.Bool{Value: &exitOnErrors}, "Errors make the program exit if this flag is set to true")
It is expected that the most common use of this package will be to pass instances of the various setters as a parameter to the Add(...) method.
Index ¶
- Constants
- func HasChecks(cc CheckCounter) string
- func NilValueMessage(paramName, setterType string) string
- type Bool
- type CheckCounter
- type Duration
- type Enum
- type EnumList
- type EnumMap
- type Float64
- type Int64
- type Int64List
- type Map
- type Nil
- type Pathname
- type Regexp
- type StrList
- type StrListAppender
- type StrListSeparator
- type String
- type Time
- type TimeLocation
Constants ¶
const ( TimeFmtDefault = "2006/Jan/02T15:04:05" TimeFmtHMS = "15:04:05" TimeFmtHoursMins = "15:04" TimeFmtDateOnly = "2006/Jan/02" TimeFmtTimestamp = "20060102.150405" TimeFmtISO8601 = "2006-01-02T15:04:05" )
Time Formats given here can be used to set the Format member of the Time struct
const (
StrListDefaultSep = ","
)
StrListDefaultSep is the default separator for a list of strings. it is set to a comma
Variables ¶
This section is empty.
Functions ¶
func HasChecks ¶
func HasChecks(cc CheckCounter) string
HasChecks returns a string reporting whether or not the number of checks is zero. This is suitable for constructing a string to be returned by an AllowedValues function.
func NilValueMessage ¶
NilValueMessage ...
Types ¶
type Bool ¶
Bool is used to set boolean flags
The Invert flag is used to invert the normal meaning of a boolean parameter. It is useful where you want to have a parameter of the form 'dont-xxx' but use it to set the value of a bool variable (default value: true) such as 'xxx' which you can then test by saying:
if xxx { doXXX() }
rather than having to set the value of a variable which you would have to call dontXXX and then test by saying:
if !dontXXX { doXXX() }
The benefit is that you can avoid the ugly double negative
func (Bool) AllowedValues ¶
AllowedValues returns a description of the allowed values.
func (Bool) CheckSetter ¶
CheckSetter panics if the setter has not been properly created - if the Value is nil
func (Bool) CurrentValue ¶
CurrentValue returns the current setting of the parameter value
func (Bool) SetWithVal ¶
SetWithVal should be called when a value is given for the parameter
type CheckCounter ¶
type CheckCounter interface {
CountChecks() int
}
CheckCounter can be used if you want to report the number of checks that a type has
type Duration ¶
type Duration struct { param.ValueReqMandatory param.NilAVM Value *time.Duration Checks []check.Duration }
Duration allows you to specify a parameter that can be used to set a time.Duration value. You can also supply a check function that will validate the Value. See the check package for some common pre-defined checks.
func (Duration) AllowedValues ¶
AllowedValues returns a string describing the allowed values
func (Duration) CheckSetter ¶
CheckSetter panics if the setter has not been properly created - if the Value is nil
func (Duration) CountChecks ¶
CountChecks returns the number of check functions this setter has
func (Duration) CurrentValue ¶
CurrentValue returns the current setting of the parameter value
func (Duration) SetWithVal ¶
SetWithVal (called when a value follows the parameter) checks that the value can be parsed to a duration, if it cannot be parsed successfully it returns an error. If there is a check and the check is violated it returns an error. Only if the value is parsed successfully and the check is not violated is the Value set.
type Enum ¶
type Enum struct { param.ValueReqMandatory param.AVM Value *string }
Enum allows you to specify a parameter that will only allow an enumerated range of values which are specified in the AllowedVals map which maps each allowed value to a description
func (Enum) AllowedValues ¶
AllowedValues returns a string listing the allowed values
func (Enum) CheckSetter ¶
CheckSetter panics if the setter has not been properly created - if the Value is nil or there are no allowed values.
func (Enum) CurrentValue ¶
CurrentValue returns the current setting of the parameter value
type EnumList ¶
type EnumList struct { param.ValueReqMandatory param.AVM Value *[]string StrListSeparator Checks []check.StringSlice }
EnumList sets the values in a slice of strings. The values must be in the allowed values map
func (EnumList) AllowedValues ¶
AllowedValues returns a string listing the allowed values
func (EnumList) CheckSetter ¶
CheckSetter panics if the setter has not been properly created - if the Value is nil or there are no allowed values or the initial value is not allowed.
func (EnumList) CountChecks ¶
CountChecks returns the number of check functions this setter has
func (EnumList) CurrentValue ¶
CurrentValue returns the current setting of the parameter value
func (EnumList) SetWithVal ¶
SetWithVal (called when a value follows the parameter) splits the value using the list separator. It then checks all the values for validity and only if all the values are in the allowed values list does it add them to the slice of strings pointed to by the Value. It returns a error for the first invalid value or if a check is breached.
type EnumMap ¶
type EnumMap struct { param.ValueReqMandatory param.AVM Value *map[string]bool AllowHiddenMapEntries bool StrListSeparator }
EnumMap sets the entry in a map of strings. The values initially set in the map must be in the allowed values map unless AllowHiddenMapEntries is set to true. Only values with keys in the allowed values map can be set. If you allow hidden values then you can have entries in your map which cannot be set through this interface but this will still only allow values to be set which are in the allowed values map.
It is recommended that you should use string constants for setting and accessing the map entries and for initialising the allowed values map to avoid possible errors.
func (EnumMap) AllowedValues ¶
AllowedValues returns a string listing the allowed values
func (EnumMap) CheckSetter ¶
CheckSetter panics if the setter has not been properly created - if the Value is nil or the map has not been created yet or if there are no allowed values.
func (EnumMap) CurrentValue ¶
CurrentValue returns the current setting of the parameter value
func (EnumMap) SetWithVal ¶
SetWithVal (called when a value follows the parameter) splits the value using the list separator. It then checks all the values for validity and only if all the values are in the allowed values list does it set the entries in the map of strings pointed to by the Value. It returns a error for the first invalid value.
type Float64 ¶
Float64 allows you to specify a parameter that can be used to set an float64 value. You can also supply a check function that will validate the Value. There are some helper functions given below (called Float64Check...) which will return functions that can perform a few common checks. For instance you can ensure that the value is positive by setting one of the Checks to the value returned by Float64CheckGT(0)
func (Float64) AllowedValues ¶
AllowedValues returns a string describing the allowed values
func (Float64) CheckSetter ¶
CheckSetter panics if the setter has not been properly created - if the Value is nil.
func (Float64) CountChecks ¶
CountChecks returns the number of check functions this setter has
func (Float64) CurrentValue ¶
CurrentValue returns the current setting of the parameter value
func (Float64) SetWithVal ¶
SetWithVal (called when a value follows the parameter) checks that the value can be parsed to a float, if it cannot be parsed successfully it returns an error. If there is a check and the check is violated it returns an error. Only if the value is parsed successfully and the check is not violated is the Value set.
type Int64 ¶
Int64 allows you to specify a parameter that can be used to set an int64 value. You can also supply a check function that will validate the Value. See the check package for some helper functions which will return functions that can perform a few common checks. For instance you can ensure that the value is positive by setting one of the Checks to the value returned by check.Int64GT(0)
func (Int64) AllowedValues ¶
AllowedValues returns a string describing the allowed values
func (Int64) CheckSetter ¶
CheckSetter panics if the setter has not been properly created - if the Value is nil.
func (Int64) CountChecks ¶
CountChecks returns the number of check functions this setter has
func (Int64) CurrentValue ¶
CurrentValue returns the current setting of the parameter value
func (Int64) SetWithVal ¶
SetWithVal (called when a value follows the parameter) checks that the value can be parsed to an integer, if it cannot be parsed successfully it returns an error. If there are checks and any check is violated it returns an error. Only if the value is parsed successfully and no checks are violated is the Value set.
type Int64List ¶
type Int64List struct { param.ValueReqMandatory param.NilAVM Value *[]int64 StrListSeparator Checks []check.Int64Slice }
Int64List allows you to specify a parameter that can be used to set a list (a slice) of int64's. You can override the list separator by setting the Sep value.
If you have a list of allowed values you should use EnumList
func (Int64List) AllowedValues ¶
AllowedValues returns a description of the allowed values. It includes the separator to be used
func (Int64List) CheckSetter ¶
CheckSetter panics if the setter has not been properly created - if the Value is nil.
func (Int64List) CountChecks ¶
CountChecks returns the number of check functions this setter has
func (Int64List) CurrentValue ¶
CurrentValue returns the current setting of the parameter value
type Map ¶
type Map struct { param.ValueReqMandatory param.NilAVM Value *map[string]bool Checks []check.MapStringBool StrListSeparator }
Map sets the entry in a map of strings. Each value from the parameter is used as a key in the map with the map entry set to true.
func (Map) AllowedValues ¶
AllowedValues returns a string listing the allowed values
func (Map) CheckSetter ¶
CheckSetter panics if the setter has not been properly created - if the Value is nil or the map has not been created yet.
func (Map) CountChecks ¶ added in v3.3.0
CountChecks returns the number of check functions this setter has
func (Map) CurrentValue ¶
CurrentValue returns the current setting of the parameter value
type Nil ¶
type Nil struct { param.ValueReqNone param.NilAVM }
Nil is used if no value is to be set. It can be useful if the only effect is to be through the PostAction.
func (Nil) AllowedValues ¶
AllowedValues returns a description of the allowed values.
func (Nil) CurrentValue ¶
CurrentValue returns the current setting of the parameter value, in this case there is never any current value.
type Pathname ¶
type Pathname struct { param.ValueReqMandatory param.NilAVM Value *string Expectation filecheck.Provisos Checks []check.String }
Pathname allows you to specify a parameter that can be used to set an pathname value. You can specify some required attributes of the referenced file-system object by setting the AttributesNeeded member. You can also supply a check function that will validate the Value. For instance you could check that the value was in a particular directory
func (Pathname) AllowedValues ¶
AllowedValues returns a string describing the allowed values
func (Pathname) CheckSetter ¶
CheckSetter panics if the setter has not been properly created - if the Value is nil.
func (Pathname) CountChecks ¶
CountChecks returns the number of check functions this setter has
func (Pathname) CurrentValue ¶
CurrentValue returns the current setting of the parameter value
func (Pathname) SetWithVal ¶
SetWithVal (called when a value follows the parameter) checks first that the value can be converted into a pathname. Then it confirms that the file conforms to the supplied provisos. If there are checks and any check is violated it returns an error. Only if the value is converted successfully and no checks are violated is the Value set and a nil error is returned.
type Regexp ¶
Regexp allows you to specify a parameter that can be used to set an regexp value.
func (Regexp) AllowedValues ¶
AllowedValues returns a string describing the allowed values
func (Regexp) CheckSetter ¶
CheckSetter panics if the setter has not been properly created - if the Value is nil.
func (Regexp) CurrentValue ¶
CurrentValue returns the current setting of the parameter value
func (Regexp) SetWithVal ¶
SetWithVal (called when a value follows the parameter) checks that the value can be parsed to regular expression, if it cannot be parsed successfully it returns an error. Only if the value is parsed successfully is the Value set.
type StrList ¶
type StrList struct { param.ValueReqMandatory param.NilAVM Value *[]string StrListSeparator Checks []check.StringSlice }
StrList allows you to specify a parameter that can be used to set an list (a slice) of strings. You can override the list separator by setting the Sep value.
If you have a list of allowed values you should use EnumList
func (StrList) AllowedValues ¶
AllowedValues returns a description of the allowed values. It includes the separator to be used
func (StrList) CheckSetter ¶
CheckSetter panics if the setter has not been properly created - if the Value is nil.
func (StrList) CountChecks ¶
CountChecks returns the number of check functions this setter has
func (StrList) CurrentValue ¶
CurrentValue returns the current setting of the parameter value
type StrListAppender ¶ added in v3.2.0
type StrListAppender struct { param.ValueReqMandatory param.NilAVM Value *[]string StrListSeparator Checks []check.String }
StrListAppender allows you to specify a parameter that can be used to add to a list (a slice) of strings.
The user of the program which has a parameter of this type can pass multiple parameters and each will add to the list of values rather than replacing it each time. Note that each value must be passed separately - there is no way to pass multiple values at the same time. Also note that there is no way to reset the value, if y=this feature is required another parameter could be set up that will do this.
If you have a list of allowed values you should use EnumList
func (StrListAppender) AllowedValues ¶ added in v3.2.0
func (s StrListAppender) AllowedValues() string
AllowedValues returns a description of the allowed values. It includes the separator to be used
func (StrListAppender) CheckSetter ¶ added in v3.2.0
func (s StrListAppender) CheckSetter(name string)
CheckSetter panics if the setter has not been properly created - if the Value is nil.
func (StrListAppender) CountChecks ¶ added in v3.2.0
func (s StrListAppender) CountChecks() int
CountChecks returns the number of check functions this setter has
func (StrListAppender) CurrentValue ¶ added in v3.2.0
func (s StrListAppender) CurrentValue() string
CurrentValue returns the current setting of the parameter value
func (StrListAppender) SetWithVal ¶ added in v3.2.0
func (s StrListAppender) SetWithVal(_ string, paramVal string) error
SetWithVal (called when a value follows the parameter) splits the value into a slice of strings and sets the Value accordingly. It will return an error if a check is breached.
type StrListSeparator ¶
type StrListSeparator struct {
Sep string
}
StrListSeparator holds the separator value
func (StrListSeparator) GetSeparator ¶
func (sls StrListSeparator) GetSeparator() string
GetSeparator returns the separator or the default value (a comma) if it is an empty string
func (StrListSeparator) ListValDesc ¶
func (sls StrListSeparator) ListValDesc(name string) string
ListValDesc returns that fragment of the description of what values are allowed which explains how the list values are separated from one another.
type String ¶
String is the type for setting string values from parameters
func (String) AllowedValues ¶
AllowedValues simply returns "any string" since String does not check its value
func (String) CheckSetter ¶
CheckSetter panics if the setter has not been properly created - if the Value is nil.
func (String) CountChecks ¶
CountChecks returns the number of check functions this setter has
func (String) CurrentValue ¶
CurrentValue returns the current setting of the parameter value
type Time ¶ added in v3.6.0
type Time struct { param.ValueReqMandatory param.NilAVM Value *time.Time Format string Checks []check.Time }
Time allows you to specify a parameter that can be used to set a time.Time value. You can also supply check functions that will validate the Value. If no Format is given then the default format (see TimeFmtDefault) will be used to parse the time value
func (Time) AllowedValues ¶ added in v3.6.0
AllowedValues returns a string describing the allowed values
func (Time) CheckSetter ¶ added in v3.6.0
CheckSetter panics if the setter has not been properly created - if the Value is nil.
func (Time) CountChecks ¶ added in v3.6.0
CountChecks returns the number of check functions this setter has
func (Time) CurrentValue ¶ added in v3.6.0
CurrentValue returns the current setting of the parameter value
func (Time) SetWithVal ¶ added in v3.6.0
SetWithVal (called when a value follows the parameter) checks that the value can be parsed to a time, if it cannot be parsed successfully it returns an error. If there is a check and the check is violated it returns an error. Only if the value is parsed successfully and the check is not violated is the Value set.
type TimeLocation ¶
type TimeLocation struct { param.ValueReqMandatory param.NilAVM Value **time.Location Checks []check.TimeLocation }
TimeLocation allows you to specify a parameter that can be used to set a time.Location pointer. You can also supply check functions that will validate the Value.
func (TimeLocation) AllowedValues ¶
func (s TimeLocation) AllowedValues() string
AllowedValues returns a string describing the allowed values
func (TimeLocation) CheckSetter ¶
func (s TimeLocation) CheckSetter(name string)
CheckSetter panics if the setter has not been properly created - if the Value is nil.
func (TimeLocation) CountChecks ¶
func (s TimeLocation) CountChecks() int
CountChecks returns the number of check functions this setter has
func (TimeLocation) CurrentValue ¶
func (s TimeLocation) CurrentValue() string
CurrentValue returns the current setting of the parameter value
func (TimeLocation) SetWithVal ¶
func (s TimeLocation) SetWithVal(_ string, paramVal string) error
SetWithVal (called when a value follows the parameter) checks that the value can be parsed to a location, if it cannot be parsed successfully it returns an error. If there is a check and the check is violated it returns an error. Only if the value is parsed successfully and the check is not violated is the Value set. If the supplied value cannot be successfully translated into a time.Location then any embedded spaces will be converted to underscores and the value will be retried. If this also fails then the original error is returned.
Source Files ¶
- boolSetter.go
- checkSetterUtils.go
- doc.go
- durationSetter.go
- enumListSetter.go
- enumMapSetter.go
- enumSetter.go
- float64Setter.go
- hasChecks.go
- int64ListSetter.go
- int64Setter.go
- mapSetter.go
- nilSetter.go
- pathnameSetter.go
- regexpSetter.go
- strListAppender.go
- strListSeparator.go
- strListSetter.go
- stringSetter.go
- timeLocationSetter.go
- timeSetter.go