structflag

package module
v0.0.0-...-fb412cd Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2023 License: MIT Imports: 15 Imported by: 1

README

go-structflag

Use the Go flag package on config structs

Documentation

Index

Constants

View Source
const (

	// ErrCommandNotFound is returned when a command was not found
	ErrCommandNotFound = constError("command not found")

	// ErrNotEnoughArguments is returned when a command
	// is called with not enough aruments
	ErrNotEnoughArguments = constError("not enough argumetns")
)

Variables

View Source
var (
	// Commands holds the global list of app commands.
	// A command is an action executed by the application,
	// something that is not saved in a configuration file.
	Commands CommandList

	// CommandUsageColor is the color in which the
	// command usage will be printed on the screen.
	CommandUsageColor = color.New(color.FgHiCyan)

	// CommandDescriptionColor is the color in which the
	// command usage description will be printed on the screen.
	CommandDescriptionColor = color.New(color.FgCyan)
)
View Source
var (

	// Output used for printing usage
	Output io.Writer = os.Stderr

	// FlagUsageColor is the color in which the
	// flag usage will be printed on the screen.
	FlagUsageColor = color.New(color.FgHiGreen)

	// FlagDescriptionColor is the color in which the
	// flag usage description will be printed on the screen.
	FlagDescriptionColor = color.New(color.FgGreen)

	// AppName is the name of the application, defaults to os.Args[0]
	AppName = os.Args[0]

	PrintUsageIntro = PrintCommandsUsageIntro

	// OnParseError defines the behaviour if there is an
	// error while parsing the flags.
	// See https://golang.org/pkg/flag/#ErrorHandling
	OnParseError = pflag.ExitOnError

	// NewFlags returns new Flags, defaults to flag.NewFlagSet(AppName, OnParseError).
	NewFlags = func() Flags {
		flagSet := pflag.NewFlagSet(AppName, OnParseError)
		flagSet.Usage = PrintUsage
		flagSet.SetOutput(&flagSetColorOutput)
		return flagSet
	}
)
View Source
var (
	// NameTag is the struct tag used to overwrite
	// the struct field name as flag name.
	// Struct fields with NameTag of "-" will be ignored.
	NameTag = "flag"

	// ShorthandTag is the struct tag used to define
	// the possix shorthand command line argument.
	ShorthandTag = "short"

	// UsageTag is the struct tag used to give
	// the usage description of a flag
	UsageTag = "usage"

	// DefaultTag is the struct tag used to
	// define the default value for the field
	// (if that default value is different from the zero value)
	DefaultTag = "default"

	// NameFunc is called as last operation for every flag name
	NameFunc = func(name string) string { return name }
)

Functions

func LoadFile

func LoadFile(filename string, structPtr interface{}) error

LoadFile loads a struct from a JSON or XML file. The file type is determined by the file extension.

func LoadFileAndParseCommandLine

func LoadFileAndParseCommandLine(filename string, structPtr interface{}) ([]string, error)

LoadFileAndParseCommandLine loads the configuration from filename into structPtr and then parses the command line. Every value that is present in command line overwrites the value loaded from the configuration file. Values not present in the command line won't effect the Values loaded from the configuration file. If there is an error loading the configuration file, then the command line still gets parsed. An error where os.IsNotExist(err) == true can be ignored if the existence of the configuration file is optional.

func LoadFileIfExistsAndMustParseCommandLine

func LoadFileIfExistsAndMustParseCommandLine(filename string, structPtr interface{}) []string

LoadFileIfExistsAndMustParseCommandLine same as LoadFileAndParseCommandLine but panics on error

func LoadJSON

func LoadJSON(filename string, structPtr interface{}) error

LoadJSON loads a struct from a JSON file

func LoadXML

func LoadXML(filename string, structPtr interface{}) error

LoadXML loads a struct from a XML file

func MustLoadFileAndParseCommandLine

func MustLoadFileAndParseCommandLine(filename string, structPtr interface{}) []string

MustLoadFileAndParseCommandLine same as LoadFileAndParseCommandLine but panics on error

func MustParseCommandLine

func MustParseCommandLine() []string

MustParseCommandLine without loading any configuration. Panics in case of an error.

func Parse

func Parse(args ...string) ([]string, error)

Parse parses args, or if no args are given os.Args[1:]

func ParseCommandLine

func ParseCommandLine() ([]string, error)

ParseCommandLine without loading any configuration

func PrintCommandsUsageIntro

func PrintCommandsUsageIntro(output io.Writer)

