interact

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2022 License: MIT Imports: 7 Imported by: 130

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotANumber = errors.New("not a number")

ErrNotANumber is used internally by Resolve when the user enters a bogus value when resolving into an int.

Resolve will retry on this error; it is only exposed so you can know where the string is coming from.

View Source
var ErrNotBoolean = errors.New("not y, n, yes, or no")

ErrNotBoolean is used internally by Resolve when the user enters a bogus value when resolving into a bool.

Resolve will retry on this error; it is only exposed so you can know where the string is coming from.

Functions

This section is empty.

Types

type Choice

type Choice struct {
	Display string
	Value   interface{}
}

Choice is used to allow the user to select a value of an arbitrary type. Its Display value will be shown in a listing during Resolve, and if its entry in the list is chosen, the Value will be used to populate the destination.

type Interaction

type Interaction struct {
	Prompt  string
	Choices []Choice

	Input  io.Reader
	Output io.Writer
}

Interaction represents a single question to ask, optionally with a set of choices to limit the answer to.

func NewInteraction

func NewInteraction(prompt string, choices ...Choice) Interaction

NewInteraction constructs an interaction with the given prompt, limited to the given choices, if any.

Defaults Input and Output to os.Stdin and os.Stderr, respectively.

func (Interaction) Resolve

func (interaction Interaction) Resolve(dst interface{}) error

Resolve prints the prompt, indicating the default value, and asks for the value to populate into the destination dst, which should be a pointer to a value to set.

The default value is whatever value is currently held in dst, and will be shown in the prompt. Note that zero-values are valid defaults (e.g. false for a boolean prompt), so to disambiguate from having just allocated dst, and not intending its current zero-value to be the default, you must wrap it in a RequiredDestination.

If the choices are limited, the default value will be inferred by finding the value held in dst within the set of choices. The number corresponding to the choice will be the default value shown to the user. If no default is found, Resolve will require the user to make a selection.

The type of dst determines how the value is read. Currently supported types for the destination are int, string, bool, and any arbitrary value that is defined within the set of Choices.

Valid input strings for bools are "y", "n", "Y", "N", "yes", and "no". Integer values are parsed in base-10. String values will not include any trailing linebreak.

type NotAssignableError

type NotAssignableError struct {
	Destination reflect.Type
	Value       reflect.Type
}

NotAssignableError is returned by Resolve when the value present in the Choice the user selected is not assignable to the destination value during Resolve.

func (NotAssignableError) Error

func (err NotAssignableError) Error() string

type Password

type Password string

Password is a string whose value will not be echoed when the user's typing or when used as a default value.

type RequiredDestination

type RequiredDestination struct {
	Destination interface{}
}

RequiredDestination wraps the real destination and indicates to Resolve that a value must be explicitly provided, and that there is no default. This is to distinguish from defaulting to the zero-value.

func Required

func Required(dst interface{}) RequiredDestination

Required is a convenience function for constructing a RequiredDestination.

Jump to

Keyboard shortcuts

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