clarg

package module
v0.0.0-...-f5474c6 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2018 License: MIT Imports: 3 Imported by: 0

README

clarg

GoDoc Go Report Card
Simple Sub-Commands for Go using flag package - can be used via copy/paste too, because it's small and it's just some functions!

Go get master:

$ go get -u -v github.com/dc0d/clarg

Go get v3:

$ go get -u -v gopkg.in/dc0d/clarg.v3

Go get v2:

$ go get -u -v gopkg.in/dc0d/clarg.v2

Documentation

Overview

Package clarg provides simple Sub-Commands for Go using flag package - can be used via copy/paste too, because it's small and it's just some functions!

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(top *flag.FlagSet, subs ...*flag.FlagSet) (*flag.FlagSet, error)

Parse parses arguments for list of commands. The first command is the default command (top level args) and can be nil. Non-Flag args are available via matched FlagSet's Args() method.

Example
topFlags := flag.NewFlagSet("", flag.ExitOnError)
cmdListFlags := flag.NewFlagSet("list", flag.ExitOnError)
cmdSendFlags := flag.NewFlagSet("send", flag.ExitOnError)

var cmdDefault struct {
	count int
	data  string
}

var cmdList struct {
	age  int
	name string
}

var cmdSend struct {
	dst     string
	payload string
}

topFlags.IntVar(&cmdDefault.count, "cnt", 0, "-cnt <count>")
topFlags.StringVar(&cmdDefault.data, "data", "", "-data <data string>")

cmdListFlags.IntVar(&cmdList.age, "age", -1, "-age <age>")
cmdListFlags.StringVar(&cmdList.name, "name", "", "-name <name>")

cmdSendFlags.StringVar(&cmdSend.dst, "dst", "", "-dst <destination>")
cmdSendFlags.StringVar(&cmdSend.payload, "p", "", "-p <payload>")

if cmd, err := Parse(topFlags,
	cmdListFlags,
	cmdSendFlags); err != nil {
	// show/handle error
} else {
	_ = cmd // the matched *flag.FlagSet
}

// use values of back fields for flags
Output:

Example (Env)
topFlags := flag.NewFlagSet("", flag.ExitOnError)

var cmdDefault struct {
	count int
	data  string
}

topFlags.IntVar(&cmdDefault.count, "cnt", 0, "-cnt <count>")
topFlags.StringVar(&cmdDefault.data, "data", "", "-data <data string>")

if cmd, err := Parse(topFlags); err != nil {
	// show/handle error
} else {
	fromenv := func(set *flag.FlagSet, flagName, envName string) error {
		f := set.Lookup(flagName)
		if f == nil {
			return nil
		}
		if f.Value.String() != f.DefValue {
			return nil
		}
		e := os.Getenv(envName)
		if e == "" {
			return nil
		}
		if e == f.DefValue {
			return nil
		}
		return f.Value.Set(e)
	}
	topFlags.VisitAll(func(f *flag.Flag) {
		// sample env var name convention: APPNAME_FLAGNAME
		if err := fromenv(topFlags,
			f.Name,
			strings.ToUpper(filepath.Base(os.Args[0])+"_"+f.Name)); err != nil {
			log.Fatal(err)
		}
	})
	_ = cmd // the matched *flag.FlagSet
}

// use values of back fields for flags
Output:

Example (NonArgs)
topFlags := flag.NewFlagSet("", flag.ExitOnError)

var cmdDefault struct {
	count int
	data  string
}

topFlags.IntVar(&cmdDefault.count, "cnt", 0, "-cnt <count>")
topFlags.StringVar(&cmdDefault.data, "data", "", "-data <data string>")

if cmd, err := Parse(topFlags); err != nil {
	// show/handle error
} else {
	nonFlags := topFlags.Args()
	_ = cmd // the matched *flag.FlagSet
	_ = nonFlags
}

// use values of back fields for flags
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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