conflag

package module
v0.0.0-...-3f49fa4 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2013 License: MIT Imports: 8 Imported by: 0

README

Package conflag is a simple wrapper around flag package that fills flag values
from configuration files before parsing them from command line.

Documentation

Overview

Package conflag is a simple wrapper around flag package that fills flag values from configuration files before parsing them from command-line.

Configuration files contain list of flags in the same format they would appear as command line arguments, but without the leading dash:

http=localhost:8080
play=false

The order of loading configurations is:

/etc/progname
$HOME/.progname

These files are parsed before command-line arguments, so real arguments override flags from configuration file.

$ mycmd -play=true

TODO: Support Windows-specific paths.

Use this package like you would normally use flag:

import (
	...
	flag "github.com/dchest/conflag"
	...
)

But before calling Parse(), set program name:

func main() {
	flag.SetProgName("mycmd")
	flag.Parse()
	...
}

If your program overwrites Usage variable, call SetUsage() instead.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Arg

func Arg(i int) string

Arg returns the i'th command-line argument. Arg(0) is the first remaining argument after flags have been processed.

func Args

func Args() []string

Args returns the non-flag command-line arguments.

func Bool

func Bool(name string, value bool, usage string) *bool

Bool defines a bool flag with specified name, default value, and usage string. The return value is the address of a bool variable that stores the value of the flag.

func BoolVar

func BoolVar(p *bool, name string, value bool, usage string)

BoolVar defines a bool flag with specified name, default value, and usage string. The argument p points to a bool variable in which to store the value of the flag.

func Duration

func Duration(name string, value time.Duration, usage string) *time.Duration

Duration defines a time.Duration flag with specified name, default value, and usage string. The return value is the address of a time.Duration variable that stores the value of the flag.

func DurationVar

func DurationVar(p *time.Duration, name string, value time.Duration, usage string)

DurationVar defines a time.Duration flag with specified name, default value, and usage string. The argument p points to a time.Duration variable in which to store the value of the flag.

func Float64

func Float64(name string, value float64, usage string) *float64

Float64 defines a float64 flag with specified name, default value, and usage string. The return value is the address of a float64 variable that stores the value of the flag.

func Float64Var

func Float64Var(p *float64, name string, value float64, usage string)

Float64Var defines a float64 flag with specified name, default value, and usage string. The argument p points to a float64 variable in which to store the value of the flag.

func GlobalConfigFilePath

func GlobalConfigFilePath() string

GlobalConfigFilePath returns user configuration file path (/etc/progname). If program name is not set, returns an empty string.

func Int

func Int(name string, value int, usage string) *int

Int defines an int flag with specified name, default value, and usage string. The return value is the address of an int variable that stores the value of the flag.

func Int64

func Int64(name string, value int64, usage string) *int64

Int64 defines an int64 flag with specified name, default value, and usage string. The return value is the address of an int64 variable that stores the value of the flag.

func Int64Var

func Int64Var(p *int64, name string, value int64, usage string)

Int64Var defines an int64 flag with specified name, default value, and usage string. The argument p points to an int64 variable in which to store the value of the flag.

func IntVar

func IntVar(p *int, name string, value int, usage string)

IntVar defines an int flag with specified name, default value, and usage string. The argument p points to an int variable in which to store the value of the flag.

func Lookup

func Lookup(name string) *flag.Flag

Lookup returns the Flag structure of the named command-line flag, returning nil if none exists.

func NArg

func NArg() int

NArg is the number of arguments remaining after flags have been processed.

func NFlag

func NFlag() int

NFlag returns the number of command-line flags that have been set.

func NewFlagSet

func NewFlagSet(name string, errorHandling flag.ErrorHandling) *flag.FlagSet

NewFlagSet returns a new, empty flag set with the specified name and error handling property.

func Parse

func Parse()

Parse parses the command-line flags from os.Args[1:]. Must be called after all flags are defined and before flags are accessed by the program.

func Parsed

func Parsed() bool

Parsed returns true if the command-line flags have been parsed.

func PrintDefaults

func PrintDefaults()

PrintDefaults prints to standard error the default values of all defined command-line flags.

func Set

func Set(name, value string) error

Set sets the value of the named command-line flag.

func SetProgName

func SetProgName(name string)

SetProgName sets program name, which is used for locating configuration file, and outputting usage or error information.

If program name is not set, the package won't use configuration file, only command line arguments.

func SetUsage

func SetUsage(usage func())

SetUsage sets underlying flag Usage variable.

func String

func String(name string, value string, usage string) *string

String defines a string flag with specified name, default value, and usage string. The return value is the address of a string variable that stores the value of the flag.

func StringVar

func StringVar(p *string, name string, value string, usage string)

StringVar defines a string flag with specified name, default value, and usage string. The argument p points to a string variable in which to store the value of the flag.

func Uint

func Uint(name string, value uint, usage string) *uint

Uint defines a uint flag with specified name, default value, and usage string. The return value is the address of a uint variable that stores the value of the flag.

func Uint64

func Uint64(name string, value uint64, usage string) *uint64

Uint64 defines a uint64 flag with specified name, default value, and usage string. The return value is the address of a uint64 variable that stores the value of the flag.

func Uint64Var

func Uint64Var(p *uint64, name string, value uint64, usage string)

Uint64Var defines a uint64 flag with specified name, default value, and usage string. The argument p points to a uint64 variable in which to store the value of the flag.

func UintVar

func UintVar(p *uint, name string, value uint, usage string)

UintVar defines a uint flag with specified name, default value, and usage string. The argument p points to a uint variable in which to store the value of the flag.

func Usage

func Usage()

Usage calls the underlying flag Usage function.

func UserConfigFilePath

func UserConfigFilePath() string

UserConfigFilePath returns user configuration file path ($HOME/.progname). If program name is not set, returns an empty string.

func Var

func Var(value flag.Value, name string, usage string)

Var defines a flag with the specified name and usage string. The type and value of the flag are represented by the first argument, of type Value, which typically holds a user-defined implementation of Value. For instance, the caller could create a flag that turns a comma-separated string into a slice of strings by giving the slice the methods of Value; in particular, Set would decompose the comma-separated string into the slice.

Types

This section is empty.

Jump to

Keyboard shortcuts

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