Documentation
¶
Overview ¶
Package responder provides a means of prompting for values and reading from the terminal. The terminal device will be put into a raw mode so that you can read single characters. The package offers a standard help feature and allows the caller to specify default values for the prompted value. The value entered will be checked against the list of valid entries.
Example ¶
package main import ( "fmt" "github.com/nickwells/cli.mod/cli/responder" ) func main() { r := responder.NewOrPanic( "Question", map[rune]string{ 'y': "to show differences", 'n': "to skip this file", 'q': "to quit", }, responder.SetDefault('y'), ) for { response := r.GetResponseOrDie() fmt.Println() switch response { case 'y': fmt.Println("Yes!") case 'n': fmt.Println("No.") case 'q': break } } }
Output:
Index ¶
- type FixedResponse
- type R
- func (r R) GetResponse() (response rune, err error)
- func (r R) GetResponseIndent(first, second int) (response rune, err error)
- func (r R) GetResponseIndentOrDie(first, second int) rune
- func (r R) GetResponseOrDie() rune
- func (r R) PrintHelp()
- func (r R) PrintHelpIndent(indent int)
- func (r R) PrintPrompt()
- func (r R) PrintValidResponses()
- type RespOptFunc
- type Responder
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FixedResponse ¶ added in v1.1.0
FixedResponse always returns the given response. This is expected to be useful for testing. Note that there are no checks made of the Response and so it is possible to have a response that cannot be made from the standard Responder such as an uppercase value or a whitespace character.
func (FixedResponse) GetResponse ¶ added in v1.1.0
func (fr FixedResponse) GetResponse() (rune, error)
GetResponse returns the fixed responses
func (FixedResponse) GetResponseIndent ¶ added in v1.1.0
func (fr FixedResponse) GetResponseIndent(_, _ int) (rune, error)
GetResponseIndent returns the fixed responses
func (FixedResponse) GetResponseIndentOrDie ¶ added in v1.1.0
func (fr FixedResponse) GetResponseIndentOrDie(_, _ int) rune
GetResponseIndentOrDie returns the fixed responses. It will exit if the Err field is not nil.
func (FixedResponse) GetResponseOrDie ¶ added in v1.1.0
func (fr FixedResponse) GetResponseOrDie() rune
GetResponseOrDie returns the fixed responses. It will exit if the Err field is not nil.
type R ¶
type R struct {
// contains filtered or unexported fields
}
R holds the details needed to collect and validate a response
func NewOrPanic ¶
func NewOrPanic( prompt string, responses map[rune]string, opts ...RespOptFunc, ) *R
NewOrPanic creates a new responder and panics if there are any errors
func (R) GetResponse ¶
GetResponse will print the prompt and read a single rune from standard input. It will check that the rune is a valid response. If it is not in the set of valid responses it will print an error message and reprompt. It will do this maxReprompts times before returning an error.
If an error is detected the response returned will be the unicode ReplacementChar.
func (R) GetResponseIndent ¶ added in v1.1.0
GetResponseIndent behaves as GetResponse but the indents are taken from the parameters rather than the responder.
func (R) GetResponseIndentOrDie ¶ added in v1.1.0
GetResponseIndentOrDie calls GetResponseIndent to get the response but if there is an error it will print it and exit with status 1.
func (R) GetResponseOrDie ¶
GetResponseOrDie calls GetResponse to get the response but if there is an error it will print it and exit with status 1.
func (R) PrintHelpIndent ¶ added in v1.1.0
PrintHelpIndent prints the help message.
func (R) PrintPrompt ¶
func (r R) PrintPrompt()
PrintPrompt prints the prompt and any valid responses.
Example (NoDefault) ¶
This example shows the text that will be printed to prompt the user to respond
package main import ( "github.com/nickwells/cli.mod/cli/responder" ) func main() { r := responder.NewOrPanic( "Delete File", map[rune]string{ 'y': "delete the file", 'n': "leave the file alone", }, ) r.PrintPrompt() }
Output: Delete File? (n/y/?):
Example (WithDefault) ¶
This example shows the text that will be printed to prompt the user to respond. In this example a default response is given
package main import ( "github.com/nickwells/cli.mod/cli/responder" ) func main() { r := responder.NewOrPanic( "Delete File", map[rune]string{ 'y': "delete the file", 'n': "leave the file alone", }, responder.SetDefault('y'), ) r.PrintPrompt() }
Output: Delete File? ([y]/n/?):
func (R) PrintValidResponses ¶
func (r R) PrintValidResponses()
PrintValidResponses prints the valid response runes separated by a slash.
A rune matching the default is shown in brackets (like so: [y]).
type RespOptFunc ¶
RespOptFunc is a function which can be passed to the New function to set optional parts of the R
func SetDefault ¶
func SetDefault(d rune) RespOptFunc
SetDefault sets the default value for a Responder
func SetIndents ¶
func SetIndents(indentFirst, indent int) RespOptFunc
SetIndents sets the indents for the first and subsequent lines of output
func SetMaxReprompts ¶
func SetMaxReprompts(maximum int) RespOptFunc
SetMaxReprompts sets the maximum number of times that the user will be reprompted for a valid response before reporting an error. The value must be greater than 0