Пример #1
0
package args_test

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 Example() {
	Server()
	Server(ListenOn(80), BufferSize(2048*1024), StateDir("/tmp2"))
	// Output:
	//port=8080 buf=1048576 state=/tmp
	//port=80 buf=2097152 state=/tmp2
}
Пример #2
0
package args_test

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)