envflag

package module
v0.0.0-...-ae32689 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2016 License: MIT Imports: 3 Imported by: 2

README

envflag

Go Report Card GoDoc

Simple environment extension to Golang flag.

Goals

  • Extends Golang flag with environment-variables.
  • Clear precendence: default < environment-variable < cli.
  • Adheres to 12-factor-app.

Installation

$ go get github.com/gobike/envflag

Usage

Create main.go

package main

import (
    "fmt"
    "flag"
    "github.com/gobike/envflag"
)

func main() {
    var (
        times int
    )

    flag.IntVar(&times, "f-times", 1, "this is #")
    envflag.Parse() 

    fmt.Println(times)
}

Run with default.

$ go run main.go 
1 #output

Run with environment-variable set.

$ F_TIMES=100 go run main.go 
100 #output

Run with cli set.

$ F_TIMES=100 go run main.go --f-times=10 
10 #output, environment-variable is ignored

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultEnvflag = &Envflag{
	Cli: flag.CommandLine,
}

DefaultEnvflag is the default Envflag and is used by Parse.

Functions

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.

Types

type Envflag

type Envflag struct {
	Cli *flag.FlagSet
}

Envflag is a environment-variable wrapper for flag.FlagSet.

func (Envflag) Parse

func (e Envflag) Parse()

Parse parses the command-line flags from os.Args[1:]. For unset flags, it will attempt to read & set from environment variables. Must be called after all flags are defined and before flags are accessed by the program.

Jump to

Keyboard shortcuts

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