options

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2019 License: MIT Imports: 7 Imported by: 349 modules, 586 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-options

Resolve configuration values set via command line flags, config files, and default struct values.

Build Status GoDoc GitHub release

Documentation

Overview

options resolves configuration values set via command line flags, config files, and default struct values

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Resolve

func Resolve(options interface{}, flagSet *flag.FlagSet, cfg map[string]interface{})

Resolve combines configuration values set via command line flags (FlagSet) or an externally parsed config file (map) onto an options struct.

The options struct supports struct tags "flag", "cfg", and "deprecated", ex:

type Options struct {
	MaxSize     int64         `flag:"max-size" cfg:"max_size"`
	Timeout     time.Duration `flag:"timeout" cfg:"timeout"`
	Description string        `flag:"description" cfg:"description"`
}

Values are resolved with the following priorities (highest to lowest):

  1. Command line flag
  2. Deprecated command line flag
  3. Config file value
  4. Get() value (if Getter)
  5. Options struct default value
Example
package main

import (
	"flag"
	"fmt"
	"time"

	"github.com/mreiferson/go-options"
)

type Options struct {
	MaxSize     int64         `flag:"max-size" cfg:"max_size"`
	Timeout     time.Duration `flag:"timeout" cfg:"timeout"`
	Description string        `flag:"description" cfg:"description"`
}

func main() {
	flagSet := flag.NewFlagSet("example", flag.ExitOnError)
	flagSet.Int64("max-size", 1024768, "maximum size")
	flagSet.Duration("timeout", 1*time.Hour, "timeout setting")
	flagSet.String("description", "", "description info")
	// parse command line arguments here
	// flagSet.Parse(os.Args[1:])
	flagSet.Parse([]string{"-timeout=5s"})

	opts := &Options{
		MaxSize: 1,
		Timeout: time.Second,
	}
	cfg := map[string]interface{}{
		"timeout": "1h",
	}

	fmt.Printf("%#v", opts)
	options.Resolve(opts, flagSet, cfg)
	fmt.Printf("%#v", opts)
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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