zflag

package
v0.9.13 Latest Latest
Warning

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

Go to latest
Published: May 4, 2022 License: MIT Imports: 11 Imported by: 0

README

zflag

Convert structs into flag.FlagSet arguments using reflection.

Format

The flag tag uses the same order of arguments to define a flag using the library. Omitted values are ignored and trailing commas aren't necessary. Usage strings can also contain commas.

Everything flag:"<name>,<default>,<usage>"
Just name flag:"<name>"
Just name and default flag:"<name>,<default>"
Name and usage flag:"<name>,,<usage>"

Example

package main

import (
	"flag"
	"time"

	"github.com/wyattis/z/zflag"
)

type FileServerConfig struct {
	Addr    string        `flag:",:80,the address to use for this server"`
	Root    string        `flag:"dir,,which root directory to serve files from"`
	Timeout time.Duration `flag:",1s,how long to wait before timing out a request"`
	Tls     bool          `flag:",,start a TLS server"`
}

func main() {
	config := FileServerConfig{}
	set := flag.NewFlagSet("", flag.ExitOnError)
	if err := zflag.ReflectStruct(set, &config); err != nil {
		panic(err)
	}
	set.Usage()
}
Output
Usage:
  -addr string
        the address to use for this server (default ":80")
  -root string
        which root directory to serve files from
  -timeout duration
        how long to wait before timing out a request (default 1s)
  -tls
        start a TLS server

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Configure added in v0.8.6

func Configure(set *flag.FlagSet, config interface{}, opts *FlagOptions) error

Use reflection infer options for a flag.FlagSet based on the types and tags defined on a struct

func Duration added in v0.9.5

func Duration(name string, defaultVal ztime.Duration, description string) *ztime.Duration

func DurationVar added in v0.9.5

func DurationVar(v *ztime.Duration, name string, defaultVal ztime.Duration, description string)

func Parse added in v0.9.5

func Parse()

Types

type FlagOptions added in v0.8.12

type FlagOptions struct {
	Overwrite bool
}

type StringSlice added in v0.9.3

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

func (*StringSlice) Set added in v0.9.3

func (s *StringSlice) Set(val string) error

func (*StringSlice) SetDefault added in v0.9.3

func (s *StringSlice) SetDefault(val []string)

func (*StringSlice) String added in v0.9.3

func (s *StringSlice) String() string

func (*StringSlice) Val added in v0.9.4

func (s *StringSlice) Val() []string

Jump to

Keyboard shortcuts

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