flago

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: MIT Imports: 8 Imported by: 0

README

PkgGoDev Go Report Card

Flago

Super simple package for binding command-line flags to your struct.

Installation

go get github.com/KimMachineGun/flago

Example

Playground

package main

import (
	"fmt"
	"log"
	"strings"
	"time"

	"github.com/KimMachineGun/flago"
)

type Flags struct {
	A string                `flago:"a,usage of a"`
	B int                   `flago:"b,usage of b"`
	C CommaSeparatedStrings `flago:"c,usage of c"`
	D time.Time             `flago:"d,usage of d"`
	E NestedFlags           `flago:"e.,nested flags: "`
}

type NestedFlags struct {
	A string `flago:"a,usage of a"`
	B int    `flago:"b,usage of b"`
}

// CommaSeparatedStrings implements flag.Value.
type CommaSeparatedStrings []string

func (s *CommaSeparatedStrings) String() string {
	return strings.Join(*s, ",")
}

func (s *CommaSeparatedStrings) Set(v string) error {
	*s = strings.Split(v, ",")
	return nil
}

// go run main.go -b=360 -c=Hello,World -d=2020-01-02T00:00:00Z -e.a=CD
func main() {
	// set default values
	flags := Flags{
		A: "AB",
		B: 180,
		C: []string{"Foo", "Bar"},
		D: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
		E: NestedFlags{
			A: "AB",
			B: 180,
		},
	}

	// go run main.go --help
	// Output:
	//  -a string
	//        usage of a (default "AB")
	//  -b int
	//        usage of b (default 180)
	//  -c value
	//        usage of c (default Foo,Bar)
	//  -d value
	//        usage of d (default 2020-01-01T00:00:00Z)
	//  -e.a string
	//        nested flags: usage of a (default "AB")
	//  -e.b int
	//        nested flags: usage of b (default 180)

	// parse flags
	err := flago.Parse(&flags)
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println(flags)
	// Output: {AB 360 [Hello World] 2020-01-02 00:00:00 +0000 UTC {CD 180}}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bind

func Bind(fs *flag.FlagSet, v interface{}) error

Bind defines flags based on the struct field tags and binds flags to the corresponding fields. If v is nil or not a pointer of struct, Bind returns InvalidBindError.

Bind uses 'flago' field tag to specify the name and usage of the flag. If a field does not have a 'flago' field tag, it will be ignored.

If a field is struct type, Bind will parse it recursively, and its field tag will be used as a prefix of the names of the flags defined by itself.

Supported Field Types:

  • string
  • bool
  • int
  • int64
  • uint
  • uint64
  • float64
  • time.Duration
  • flag.Value

Examples:

type Example struct {
	// Name defines a 'name' flag, and its usage message is not set (empty string).
	Name string `flago:"name"`

	// Age defines a 'age' flag, and its usage message is 'the age of gopher'.
	// The name and usage specified in the field tag are separated by comma.
	// flag.IntVar()
	Age int `flago:"age,the age of gopher"`

	// Nested defines a 'nested.*' flag, and its usage prefix is 'nested flags: '.
	// Nested is a struct type, so Bind will parse it recursively.
	Nested struct {
		// NestedName defines a 'name' flag, and its usage message is 'nested name'.
		// Since NestedName is a field of Nested, the name of the flag will be 'nested.name',
		// and its usage message will be 'nested flags: nested name'.
		NestedName string `flago:"name,nested name"`
	} `flago:"nested,nested flags: "`
}

func BindEnvExpanded added in v0.4.0

func BindEnvExpanded(fs *flag.FlagSet, v interface{}) error

BindEnvExpanded defines env-expanded flags based on the struct field tags and binds flags to the corresponding fields.

func BindEnvExpandedWithPrefix added in v0.4.0

func BindEnvExpandedWithPrefix(fs *flag.FlagSet, v interface{}, prefix string) error

BindEnvExpandedWithPrefix defines env-expanded flags with prefix.

func BindWithPrefix added in v0.3.0

func BindWithPrefix(fs *flag.FlagSet, v interface{}, prefix string) error

BindWithPrefix defines flags with prefix. See the comments of Bind for more details.

func Parse added in v0.5.0

func Parse(v interface{}) error

Parse parses the default command-line flag set defined in the flag package.

func ParseEnvExpanded added in v0.6.0

func ParseEnvExpanded(v interface{}) error

ParseEnvExpanded parses the default command-line flag set defined in the flag package. Unlike Parse, it expands environment variables in the flag values.

func ParseEnvExpandedWithPrefix added in v0.6.0

func ParseEnvExpandedWithPrefix(v interface{}, prefix string) error

ParseEnvExpandedWithPrefix parses the default command-line flag set defined in the flag package with prefix. Unlike ParseWithPrefix, it expands environment variables in the flag values.

func ParseWithPrefix added in v0.6.0

func ParseWithPrefix(v interface{}, prefix string) error

ParseWithPrefix parses the default command-line flag set defined in the flag package with prefix.

Types

type InvalidBindError

type InvalidBindError struct {
	Type reflect.Type
}

An InvalidBindError describes an invalid argument passed to Bind.

func (*InvalidBindError) Error

func (e *InvalidBindError) Error() string

Jump to

Keyboard shortcuts

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