cmd

package
v0.0.0-...-e623927 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2018 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

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

func NewContext(stdin io.Reader, stdout, stderr io.Writer) *Context

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) AskBool

func (c *Context) AskBool(question string, defaultAnswer string) bool

AskBool asks a question an expect a yes/no answer.

func (*Context) AskChoice

func (c *Context) AskChoice(question string, choices []string, defaultAnswer string) string

AskChoice asks the user to select between a set of choices

func (*Context) AskInt

func (c *Context) AskInt(question string, min int64, max int64, defaultAnswer string) int64

AskInt asks the user to enter an integer between a min and max value

func (*Context) AskPassword

func (c *Context) AskPassword(question string, reader func(int) ([]byte, error)) string

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) AskPasswordOnce

func (c *Context) AskPasswordOnce(question string, reader func(int) ([]byte, error)) string

AskPasswordOnce asks the user to enter a password.

It's the same as AskPassword, but it won't ask to enter it again.

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.

func (*Context) Error

func (c *Context) Error(format string, a ...interface{})

Error prints a message on standard error.

func (*Context) InputYAML

func (c *Context) InputYAML(out interface{}) error

InputYAML treats stdin as YAML content and returns the unmarshalled structure

func (*Context) Output

func (c *Context) Output(format string, a ...interface{})

Output prints a message on standard output.

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

type Parser struct {
	Context      *Context
	UsageMessage string
	ExitOnError  bool
}

Parser for command line arguments.

func NewParser

func NewParser(context *Context, usage string) *Parser

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

func (p *Parser) Parse(line []string, args interface{}) error

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.

Jump to

Keyboard shortcuts

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