Ejemplo n.º 1
0
//Package flag defines a config.Loader type that allows laoding values from
//command line arguments using the language's flag package.
//
//We import the flag package but give it the name flaglib to avoid confusion.
//References to flaglib throughout the documentation refer to the standard "flag" package.
package flag

import (
	flaglib "flag"
	"os"

	"github.com/gogolfing/config"
)

//DashSeparatorKeyParser is the default KeyParser for a Loader (set in New()).
const DashSeparatorKeyParser = config.SeparatorKeyParser("-")

//Loader provides settings to load values from command line arguments (or any
//slice of strings).
//Loader implements config.Loader.
type Loader struct {
	//Args is the slice of strings sent to flaglib.FlagSet.Parse().
	//It is set to os.Args[1:] by New().
	Args []string

	//Aliases provides a way to change the name of a command line flag before it
	//gets parsed by KeyParser and inserted into the resulting Values instance.
	//If a flaglib.Flag's Name is a key in this map, then the associated value
	//will be used to create the key instead.
	Aliases map[string]string
Ejemplo n.º 2
0
package env

import (
	"os"
	"strings"

	"github.com/gogolfing/config"
)

//Equal is the string around which results from os.Environ() are split into keys
//and their respective values.
const Equal = "="

//UnderscoreSeparatorKeyParser is a config.SeparatorKeyParser that parses keys
//around the "_" character.
const UnderscoreSeparatorKeyParser = config.SeparatorKeyParser("_")

type prefixParserLoader struct {
	prefix string
	parser config.KeyParser
}

//NewPrefixLowerUnderscoreLoader creates a config.Loader that
//reads in all entries from os.Environ() and inserts into the resulting Values all
//key, value associations whose keys start with prefix.
//The key inserted is parsed with UnderscoreSeparatorKeyParser after prefix is removed.
func NewPrefixLowerUnderscoreLoader(prefix string) config.Loader {
	parser := config.KeyParserFunc(func(k string) config.Key {
		return UnderscoreSeparatorKeyParser.Parse(strings.ToLower(k))
	})
	return NewPrefixParserLoader(prefix, parser)