args

package module
v0.0.0-...-6bcf4c7 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2015 License: Apache-2.0 Imports: 3 Imported by: 3

README

Introduction Travis Build Status

args is a generic library for optional arguments. It is inspired by Dave Cheney's functional options idea. It can also serve the purpose of Python "kwargs" for Go programs.

Usage

Optional arguments are defined using New and its typed variants. These arguments are basically functions that return argument values of type args.V. To use these argument values, your function receives a variadic list of args.V and then gets the value of each argument:

var Port = args.NewInt()
var RoundTripper = args.New(Default(http.DefaultTransport))
var Timeout = args.NewDuration(Flag("timeout", 10*time.Second, "timeout"))

func MyServer(args ...args.V) {
	port := Port.Get(args)
	rt := RoundTripper.Get(args)
	to := Timeout.Get(args)
	...
}

MyServer()
MyServer(Timeout(1 * time.Second))
MyServer(Timeout(2 * time.Second), RoundTripper(MyTransport))

args can load default values from flags as well as user-defined constants.

To use user-defined types instead of the generic args.V, you need to write a few lines of boiler-plates:

var roundTripper = args.New(http.DefaultTransport)
var timeout = args.NewDuration()

type ServerOpt args.V
func RoundTripper(r http.RoundTripper) ServerOpt { return ServerOpt(roundTripper(r)) }
func Timeout(d time.Duration) ServerOpt { return ServerOpt(d) }

func MyServer(opts ...ServerOpt) {
	rt := roundTripper.Get(opts).(http.RoundTripper)
	to := timeout.Get(opts)
	...
}

Note that, args is focused on easy-to-use APIs. It is not efficient and is wasteful if the function is frequently invoked.

API

Documentation

Overview

args is a generic library for optional arguments. It is inspired by Dave Cheney's functional options idea (http://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis). It can serve the purpose of Python "kwargs" for Go programs.

Optional arguments are defined using New and its typed variants:

These arguments are basically functions that return argument values of type args.V. To use these argument values the function receives a variadic list of args.V and then get the value of each argument from those values:

var RoundTripper = args.New()
var Timeout = args.NewDuration()
func MyServer(args ...args.V) {
	rt := RoundTripper.Get(args)
	to := Timeout.Get(args)
	...
}
MyServer()
MyServer(Timeout(1 * time.Second))
MyServer(Timeout(2 * time.Second), RoundTripper(MyTransport))

To use typed arguments, instead of the generic args.V, you'll need to write a few lines of boiler-plates:

var roundTripper = args.New()
var timeout = args.NewDuration()

type ServerOpt args.V
func RoundTripper(r http.RoundTripper) ServerOpt {
	return ServerOpt(roundTripper(r))
}
func Timeout(d time.Duration) ServerOpt {
	return ServerOpt(d)
}
func MyServer(opts ...ServerOpt) {
	rt := roundTripper.Get(opts).(http.RoundTripper)
	to := timeout.Get(opts)
	...
}

Note that, args is focused on easy-to-use APIs. It is not efficient and is wasteful if the function is frequently invoked.

Example
package main

import (
	"fmt"

	"github.com/soheilhy/args"
)

var ListenOn = args.NewInt(args.Default(8080))
var BufferSize = args.NewUint64(args.Default(uint64(1024 * 1024)))
var StateDir = args.NewString(args.Flag("test.state.dir", "/tmp", "state dir"))

func Server(opts ...args.V) {
	port := ListenOn.Get(opts)
	bufs := BufferSize.Get(opts)
	sdir := StateDir.Get(opts)

	fmt.Printf("port=%d buf=%d state=%s\n", port, bufs, sdir)
}

func main() {
	Server()
	Server(ListenOn(80), BufferSize(2048*1024), StateDir("/tmp2"))
}
Output:

port=8080 buf=1048576 state=/tmp
port=80 buf=2097152 state=/tmp2
Example (Typed)
package main

import (
	"fmt"
	"net/http"
	"time"

	"github.com/soheilhy/args"
)

// port is an integer argument that its default value is read from
// the "-example.typed.port" flag.
var port = args.NewInt(args.Flag("example.typed.port", 1234, "the port"))

// roundTripper is a generic argument that its default value is
// http.DefaultTransport.
var roundTripper = args.New(args.Default(http.DefaultTransport))

// timeout is a duration argument.
var timeout = args.NewDuration()

type ServerOpt args.V

// Port, RoundTripper, and Timeout respectively wrap port, roundTripper,
// and timeout to return ServerOpt instead of args.V.
func Port(p int) ServerOpt                       { return ServerOpt(port(p)) }
func RoundTripper(r http.RoundTripper) ServerOpt { return ServerOpt(roundTripper(r)) }
func Timeout(d time.Duration) ServerOpt          { return ServerOpt(timeout(d)) }