func PrintConfig

func PrintConfig(structPtr interface{})

PrintConfig prints the flattened struct fields from structPtr to Output.

func PrintUsage

func PrintUsage()

PrintUsage prints a description of all commands and flags of Set and Commands to Output

func PrintUsageTo

func PrintUsageTo(output io.Writer)

PrintUsageTo prints a description of all commands and flags of Set and Commands to output

func SaveJSON

func SaveJSON(filename string, structPtr interface{}, indent ...string) error

SaveJSON saves a struct as a JSON file

func SaveXML

func SaveXML(filename string, structPtr interface{}, indent ...string) error

SaveXML saves a struct as a XML file

func StructVar

func StructVar(structPtr interface{})

StructVar defines the fields of a struct as flags. structPtr must be a pointer to a struct. Anonoymous embedded fields are flattened. Struct fields with NameTag of "-" will be ignored.

Types

type CommandList

type CommandList []commandDetails

CommandList helps to parse and execute commands from command line arguments

func (*CommandList) Add

func (c *CommandList) Add(action func() error, command string, commandDesc ...string)

Add adds a command

func (*CommandList) AddDefault

func (c *CommandList) AddDefault(action func() error, commandDesc ...string)

AddDefault adds a command that is executed when no other command was specified on the command line.

func (*CommandList) AddWith2Args

func (c *CommandList) AddWith2Args(action func(string, string) error, command, argDesc string, commandDesc ...string)

AddWith2Args adds a command with two additional string argument

func (*CommandList) AddWith3Args

func (c *CommandList) AddWith3Args(action func(string, string, string) error, command, argDesc string, commandDesc ...string)

AddWith3Args adds a command with threee additional string argument

func (*CommandList) AddWith4Args

func (c *CommandList) AddWith4Args(action func(string, string, string, string) error, command, argDesc string, commandDesc ...string)

AddWith4Args adds a command with threee additional string argument

func (*CommandList) AddWith5Args

func (c *CommandList) AddWith5Args(action func(string, string, string, string, string) error, command, argDesc string, commandDesc ...string)

AddWith5Args adds a command with threee additional string argument

func (*CommandList) AddWithArg

func (c *CommandList) AddWithArg(action func(string) error, command, argDesc string, commandDesc ...string)

AddWithArg adds a command with a single additional string argument

func (*CommandList) AddWithArgs

func (c *CommandList) AddWithArgs(action func([]string) error, command, argDesc string, commandDesc ...string)

AddWithArgs adds a command with additional string arguments

func (*CommandList) Execute

func (c *CommandList) Execute(args []string) (command string, exeErr error)

Execute executes the command from args[0] and returns the executed command name and the error returned from the command function. The error is ErrNotEnoughArguments if args did not have enough extra arguments for the command. Returns ErrCommandNotFound if no matching command was found

func (*CommandList) PrintUsage

func (c *CommandList) PrintUsage()

PrintUsage prints a description of all commands to Output

type Flags

type Flags interface {
	Args() []string
	Parse(arguments []string) error
	PrintDefaults()

	BoolVar(p *bool, name string, value bool, usage string)
	DurationVar(p *time.Duration, name string, value time.Duration, usage string)
	Float64Var(p *float64, name string, value float64, usage string)
	Int64Var(p *int64, name string, value int64, usage string)
	IntVar(p *int, name string, value int, usage string)
	StringVar(p *string, name string, value string, usage string)
	Uint64Var(p *uint64, name string, value uint64, usage string)
	UintVar(p *uint, name string, value uint, usage string)
	Var(value pflag.Value, name string, usage string)
}

Flags is the minimal interface structflag needs to work. It is a subset of flag.FlagSet

type FlagsP

type FlagsP interface {
	Flags

	BoolVarP(p *bool, name, shorthand string, value bool, usage string)
	DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string)
	Float64VarP(p *float64, name, shorthand string, value float64, usage string)
	Int64VarP(p *int64, name, shorthand string, value int64, usage string)
	IntVarP(p *int, name, shorthand string, value int, usage string)
	StringVarP(p *string, name, shorthand string, value string, usage string)
	Uint64VarP(p *uint64, name, shorthand string, value uint64, usage string)
	UintVarP(p *uint, name, shorthand string, value uint, usage string)
	VarP(value pflag.Value, name, shorthand string, usage string)
}

FlagsP supports github.com/ogier/pflag

Directories

Path Synopsis
examples
cmdparse command
xmlfile command

Jump to

Keyboard shortcuts

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