prompter

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2019 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package prompter is a simple user prompter that asks users for input data or confirmations.

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultConfirmationChoices is the default value for the choices that are given to
	// the users in a confirmation prompt.
	DefaultConfirmationChoices = []string{"y", "n"}
)

Functions

func DefaultConfirmation

func DefaultConfirmation(input string) (bool, error)

DefaultConfirmation is a confirmation parser that covers most cases for confirmation. It converts y/Y/yes/YES/t/T/true/True/1 to true. It converts n/N/no/NO/f/F/false/FALSE/0 to false.

Types

type Confirmation

type Confirmation struct {
	// The label that will be prompted to the user.
	// Example: `Are you sure?`
	Label string

	// The choices that will be presented to the user.
	// Example: `Y/n`. (A good practice is to uppercase
	// the default value, if there is one).
	Choices []string

	// EnableDefaultValue tells the prompter whether or not
	// there is a default value that will be used when the
	// user doesn't input any data.
	EnableDefaultValue bool

	// DefaultValue is the default value that will be used when
	// the user doesn't input any data, if EnableDefaultValue
	// is set to true OR that the prompter is set to not
	// interactive.
	DefaultValue bool

	// The parser that will be used to convert the user's input
	// into a true/false value.
	Parser ConfirmationParser
}

Confirmation represents a confirmation prompt's configuration.

type ConfirmationParser

type ConfirmationParser func(string) (bool, error)

ConfirmationParser is a function that parses an input and returns a confirmation value as well as an error, if the input can't be parsed.

type Prompter

type Prompter struct {
	// contains filtered or unexported fields
}

Prompter prompts users to let them input data, and parses it.

func New

func New(w io.Writer, r io.Reader, interactive bool) *Prompter

New instantiates a new prompter which will prompt users on the writer and read their output from the reader. The interactive boolean makes all prompts return a default value if set to false, and won't prompt them. This should be used if your users are not in a TTY and can't write to answer to the prompt.

func (Prompter) Confirm

func (p Prompter) Confirm(config Confirmation) (bool, error)

Confirm prompts the user to confirm something.

Jump to

Keyboard shortcuts

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