Giter Club home page Giter Club logo

args's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.