Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option interface {
// Flags defines both a short and long flag for setting the option from the
// command line. For example:
//
// option.Flags('f', "flag")
//
// allows the flags:
//
// myApp -f value
// myApp --flag value
//
// An Option can only have one short and long flag definition so any
// previously defined values will be overwritten. This function does nothing
// if Parse has already been called.
Flags(short rune, longFlag string)
// ShortFlag defines a short flag for setting the option from the command
// line. For example:
//
// option.ShortFlag('f')
//
// allows the flag:
//
// myApp -f value
//
// An Option can only have one short flag defined so any previously defined
// value will be overwritten. This function does nothing if Parse has
// already been called.
ShortFlag(name rune)
// LongFlag defines a long flag for setting the option from the command
// line. For example:
//
// option.LongFlag("flag")
//
// allows the flag:
//
// myApp --flag value
//
// An Option can only have one long flag defined so any previously defined
// value will be overwritten. This function does nothing if Parse has
// already been called.
LongFlag(name string)
// EnvVar defines the name of an environment variable that can be used to
// set this option. The string value of the environment variable must be
// convertible to the type of the option.
EnvVar(name string)
// ConfigKey defines the key this option can use to set itself from a JSON
// configuration file. The value stored under this key must be convertible
// to the Option Type.
ConfigKey(name string)
// Default defines a value that will be used by the option if no flags,
// environment variables, or configuration file values are set or found. The
// value must be the same type as the Option.
Default(value interface{})
// Value returns the encapsulated value held by this Option. Value should
// not be called until Parse has been called. In general Value only needs to
// be called by a parent Options type.
Value() value.Data
// RegisterFlags causes the flags defined on this option to be registered
// with the provided FlagSet. In general RegisterFlags only needs to be
// called by a parent Options type.
RegisterFlags(flags *flag.FlagSet) error
// Parse this option with the provided config. In general Parse only needs
// to be called by a parent Options type.
Parse(config map[string]interface{}) error
// Output the option as a human readable and formatted string.
String() string
}
Option represents a configuration option that can be set either by flag, configuration file, environment variable, or a default value with the value to use being chosen in that order. Options must be one of bool, int, int64, uint, unit64, float64, string, or time.Duration.
func NewOption ¶
NewOption returns an option with i being a pointer to a variable of the type of this option (*bool, *int, *int64, *uint, *uint64, *float64, *string, *time.Duration). The description is used when producing usage information. Providing an invalid type for i will not error here, but will generate an error when the Option is parsed.
type Options ¶
type Options interface {
// Add an option to this set of Options.
Add(option Option)
// Parse the Options using the provided map for configuration options.
Parse(config map[string]interface{}) error
// ParseUsing uses the given Option to locate and load the configuration
// from a file.
ParseUsing(option Option) error
// NArg is the number of arguments remaining after flags have been
// processed. Calling NArg before Parse will simply return 0.
NArg() int
// Args returns the non-flag command-line arguments. Calling Args before
// calling Parse will simply return an empty slice.
Args() []string
// Usage displays the usage information for this set of options to STDERR.
Usage()
// UsageString building of custom usage output by providing just the usage
// details for the defined options.
UsageString() string
}
Options holds a set of configuration options which can be provided by the command line, environment variables, configuration files, or default values.
func NewOptions ¶
func NewOptions() Options
NewOptions returns a new Options type that takes its flags from the arguments provided to the process.
Example ¶
package main
import (
"fmt"
"github.com/domdavis/goconfigure"
)
func main() {
var r string
opts := goconfigure.NewOptions()
// We need to replace opts so arguments sent be testing harnesses are not
// included. Ordinarily this line can be omitted.
opts = goconfigure.NewOptionsWithArgs(nil)
opt := goconfigure.NewOption(&r, "option")
opt.LongFlag("config")
opts.Add(opt)
err := opts.ParseUsing(opt)
fmt.Println(err)
}
Output: <nil>
func NewOptionsWithArgs ¶
NewOptionsWithArgs returns a new Options type that uses the given slice of strings as its argument set.
Example ¶
package main
import (
"fmt"
"github.com/domdavis/goconfigure"
)
func main() {
var options struct {
test string
}
opts := goconfigure.NewOptionsWithArgs([]string{"-flag", "test"})
opt := goconfigure.NewOption(&options.test, "test option")
opt.ShortFlag('f')
opt.LongFlag("flag")
opts.Add(opt)
err := opts.ParseUsing(nil)
if err == nil {
fmt.Println(options)
} else {
fmt.Println(err)
}
}
Output: {test}