flagstruct

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2022 License: MIT Imports: 9 Imported by: 13

README

flagstruct

define flagset with struct and reflect

features

  • Builds pflag.FlagSet by struct definition
  • Supports only a single use case (shorthand of flag package)
  • Nested structure support (example, shared common option)
  • Default envvar support

install

$ go get -v github.com/podhmo/flagstruct

how to use

package main

import (
	"fmt"
	"os"

	"github.com/podhmo/flagstruct"
)

type Options struct {
	Name    string `flag:"name" help:"name of greeting"`
	Verbose bool   `flag:"verbose" short:"v"`
}

func main() {
	options := &Options{Name: "foo"} // default value

	flagstruct.Parse(options, func(b *flagstruct.Builder) {
		b.Name = "hello"
		b.EnvPrefix = "X_"
	})

	fmt.Printf("parsed: %#+v\n", options)
}

default help message.

$ hello --help
Usage of hello:
      --name string   ENV: X_NAME	name of greeting (default "foo")
  -v, --verbose       ENV: X_VERBOSE	-
pflag: help requested
exit status 2

run it.

$ hello --verbose
parsed: &main.Options{Name:"foo", Verbose:true}
$ hello -v --name bar
parsed: &main.Options{Name:"name", Verbose:true}

# envvar support
$ X_NAME=bar hello -v
parsed: &main.Options{Name:"bar", Verbose:true}

see also ./examples

inspired by

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse[T any](o *T, options ...func(*Builder))

func ParseArgs added in v0.4.0

func ParseArgs[T any](o *T, args []string, options ...func(*Builder))

func PrintHelpAndExitIfError added in v0.4.2

func PrintHelpAndExitIfError(fs *flag.FlagSet, err error, code int)

func WithMoreFlagnameTags added in v0.5.0

func WithMoreFlagnameTags(tags ...string) func(b *Builder)

Types

type Binder

type Binder struct {
	*Config

	State struct {
		// contains filtered or unexported fields
	}
}

func (*Binder) AllRequiredFlagNames added in v0.4.2

func (b *Binder) AllRequiredFlagNames() []string

func (*Binder) Bind

func (b *Binder) Bind(fs *flag.FlagSet, o interface{}) func(*flag.FlagSet) error

func (*Binder) ValidateRequiredFlags added in v0.4.2

func (b *Binder) ValidateRequiredFlags(fs *flag.FlagSet) error

type Builder

type Builder struct {
	Name string
	*Config
}

func NewBuilder

func NewBuilder() *Builder

func (*Builder) Build

func (b *Builder) Build(o interface{}) *FlagSet

type Config

type Config struct {
	HandlingMode flag.ErrorHandling

	EnvvarSupport bool
	EnvPrefix     string
	EnvNameFunc   func(string) string

	FlagnameTags []string
	FlagNameFunc func(string) string

	ShorthandTag string
	HelpTextTag  string
	RequiredTag  string
}

func DefaultConfig

func DefaultConfig() *Config

type FlagSet

type FlagSet struct {
	*flag.FlagSet
	Binder *Binder
}

func Build added in v0.4.0

func Build[T any](o *T, options ...func(*Builder)) *FlagSet

func (*FlagSet) Parse

func (fs *FlagSet) Parse(args []string) error

type HasHelpText

type HasHelpText interface {
	HelpText() string
}

Directories

Path Synopsis
examples module

Jump to

Keyboard shortcuts

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