flagutil

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2022 License: Apache-2.0 Imports: 7 Imported by: 4

README

flagutil

Package flagutil parses flagfiles and env into flagsets of the standard Go 'flag' package.

It attempts to minimize conflict with the use of the standard package. In typical use case (read flagfile first, read environment next, and finally do typical processing of the command line flags), it is sufficient to replace the call to flag.Parse() with flagutil.Parse().

Author: Ivan Vucica

Documentation

License

This package has no license at this time.

Documentation

Overview

Package flagutil parses flagfiles and env into flagsets of the standard Go 'flag' package.

It attempts to minimize conflict with the use of the standard package. In typical use case (read flagfile first, read environment next, and finally do typical processing of the command line flags), it is sufficient to replace the call to flag.Parse() with flagutil.Parse().

Index

Constants

This section is empty.

Variables

View Source
var (
	EnvironmentPrefix = ""
	FlagFileFlagName  = "flag_file"
	FlagFile          = flag.String(FlagFileFlagName, "", "flag file containing options as if specified on the command line, except new-line-delimited")
)

Functions

func Parse

func Parse()

func ParseDefaultEnv

func ParseDefaultEnv(fs *flag.FlagSet) error

ParseDefaultEnv iterates through every flag in the passed flagset, makes the flag names uppercase, and checks if the passed flag is in the default environment using os.LookupEnv. If so, it is set in the flagset.

If the global variable of this package called EnvironmentPrefix is set, then it is uppercased and prepended along with an underscore.

For instance, if a flag is called 'user_name' and the EnvironmentPrefix is set to 'simple', then the environment variable checked will be called 'SIMPLE_USER_NAME'.

func ParseDefaultFlagFile

func ParseDefaultFlagFile(fs *flag.FlagSet) error

ParseDefaultFlagFile opens the file file passed in the --flag_file command line flag, and processes it using ParseFlagFile.

This is expected to be called before parsing the passed flag set, which may be especially problematic if this is the flag.CommandLine flagset. Therefore, special care is taken to process just --flag_file separately.

Additionally, environment is checked for the flag named EnvironmentPrefix + "_FLAG_FILE" (with prefix uppercased), or, if prefix is unset, just "FLAG_FILE".

Name of flag "--flag_file" can be overridden in the global variable FlagFileFlagName. This is taken into account when looking into the environment, and is adequately uppercased.

Flag file description can be seen in docstring for ParseFlagsFromReader.

Currently, only the last "--flag_file" will be taken into account. In future, multiple values may be processed, and in addition to the environment's FLAG_FILE.

func ParseFlagFile

func ParseFlagFile(fs *flag.FlagSet, flagFilePath string) error

ParseFlagFile opens the file from specified path and parses the flags in the file into the passed flagset.

Flag file description can be seen in docstring for ParseFlagsFromReader.

func ParseFlagsFromReader

func ParseFlagsFromReader(fs *flag.FlagSet, reader io.Reader) error

ParseFlagsFromReader accessed the already-opened reader and reads it in its entirety. Each line is considered to be a new flag. Empty lines and lines beginning with '#' are ignored. Each line is trimmed, so lines consisting of just whitespace, or starting with whitespace and '#', will also be skipped.

At this time, there is no way to pass a flag value ending with whitespace. This may change.

Types

This section is empty.

Directories

Path Synopsis
examples
[DEPRECATED - SEE README] Package flagutil parses flagfiles and env into flagsets of the standard Go 'flag' package.
[DEPRECATED - SEE README] Package flagutil parses flagfiles and env into flagsets of the standard Go 'flag' package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL