Documentation ¶
Index ¶
- Constants
- Variables
- func Build() error
- func Usage()
- type Example
- type FileIO
- type FlagSet
- type IO
- type IOError
- type Option
- func Add(o *Option) *Option
- func Bool(name string, defaultValue bool, description string) *Option
- func Enum(name string, possibleValues []string, defaultValue string, description string) *Option
- func Float(name string, defaultValue float64, description string) *Option
- func Get(key string) (*Option, error)
- func Int(name string, defaultValue int64, description string) *Option
- func Require(key string) *Option
- func Str(name string, defaultValue string, description string) *Option
- func (o *Option) AddFilter(v OptionFilterFunc) *Option
- func (o *Option) AddScope(s string)
- func (o Option) Bool() bool
- func (o Option) DebugString() string
- func (o Option) DefaultValueString() string
- func (o *Option) Exportable(v bool) *Option
- func (o Option) Float() float64
- func (o *Option) HasScope(s string) bool
- func (o Option) Int() int64
- func (o *Option) SetFromFlagValue(val string) (err error)
- func (o *Option) SetFromString(val string) (err error)
- func (o *Option) SortOrder(i int) *Option
- func (o Option) Str() string
- func (o Option) String() string
- func (o *Option) Validate(v bool) *Option
- type OptionFilterFunc
- type OptionMeta
- type OptionSet
- type SearchFile
- type Type
Constants ¶
const ( BoolType Type = "bool" StringType = "string" FloatType = "float64" IntType = "int64" CustomType = "custom" )
These Types constants parallel their standard counterparts, and are the four elementary types that come when unmarshaling JSON
Variables ¶
var DefaultOptionMeta = OptionMeta{ Exportable: false, Validate: true, Filters: []OptionFilterFunc{}, SortOrder: 0, }
DefaultOptionMeta returns the default OptionMeta object
var Description string
Description describes the application you're configuring.
var Examples = []Example{}
Examples contains a list of example commands and what they do.
var Name = os.Args[0]
Name is the name of the application you're configuring.
var SearchFiles = []SearchFile{ { Scope: "app", Path: "./config.json", }, { Scope: "user", Path: "$HOME/." + strings.ToLower(Name) + "/config.json", }, }
SearchFiles contains a list of files which may or may not exist, and if they do, contain configuration files. The last entry in this list is parsed first, and its values are overwritten by values in files further up the list.
var UsageWriter io.Writer = os.Stdout
UsageWriter is the io.Writer to use for outputting Usage(). Defaults to stdout.
var Version string
Version is the version of the application you're configuring.
Functions ¶
func Build ¶
func Build() error
Build builds the configuration object. Starts by setting the default values as defined in code, then parses the config file, then loads the overridden options from flag. If set, this also exports the as-run configuration to the the filename set in the "config" option.
Types ¶
type FileIO ¶
type FileIO struct {
// contains filtered or unexported fields
}
FileIO implements IO and writes to the filesystem
type FlagSet ¶
type FlagSet struct {
// contains filtered or unexported fields
}
A FlagSet is a set of Flags from the command-line
func NewFlagSet ¶
NewFlagSet instanciates a new FlagSet with an executable named `name` and OS args `args`.
func (FlagSet) HasHelpFlag ¶
HasHelpFlag returns true if the FlagSet's args contains '-h', '-help' or something similar.
func (*FlagSet) ParseBuiltIn ¶
ParseBuiltIn parses only the built-in flags in the FlagSet (config, config-export, config-generate)
type Option ¶
type Option struct { // The name of the option is what's used to reference the option and its value during the program Name string // What the option is for. This also shows up when invoking `program --help`. Description string // Holds the actual value contained by this option Value interface{} // Holds the default value for this option DefaultValue interface{} // Holds the type of this option Type Type // Extra options Options OptionMeta // contains filtered or unexported fields }
Option holds information for a configuration option
func Enum ¶
Enum creates an Option with the parameters given of type string and a built-in validation to make sure that the parsed Option value is contained within the possibleValues argument.
func Get ¶
Get looks for an Option with the name of `key`. If no Option is found, this function returns an error.
func Require ¶
Require looks for an Option with the name of `key`. If no Option is found, this function panics.
func (*Option) AddFilter ¶
func (o *Option) AddFilter(v OptionFilterFunc) *Option
AddFilter adds an OptionFilterFunc to the Option's filter set. It also sets Validate to true.
func (*Option) AddScope ¶
AddScope adds a scope to an Option indicating that it was parsed in a file with the given scope.
func (Option) Bool ¶
Bool returns the bool value of the option. Will panic if the Option's type is not a bool.
func (Option) DebugString ¶
DebugString returns a string describing some attributes about the Option, including the name, value, type and what scopes it came from.
func (Option) DefaultValueString ¶
DefaultValueString returns the Option's default value as a string.
func (*Option) Exportable ¶
Exportable sets whether or not the Option is exportable to a config file.
func (Option) Float ¶
Float returns the float64 value of the option. Will panic if the Option's type is not a float64.
func (Option) Int ¶
Int returns the int64 value of the option. Will panic if the Option's type not an int64.
func (*Option) SetFromFlagValue ¶
SetFromFlagValue attempts to set the Option's value as its proper type by parsing the string argument, and also sets a hidden value on the Option indicating it was overridden by a flag argument.
func (*Option) SetFromString ¶
SetFromString attempts to set the Option's value as its proper type by parsing the string argument
func (Option) Str ¶
Str returns the string value of the option. Will panic if the Option's type is not a string.
type OptionFilterFunc ¶
OptionFilterFunc is a function type that takes an *Option as a parameter. It returns true, nil if the *Option passes the filter, and false, error with a reason why if it didn't.
func IsOneOfStrings ¶
func IsOneOfStrings(possibleValues []string) OptionFilterFunc
IsOneOfStrings returns an OptionFilterFunc that checks the Option value against a list of string values and returns true if the Option value matches one of the possible values
func NonEmptyString ¶
func NonEmptyString() OptionFilterFunc
NonEmptyString returns an OptionFilterFunc that returns true if the Option value is a non-empty string. It will also return false if the Option is not a string.
type OptionMeta ¶
type OptionMeta struct { // Exportable is true if the option is exportable to a config.json file Exportable bool // Validate is true if the option is required Validate bool // Filters is a set of boolean functions that are tested with the given value. If Validate is true, all of these must succeed. Filters []OptionFilterFunc // SortOrder controls the sort order of Options when displayed in Usage(). Defaults to 0; ties are resolved alphabetically. SortOrder int }
OptionMeta holds information for configuring options on Options
type OptionSet ¶
An OptionSet is map of Options, keyed by the Options' Names.
func (OptionSet) Export ¶
func (os OptionSet) Export(includeNonExportable bool, includeNonOverrides bool) map[string]interface{}
Export returns a map that's suitable for pushing into a config.json file.
func (OptionSet) Get ¶
Get retrieves an Option with the Name of key, and a boolean to determine if it was found or not.
type SearchFile ¶
SearchFile contains a potential config file path and a scope relating to where that file is stored.
func (SearchFile) ExpandedPath ¶
func (f SearchFile) ExpandedPath() string
ExpandedPath returns the SearchFile's path expanded with environment variables.