GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - urfave/sflags: Generate flags by parsing structures

BranchesTags
Open more actions menu

Latest commit

 

History

117 Commits
117 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
cmd/genvalues
cmd/genvalues
 
 
examples
examples
 
 
gen
gen
 
 
scripts
scripts
 
 
validator/govalidator
validator/govalidator
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
.travis.yml
.travis.yml
 
 
LICENSE
LICENSE
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 
camelcase.go
camelcase.go
 
 
camelcase_test.go
camelcase_test.go
 
 
converters.go
converters.go
 
 
converters_test.go
converters_test.go
 
 
flag.go
flag.go
 
 
go.mod
go.mod
 
 
go.sum
go.sum
 
 
parser.go
parser.go
 
 
parser_test.go
parser_test.go
 
 
values.go
values.go
 
 
values.json
values.json
 
 
values_generated.go
values_generated.go
 
 
values_generated_test.go
values_generated_test.go
 
 
values_test.go
values_test.go
 
 

Repository files navigation

Flags based on structures GoDoc Build Status codecov Go Report Card

The sflags package uses structs, reflection and struct field tags to allow you specify command line options. It supports different types and features.

An example:

type HTTPConfig struct {
	Host    string        `desc:"HTTP host"`
	Port    int           `flag:"port p" desc:"some port"`
	SSL     bool          `env:"HTTP_SSL_VALUE"`
	Timeout time.Duration `flag:",deprecated,hidden"`
}

type Config struct {
	HTTP  HTTPConfig
	Stats StatsConfig
}

And you can use your favorite flag or cli library!

Supported libraries and features:

Hidden Deprecated Short Env Required
  • flag
    example - - - - -
    • kingpin
    example
    • spf13/pflag
    example
    - -
    • spf13/cobra
    example
    - -
    • urfave/cli
    example
    -
    • - feature is supported and implemented

    - - feature can't be implemented for this cli library

    Features:

    • Set environment name
    • Set usage
    • Long and short forms
    • Skip field
    • Required
    • Placeholders (by name)
    • Deprecated and hidden options
    • Multiple ENV names
    • Interface for user types.
    • Validation (using govalidator package)
    • Anonymous nested structure support (anonymous structures flatten by default)

    Supported types in structures:

    • int, int8, int16, int32, int64
    • uint, uint8, uint16, uint32, uint64
    • float32, float64
    • slices for all previous numeric types (e.g. []int, []float64)
    • bool
    • []bool
    • string
    • []string
    • nested structures
    • net.TCPAddr
    • net.IP
    • time.Duration
    • regexp.Regexp
    • map for all previous types (e.g. map[int64]bool, map[string]float64)

    Custom types:

    • HexBytes

    • count

    • ipmask

    • enum values

    • enum list values

    • file

    • file list

    • url

    • url list

    • units (bytes 1kb = 1024b, speed, etc)

    Example:

    The code below shows how to use sflags with the flag library. Examples for other flag libraries are available from ./examples dir.

    package main
    
    import (
    	"flag"
    	"log"
    	"time"
    
    	"github.com/urfave/sflags/gen/gflag"
    )
    
    type httpConfig struct {
    	Host    string `desc:"HTTP host"`
    	Port    int
    	SSL     bool
    	Timeout time.Duration
    }
    
    type config struct {
    	HTTP httpConfig
    }
    
    func main() {
    	cfg := &config{
    		HTTP: httpConfig{
    			Host:    "127.0.0.1",
    			Port:    6000,
    			SSL:     false,
    			Timeout: 15 * time.Second,
    		},
    	}
    	err := gflag.ParseToDef(cfg)
    	if err != nil {
    		log.Fatalf("err: %v", err)
    	}
    	flag.Parse()
    }

    That code generates next output:

    $ go run ./main.go --help
    Usage of _obj/exe/main:
      -http-host value
        	HTTP host (default 127.0.0.1)
      -http-port value
        	 (default 6000)
      -http-ssl
    
      -http-timeout value
        	 (default 15s)
    exit status 2

    Options for flag tag

    The flag default key string is the struct field name but can be specified in the struct field's tag value. The "flag" key in the struct field's tag value is the key name, followed by an optional comma and options. Examples:

    // Field is ignored by this package.
    Field int `flag:"-"`
    
    // Field appears in flags as "myName".
    Field int `flag:"myName"`
    
    // If this field is from nested struct, prefix from parent struct will be ingored.
    Field int `flag:"~myName"`
    
    // You can set short name for flags by providing it's value after a space
    // Prefixes will not be applied for short names.
    Field int `flag:"myName a"`
    
    // this field will be removed from generated help text.
    Field int `flag:",hidden"`
    
    // this field will be marked as deprecated in generated help text
    Field int `flag:",deprecated"`

    Options for desc tag

    If you specify description in description tag (desc by default) it will be used in USAGE section.

    Addr string `desc:"HTTP host"`

    This description produces something like:

      -addr value
        	HTTP host (default 127.0.0.1)

    Options for env tag

    If you specify environment variable name in env tag, it will be used to set the value of the field.

    SSL     bool          `env:"HTTP_SSL_VALUE"`

    Options for Parse function:

    // DescTag sets custom description tag. It is "desc" by default.
    func DescTag(val string)
    
    // FlagTag sets custom flag tag. It is "flag" be default.
    func FlagTag(val string)
    
    // Prefix sets prefix that will be applied for all flags (if they are not marked as ~).
    func Prefix(val string)
    
    // EnvPrefix sets prefix that will be applied for all environment variables (if they are not marked as ~).
    func EnvPrefix(val string)
    
    // FlagDivider sets custom divider for flags. It is dash by default. e.g. "flag-name".
    func FlagDivider(val string)
    
    // EnvDivider sets custom divider for environment variables.
    // It is underscore by default. e.g. "ENV_NAME".
    func EnvDivider(val string)
    
    // Validator sets validator function for flags.
    // Check existed validators in sflags/validator package.
    func Validator(val ValidateFunc)
    
    // Set to false if you don't want anonymous structure fields to be flattened.
    func Flatten(val bool)
    
    // InheritHidden sets if fields should inherit the value of the hidden tag from parent structs.
    func InheritHidden()
    
    // InheritDeprecated sets if fields should inherit the value of the deprecated tag from parent structs.
    func InheritDeprecated()

    Known issues

    • kingpin doesn't pass value for boolean arguments. Counter can't get initial value from arguments.

    Similar projects

    • https://github.com/jaffee/commandeer
    • https://github.com/anacrolix/tagflag
    • https://github.com/jessevdk/go-flags

    About

    Generate flags by parsing structures

    Topics

    cli-libraryflaggo

    Resources

    Readme
    BSD-3-Clause license
    Activity
    Custom properties

    Stars

    168 stars

    Watchers

    16 watching

    Forks

    36 forks
    Report repository

    Releases

    Packages

    Used by

    Contributors

    Languages

    You are visitor 01402520 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.