zflag

package
v0.7.8 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2022 License: MIT Imports: 8 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 ReflectStruct

func ReflectStruct(set *flag.FlagSet, config interface{}) (err error)

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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