Skip to content

Commit

Permalink
Minor renames to ParseContext fields
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbourgon committed Jul 27, 2023
1 parent aac01a7 commit 80cf439
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
36 changes: 18 additions & 18 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ type Option func(*ParseContext)

// ParseContext receives and maintains parse options.
type ParseContext struct {
readEnvVars bool
envVarPrefix string
envVarSplit string
envVarEnabled bool
envVarPrefix string
envVarSplit string

configFileFilename string
configFileFlagName string
configFileParseFunc ConfigFileParseFunc
configFileOpenFunc func(string) (iofs.File, error)
allowMissingConfigFile bool
ignoreUndefined bool
configFileName string
configFlagName string
configParseFunc ConfigFileParseFunc
configOpenFunc func(string) (iofs.File, error)
configAllowMissingFile bool
configIgnoreUndefinedFlags bool
}

// ConfigFileParseFunc is a function that consumes the provided reader as a config
Expand All @@ -35,7 +35,7 @@ type ConfigFileParseFunc func(r io.Reader, set func(name, value string) error) e
// rarely be used; prefer [WithConfigFileFlag].
func WithConfigFile(filename string) Option {
return func(pc *ParseContext) {
pc.configFileFilename = filename
pc.configFileName = filename
}
}

Expand All @@ -46,7 +46,7 @@ func WithConfigFile(filename string) Option {
// value of the corresponding flag.
func WithConfigFileFlag(flagname string) Option {
return func(pc *ParseContext) {
pc.configFileFlagName = flagname
pc.configFlagName = flagname
}
}

Expand All @@ -55,7 +55,7 @@ func WithConfigFileFlag(flagname string) Option {
// config files are ignored.
func WithConfigFileParser(pf ConfigFileParseFunc) Option {
return func(pc *ParseContext) {
pc.configFileParseFunc = pf
pc.configParseFunc = pf
}
}

Expand All @@ -65,7 +65,7 @@ func WithConfigFileParser(pf ConfigFileParseFunc) Option {
// By default, missing config files result in a parse error.
func WithConfigAllowMissingFile() Option {
return func(pc *ParseContext) {
pc.allowMissingConfigFile = true
pc.configAllowMissingFile = true
}
}

Expand All @@ -76,7 +76,7 @@ func WithConfigAllowMissingFile() Option {
// By default, undefined flags in config files result in a parse error.
func WithConfigIgnoreUndefinedFlags() Option {
return func(pc *ParseContext) {
pc.ignoreUndefined = true
pc.configIgnoreUndefinedFlags = true
}
}

Expand All @@ -87,7 +87,7 @@ func WithConfigIgnoreUndefinedFlags() Option {
// By default, flags are not parsed from environment variables at all.
func WithEnvVars() Option {
return func(pc *ParseContext) {
pc.readEnvVars = true
pc.envVarEnabled = true
}
}

Expand All @@ -100,7 +100,7 @@ func WithEnvVars() Option {
// By default, flags are not parsed from environment variables at all.
func WithEnvVarPrefix(prefix string) Option {
return func(pc *ParseContext) {
pc.readEnvVars = true
pc.envVarEnabled = true
pc.envVarPrefix = prefix
}
}
Expand All @@ -117,7 +117,7 @@ func WithEnvVarPrefix(prefix string) Option {
// By default, no splitting of environment variable values occurs.
func WithEnvVarSplit(delimiter string) Option {
return func(pc *ParseContext) {
pc.readEnvVars = true
pc.envVarEnabled = true
pc.envVarSplit = delimiter
}
}
Expand All @@ -127,6 +127,6 @@ func WithEnvVarSplit(delimiter string) Option {
// filesystem is used, via [os.Open].
func WithFilesystem(fs embed.FS) Option {
return func(pc *ParseContext) {
pc.configFileOpenFunc = fs.Open
pc.configOpenFunc = fs.Open
}
}
26 changes: 13 additions & 13 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func parseFlags(fs Flags, args []string, options ...Option) error {

// Second priority: the environment, i.e. the session.
{
if pc.readEnvVars {
if pc.envVarEnabled {
if err := fs.WalkFlags(func(f Flag) error {
// If the flag has already been set, we can't do anything.
if provided.has(f) {
Expand Down Expand Up @@ -120,36 +120,36 @@ func parseFlags(fs Flags, args []string, options ...Option) error {
{
// First, prefer an explicit filename string.
var configFile string
if pc.configFileFilename != "" {
configFile = pc.configFileFilename
if pc.configFileName != "" {
configFile = pc.configFileName
}

// Next, check the flag name.
if configFile == "" && pc.configFileFlagName != "" {
if f, ok := fs.GetFlag(pc.configFileFlagName); ok {
if configFile == "" && pc.configFlagName != "" {
if f, ok := fs.GetFlag(pc.configFlagName); ok {
configFile = f.GetValue()
}
}

// If they didn't provide an open func, set the default.
if pc.configFileOpenFunc == nil {
pc.configFileOpenFunc = func(s string) (iofs.File, error) {
if pc.configOpenFunc == nil {
pc.configOpenFunc = func(s string) (iofs.File, error) {
return os.Open(s)
}
}

// Config files require both a filename and a parser.
var (
haveConfigFile = configFile != ""
haveParser = pc.configFileParseFunc != nil
haveParser = pc.configParseFunc != nil
parseConfigFile = haveConfigFile && haveParser
)
if parseConfigFile {
configFile, err := pc.configFileOpenFunc(configFile)
configFile, err := pc.configOpenFunc(configFile)
switch {
case err == nil:
defer configFile.Close()
if err := pc.configFileParseFunc(configFile, func(name, value string) error {
if err := pc.configParseFunc(configFile, func(name, value string) error {
// The parser calls us with a name=value pair. We want to
// allow the name to be either the actual flag name, or its
// env var representation (to support .env files).
Expand All @@ -163,9 +163,9 @@ func parseFlags(fs Flags, args []string, options ...Option) error {
target = setFlag
case !fromSet && fromEnv:
target = envFlag
case !fromSet && !fromEnv && pc.ignoreUndefined:
case !fromSet && !fromEnv && pc.configIgnoreUndefinedFlags:
return nil
case !fromSet && !fromEnv && !pc.ignoreUndefined:
case !fromSet && !fromEnv && !pc.configIgnoreUndefinedFlags:
return fmt.Errorf("%s: %w", name, ErrUnknownFlag)
}

Expand All @@ -185,7 +185,7 @@ func parseFlags(fs Flags, args []string, options ...Option) error {
return fmt.Errorf("parse config file: %w", err)
}

case errors.Is(err, iofs.ErrNotExist) && pc.allowMissingConfigFile:
case errors.Is(err, iofs.ErrNotExist) && pc.configAllowMissingFile:
// no problem

default:
Expand Down

0 comments on commit 80cf439

Please sign in to comment.