8000 GitHub - sebnyberg/flagtags: Tag structs fields to generate urfave/cli flags
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

sebnyberg/flagtags

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

flagtags

Go Reference

Each configurable option should exist in three forms:

  1. Struct field (PascalCase)
  2. CLI flag (--kebab-case)
  3. Environment variable (SCREAMING_SNAKE_CASE)

Keeping the three in sync is tedious and error-prone.

This package uses a struct as the source of truth to generate urfave/cli/v2 flags with consistent naming conventions.

Example

package main

import (
	"log"
	"os"

	"github.com/sebnyberg/flagtags"
	"github.com/urfave/cli/v2"
)

type config struct {
	Port        int `value:"3001"`
	DisableAuth bool
	JWTSignKey  string
}

func main() {
	var opts flagtags.Options
	opts.EnvPrefix = "MYAPP_"
	flags := flagtags.ParseFlagsWithOpts(&config, opts)
	app := &cli.App{flags: flags}
	app.Run(os.Args)
}

Supported flag fields (tags)

Tag Flag field Description Default value
name flag.Name Flag name Struct field name in kebab-case
env flag.EnvVars[0] Environment variable name Struct field name in SCREAMING_SNAKE_CASE
value flag.Value Default flag value Default initialized value for the corresponding primitive type
usage flag.Usage Usage Empty string

Supported types

Currently this library only supports struct fields of type:

  • int
  • string
  • bool

Complete example

package main

import (
	"fmt"
	"os"

	"github.com/kr/pretty"
	"github.com/sebnyberg/flagtags"
	"github.com/urfave/cli/v2"
)

type config struct {
	Port             int    `value:"3001"`
	DisableAuth      bool   `usage:"Disable authentication"`
	JWTSignKey       string `usage:"JWT signing key"`
	DatabaseHost     string `name:"pghost" env:"PGHOST" usage:"Postgres hostname"`
	DatabasePassword string `name:"pgpassword" env:"PGPASSWORD" usage:"Postgres hostname"`
}

func main() {
	var conf config
	var parseOpts flagtags.Options
	parseOpts.EnvPrefix = "MYAPP_"
	flags := flagtags.MustParseFlagsWithOpts(&conf, parseOpts)

	app := &cli.App{
		Name:     "server",
		HelpName: "server",
		Usage:    "run the server",
		Flags:    flags,
		Action: func(c *cli.Context) error {
			pretty.Println(conf)
			return nil
		},
	}

	if err := app.Run(os.Args); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

Example output from the CLI invocation:

$ go run main.go --help
NAME:
   server - run the server

USAGE:
   server [global options] command [command options] [arguments...]

COMMANDS:
   help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --port value          (default: 3001) [$PORT]
   --disable-auth        Disable authentication (default: false) [$DISABLE_AUTH]
   --jwt-sign-key value  JWT signing key [$JWT_SIGN_KEY]
   --pghost value        Postgres hostname (default: "test") [$PGHOST]
   --pgpassword value    Postgres hostname [$PGPASSWORD]
   --help, -h            show help (default: false)

About

Tag structs fields to generate urfave/cli flags

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

0