Documentation ¶
Overview ¶
Package gocommander provides a command line interface making your tool developments easier and happier.
Index ¶
- Variables
- func CreateFile(name FilePath) (*os.File, error)
- func DecompressFile(file *os.File, cache bool, logger *golog.Logger) (*os.File, error)
- func DownloadAndDecompressFile(dirpath FilePath, rawurl string, cache bool, logger *golog.Logger) (*os.File, error)
- func DownloadFile(dirpath FilePath, rawurl string, cache bool, logger *golog.Logger) (*os.File, error)
- type BoxString
- type Command
- type Commander
- func (commander *Commander) Add(name string, cmd Command) *Context
- func (commander *Commander) Copyright() string
- func (commander *Commander) Get(name string) *Context
- func (commander *Commander) Help()
- func (commander *Commander) Logger() *golog.Logger
- func (commander *Commander) Name() string
- func (commander *Commander) Parse(args []string) (int, error)
- func (commander *Commander) Reset()
- func (commander *Commander) Run() error
- type Context
- func (ctx *Context) AddOption(name string, opt Option, description string)
- func (ctx *Context) GetOption(name string) Option
- func (ctx *Context) Help(cmdname string)
- func (ctx *Context) Logger() *golog.Logger
- func (ctx *Context) OptionsString() string
- func (ctx *Context) Parse(args []string) (int, error)
- type FilePath
- type Option
- type OptionBool
- type OptionString
- type Settings
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultName is the default value of CommanderSettings.Name. DefaultName = "An go-commander application" // DefaultCopyright is the default value of CommanderSettings.Copyright. DefaultCopyright = "Copyright 2018- Tatsuhiro Aoshima (hiro4bbh@gmail.com)." )
Functions ¶
func CreateFile ¶
CreateFile creates the parent directories if needed, creates a new file, and returns the *os.File.
This function returns an error in file operations.
func DecompressFile ¶
DecompressFile returns a decompressed file. This function automatically closes the given file if returning the decompressed file. If the file does not exist or cache is false, then this function decompress the file. Currently, this function supports gzip (extension ".gz").
This function returns an error in decompression.
Types ¶
type BoxString ¶
type BoxString struct {
// contains filtered or unexported fields
}
BoxString represents the states BoxString(str) or BoxString(none).
func NewBoxString ¶
func NewBoxString(o interface{}) BoxString
NewBoxString returns a new BoxString.
func (BoxString) UnwrapFilePath ¶
UnwrapFilePath returns the FilePath string if ok, otherwise returns an error.
func (BoxString) UnwrapFilePathOr ¶
UnwrapFilePathOr returns the FilePath string if ok, otherwise returns defval.
type Command ¶
type Command interface { // Description returns the command description. Description() string // Init is called, and initializes the command options at the command added to a commander. Init(ctx *Context) // Run is called on the command run by a commander. // // This function can return an error in running. Run(ctx *Context) error }
Command is the command interface.
type Commander ¶
type Commander struct {
// contains filtered or unexported fields
}
Commander is a manager of commands.
func New ¶
New returns a new Commander with the given CommanderSettings. If settings is nil, then the fields are filled with the default values. If a field of settings is zero value, then the field is filled with the corresponding default value.
func (*Commander) Add ¶
Add adds a new command with the given command name and command handlers, and returns its Context.
This function calls panic if the given command name is used or the given command name is illegal. The illegal command names are "h", "help", or one ending with "+" or "-" or "=".
func (*Commander) Help ¶
func (commander *Commander) Help()
Help writes the help message to the writer of the commander's logger.
func (*Commander) Logger ¶
func (commander *Commander) Logger() *golog.Logger
Logger returns the commander's logger.
func (*Commander) Parse ¶
Parse parses the given command line arguments, and returns the next argument index.
This function returns an error in parsing.
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context is the command instance with a ContextHandlers.
func (*Context) AddOption ¶
AddOption adds the given opt with the given name.
This function calls panic if the given name is already used or the given name is illegal. The illegal name are "h" or "help".
func (*Context) Logger ¶
func (ctx *Context) Logger() *golog.Logger
Logger returns the command's logger.
func (*Context) OptionsString ¶
OptionsString returns the string representation of options.
type FilePath ¶
type FilePath string
FilePath is the string with filepath methods.
type Option ¶
type Option interface { // Set sets the value parsed from the given str, and returns error if occurred. // str has the form "", "+", "-", or "=$VALUE". Set(str string) error // String returns the string representation of the option value. String() string // ValueFormat returns the option value format. ValueFormat() string }
Option is the interface for option values.
type OptionBool ¶
type OptionBool struct {
// contains filtered or unexported fields
}
OptionBool is Option having a bool variable.
func NewOptionBool ¶
func NewOptionBool(value bool) *OptionBool
NewOptionBool returns a new OptionBool with the given default value.
func (*OptionBool) ValueFormat ¶
func (opt *OptionBool) ValueFormat() string
ValueFormat is for interface Option.
type OptionString ¶
type OptionString struct {
// contains filtered or unexported fields
}
OptionString is Option having a string variable.
func NewOptionString ¶
func NewOptionString(value string) *OptionString
NewOptionString returns a new OptionString with the given default value.
func (*OptionString) Set ¶
func (opt *OptionString) Set(str string) error
Set is for interface Option.
func (*OptionString) String ¶
func (opt *OptionString) String() string
String is for interface Option.
func (*OptionString) ValueFormat ¶
func (opt *OptionString) ValueFormat() string
ValueFormat is for interface Option.