flags

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2021 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

Package flags is for parsing execution flags.

Usage

Create a struct that requires a flag and parse:

type Config struct {
    ConnectionString string   `flag:"cs"`
    Driver           string   `flag:"driver"`
    Args             []string `flag:"*"`
}

func main() {
    conf := new(Config)

    if err := flags.Parse(conf); err != nil {
        ...

The '*' can be used to parse in all the args

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Args

func Args() []string

Args returns the remaining arguments

func Parse

func Parse(obj interface{}) error

Parse returns the

Example
// Input: app -p 8080 -driver postgres arg1 arg2 arg3
os.Args = []string{`app`, `-p`, `8080`, `-driver`, `postgres`, `arg1`, `arg2`, `arg3`}

type Config struct {
	Port               int      `flag:"p"`
	DBConnectionString string   `flag:"cs"`
	DBDriver           string   `flag:"driver"`
	Args               []string `flag:"*"`
}

c := new(Config)
if err := Parse(c); err != nil {
	panic(err)
}

fmt.Printf("port: %d, connection string: '%s', driver: '%s', args: %q\n", c.Port, c.DBConnectionString, c.DBDriver, c.Args)
Output:
port: 8080, connection string: '', driver: 'postgres', args: ["arg1" "arg2" "arg3"]

func Parsed

func Parsed() bool

Parsed returns true if any flags have been parsed

Types

type Set

type Set struct {
	// contains filtered or unexported fields
}

Set contains a new set of flags with the given arguments

func New

func New(args []string) *Set

New creates a new FlagSet

func (Set) Args

func (x Set) Args() []string

Args returns the remainder of the arguments

func (*Set) Parse

func (x *Set) Parse(obj interface{}) error

Parse gets the tag and adds it to the property if set. Please note that flags overwrite previous values

func (Set) Parsed

func (x Set) Parsed() bool

Parsed returns if any flags have been parsed

Jump to

Keyboard shortcuts

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