Documentation ¶
Index ¶
- type Context
- func (c *Context) AskBool(question string, defaultAnswer string) bool
- func (c *Context) AskChoice(question string, choices []string, defaultAnswer string) string
- func (c *Context) AskInt(question string, min int64, max int64, defaultAnswer string) int64
- func (c *Context) AskPassword(question string, reader func(int) ([]byte, error)) string
- func (c *Context) AskString(question string, defaultAnswer string, validate func(string) error) string
- func (c *Context) InputYAML(out interface{}) error
- func (c *Context) Output(format string, a ...interface{})
- type MemoryStreams
- func (s *MemoryStreams) Err() string
- func (s *MemoryStreams) InputAppend(text string)
- func (s *MemoryStreams) InputAppendBoolAnswer(answer bool)
- func (s *MemoryStreams) InputAppendLine(line string)
- func (s *MemoryStreams) InputRead() string
- func (s *MemoryStreams) InputReset(input string)
- func (s *MemoryStreams) Out() string
- type Parser
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context captures the environment the sub-command is being run in, such as in/out/err streams and command line arguments.
func DefaultContext ¶
func DefaultContext() *Context
DefaultContext returns a new Context connected the stdin, stdout and stderr streams.
func NewContext ¶
NewContext creates a new command context with the given parameters.
func NewMemoryContext ¶
func NewMemoryContext(streams *MemoryStreams) *Context
NewMemoryContext creates a new command Context using the given in-memory streams.
func (*Context) AskPassword ¶
AskPassword asks the user to enter a password. The reader function used to read the password without echoing characters must be passed (usually terminal.ReadPassword from golang.org/x/crypto/ssh/terminal).
func (*Context) AskString ¶
func (c *Context) AskString(question string, defaultAnswer string, validate func(string) error) string
AskString asks the user to enter a string, which optionally conforms to a validation function.
type MemoryStreams ¶
type MemoryStreams struct {
// contains filtered or unexported fields
}
MemoryStreams provide an in-memory version of the system stdin/stdout/stderr streams.
func NewMemoryStreams ¶
func NewMemoryStreams(input string) *MemoryStreams
NewMemoryStreams creates a new set of in-memory streams with the given user input.
func (*MemoryStreams) Err ¶
func (s *MemoryStreams) Err() string
Err returns the current content of the err stream.
func (*MemoryStreams) InputAppend ¶
func (s *MemoryStreams) InputAppend(text string)
InputAppend adds the given text to the current input.
func (*MemoryStreams) InputAppendBoolAnswer ¶
func (s *MemoryStreams) InputAppendBoolAnswer(answer bool)
InputAppendBoolAnswer adds a new "yes" or "no" line depending on the answer.
func (*MemoryStreams) InputAppendLine ¶
func (s *MemoryStreams) InputAppendLine(line string)
InputAppendLine adds a single line to the input stream.
func (*MemoryStreams) InputRead ¶
func (s *MemoryStreams) InputRead() string
InputRead returns the current input string.
func (*MemoryStreams) InputReset ¶
func (s *MemoryStreams) InputReset(input string)
InputReset replaces the data in the input stream.
func (*MemoryStreams) Out ¶
func (s *MemoryStreams) Out() string
Out returns the current content of the out stream.
type Parser ¶
Parser for command line arguments.
func NewParser ¶
NewParser returns a Parser connected to the given I/O context and printing the given usage message when '--help' or '-h' are passed.
func (*Parser) Parse ¶
Parse a command line populating the given args object accordingly.
The command line format is expected to be:
<cmd> [subcmd [params]] [flags] [-- [extra]]
The args object may have Subcommand, Params and Extra attributes (respectively of type string, []string and []string), which will be populated with the subcommand, its params and any extra argument (if present).
The type of the args object must have one attribute for each supported command line flag, annotated with a tag like `flag:"<name>"`, where <name> is the name of the command line flag.
In case of parsing error (e.g. unknown command line flag) the default behavior is to call os.Exit() with a non-zero value. This can be disabled by setting the ExitOnError attribute to false, in which case the error will be returned.