Git Product home page Git Product logo

flags's Introduction

flags - More flag Types

GoDoc Actions Status License: MIT

package main

import (
	"fmt"
	"net/url"
	"os"
	"time"

	"github.com/tebeka/flags"
)

var config struct {
	in      *os.File
	name    string
	port    int
	start   time.Time
	retries int
	url     *url.URL
}

func main() {
	flags.Add.File(&config.in, 'r', "input", "input file")
	flags.Add.Int(&config.retries, checkRetries, "retries", "number of retries")
	flags.Add.Port(&config.port, "port", "port to listen on")
	flags.Add.String(&config.name, checkName, "name", "logger name")
	flags.Add.Time(&config.start, time.RFC3339, "start", "start time")
	flags.Add.URL(config.url, "url", "url to hit")
	flags.Parse()

	fmt.Printf("in: %q\n", config.in.Name())
	fmt.Printf("name: %q\n", config.name)
	fmt.Printf("port: %d\n", config.port)
	fmt.Printf("retries: %d\n", config.retries)
	fmt.Printf("start: %s\n", config.start)
	fmt.Printf("url: %q\n", config.url.String())

	// Output:
	// in: "/dev/null"
	// name: "lassie"
	// port: 999
	// retries: 3
	// start: 2019-11-26 19:23:42 +0000 UTC
	// url: "http://example.com"
}

func checkRetries(n int) error {
	if n < 0 || n > 10 {
		return fmt.Errorf("retries = %d not in range [0:10]", n)
	}
	return nil
}

func checkName(s string) error {
	if len(s) == 0 {
		return fmt.Errorf("empty name")
	}
	return nil
}

func init() {
	// Set defaults
	config.in = os.Stdin
	config.name = "bugs"
	config.port = 8080
	config.retries = 1
	config.start = time.Now()
	config.url, _ = url.Parse("http://localhost:8080")
}

Now try:

$ ./demo --help
Usage of ./demo:
sage of /tmp/go-build652444922/b001/exe/demo:
  -input value
    	input file (default /dev/stdin)
  -name value
    	logger name (default bugs)
  -port value
    	port to listen on (default 8080)
  -retries value
    	number of retries (default 1)
  -start value
    	start time (default 2019-12-03T10:35:48+02:00)
  -url value
    	url to hit (default http://localhost:8080)

and

$ ./demo \
    -input /dev/null \
    -name lassie \
    -port 999 \
    -retries 3 \
    -start 2019-11-26T19:23:42Z \
    -url http://example.com

in: "/dev/stdin"
name: "bugs"
port: 8080
retries: 1
start: 2019-12-03 10:38:06.553133356 +0200 IST m=+0.000050679
url: "http://localhost:8080"

Types

This section is still unfinished, see example_test.go.

  • File
  • Float
  • Int
  • Port
  • String
  • Time
  • URL

flags's People

Contributors

tebeka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

flags's Issues

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.