func MyServer(opts ...ServerOpt) {
	port := port.Get(opts)
	fmt.Printf("listening on port %v\n", port)

	rt := roundTripper.Get(opts).(http.RoundTripper)
	if rt == http.DefaultTransport {
		fmt.Println("using the default transport")
	} else {
		fmt.Println("using a user-provided round-tripper")
	}

	to := timeout.Get(opts)
	fmt.Printf("using a timeout of %v\n", to)
}

func main() {
	// If you run "go test -example.typed.port=2222" this test fails,
	// because the output is (correctly) different.
	MyServer(Timeout(1 * time.Second))
}
Output:

listening on port 1234
using the default transport
using a timeout of 1s

Index

Examples

Constants

This section is empty.

Variables

View Source
var Default = defArg.getA()

Default sets the default value of an argument.

Functions

This section is empty.

Types

type A

type A func(i interface{}) V

A represents an argument

func New

func New(vals ...V) A

New creates a new argument.

func (A) Get

func (a A) Get(vals ...interface{}) (val interface{})

Get returns the value of the argument from the values. nil is returned if no value and no default is set for this argument.

func (A) IsSet

func (a A) IsSet(vals ...interface{}) bool

IsSet returns whether the argument is set in the values.

type BoolA

type BoolA func(b bool) V

BoolA is a boolean argument.

func NewBool

func NewBool(vals ...V) BoolA

NewBool creates a new integer argument.

func (BoolA) Get

func (a BoolA) Get(vals ...interface{}) (val bool)

Get returns the value of the int argument among vals.

func (BoolA) IsSet

func (a BoolA) IsSet(vals ...interface{}) bool

IsSet returns whether the argument is set in the values.

type DurationA

type DurationA func(d time.Duration) V

DurationA is a duration argument.

func NewDuration

func NewDuration(vals ...V) DurationA

NewDuration creates a new integer argument.

func (DurationA) Get

func (a DurationA) Get(vals ...interface{}) (val time.Duration)

Get returns the value of the time.Duration argument among vals.

func (DurationA) IsSet

func (a DurationA) IsSet(vals ...interface{}) bool

IsSet returns whether the argument is set in the values.

type Float64A

type Float64A func(i float64) V

Float64A is a 64-bit float argument.

func NewFloat64

func NewFloat64(vals ...V) Float64A

NewFloat64 creates a new integer argument.

func (Float64A) Get

func (a Float64A) Get(vals ...interface{}) (val float64)

Get returns the value of the float64 argument among vals.

func (Float64A) IsSet

func (a Float64A) IsSet(vals ...interface{}) bool

IsSet returns whether the argument is set in the values.

type Int64A

type Int64A func(i int64) V

Int64A is a 64-bit integer argument.

func NewInt64

func NewInt64(vals ...V) Int64A

NewInt64 creates a new integer argument.

func (Int64A) Get

func (a Int64A) Get(vals ...interface{}) (val int64)

Get returns the value of the int64 argument among vals.

func (Int64A) IsSet

func (a Int64A) IsSet(vals ...interface{}) bool

IsSet returns whether the argument is set in the values.

type IntA

type IntA func(i int) V

IntA is an integer argument.

func NewInt

func NewInt(vals ...V) IntA

NewInt creates a new integer argument.

func (IntA) Get

func (a IntA) Get(vals ...interface{}) (val int)

Get returns the value of the int argument among vals.

func (IntA) IsSet

func (a IntA) IsSet(vals ...interface{}) bool

IsSet returns whether the argument is set in the values.

type StringA

type StringA func(s string) V

StringA is a string argument.

func NewString

func NewString(vals ...V) StringA

NewString creates a new integer argument.

func (StringA) Get

func (a StringA) Get(vals ...interface{}) (val string)

Get returns the value of the string argument among vals.

func (StringA) IsSet

func (a StringA) IsSet(vals ...interface{}) bool

IsSet returns whether the argument is set in the values.

type Uint64A

type Uint64A func(i uint64) V

Uint64A is an unsigned 64-bit integer argument.

func NewUint64

func NewUint64(vals ...V) Uint64A

NewUint64 creates a new integer argument.

func (Uint64A) Get

func (a Uint64A) Get(vals ...interface{}) (val uint64)

Get returns the value of the uint64 argument among vals.

func (Uint64A) IsSet

func (a Uint64A) IsSet(vals ...interface{}) bool

IsSet returns whether the argument is set in the values.

type UintA

type UintA func(i uint) V

UintA is an unsigned integer argument.

func NewUint

func NewUint(vals ...V) UintA

NewUint creates a new integer argument.

func (UintA) Get

func (a UintA) Get(vals ...interface{}) (val uint)

Get returns the value of the int argument among vals.

func (UintA) IsSet

func (a UintA) IsSet(vals ...interface{}) bool

IsSet returns whether the argument is set in the values.

type V

type V interface {
	// contains filtered or unexported methods
}

V represents the value of an argument.

func Flag

func Flag(name string, defv interface{}, usage string) V

Flag sets the default value of an argument based on a flag.

Note that Flag can only be used in typed New functions (e.g., NewInt(), ...). New() will panic if it receives a Flag.

Flag overwrites Default if both passed to an argument.

Jump to

Keyboard shortcuts

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