Documentation
¶
Index ¶
- Constants
- Variables
- func LoadFile(filename string, structPtr interface{}) error
- func LoadFileAndParseCommandLine(filename string, structPtr interface{}) ([]string, error)
- func LoadFileIfExistsAndMustParseCommandLine(filename string, structPtr interface{}) []string
- func LoadJSON(filename string, structPtr interface{}) error
- func LoadXML(filename string, structPtr interface{}) error
- func MustLoadFileAndParseCommandLine(filename string, structPtr interface{}) []string
- func MustParseCommandLine() []string
- func Parse(args ...string) ([]string, error)
- func ParseCommandLine() ([]string, error)
- func PrintCommandsUsageIntro(output io.Writer)
- func PrintConfig(structPtr interface{})
- func PrintUsage()
- func PrintUsageTo(output io.Writer)
- func SaveJSON(filename string, structPtr interface{}, indent ...string) error
- func SaveXML(filename string, structPtr interface{}, indent ...string) error
- func StructVar(structPtr interface{})
- type CommandList
- func (c *CommandList) Add(action func() error, command string, commandDesc ...string)
- func (c *CommandList) AddDefault(action func() error, commandDesc ...string)
- func (c *CommandList) AddWith2Args(action func(string, string) error, command, argDesc string, ...)
- func (c *CommandList) AddWith3Args(action func(string, string, string) error, command, argDesc string, ...)
- func (c *CommandList) AddWith4Args(action func(string, string, string, string) error, command, argDesc string, ...)
- func (c *CommandList) AddWith5Args(action func(string, string, string, string, string) error, ...)
- func (c *CommandList) AddWithArg(action func(string) error, command, argDesc string, commandDesc ...string)
- func (c *CommandList) AddWithArgs(action func([]string) error, command, argDesc string, commandDesc ...string)
- func (c *CommandList) Execute(args []string) (command string, exeErr error)
- func (c *CommandList) PrintUsage()
- type Flags
- type FlagsP
Constants ¶
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 ¶
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) )
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 } )
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 ¶
LoadFile loads a struct from a JSON or XML file. The file type is determined by the file extension.
func LoadFileAndParseCommandLine ¶
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 ¶
LoadFileIfExistsAndMustParseCommandLine same as LoadFileAndParseCommandLine but panics on error
func MustLoadFileAndParseCommandLine ¶
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 ParseCommandLine ¶
ParseCommandLine without loading any configuration
func PrintCommandsUsageIntro ¶
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 ¶
PrintUsageTo prints a description of all commands and flags of Set and Commands to output
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