prompt

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2022 License: MIT Imports: 5 Imported by: 2

Documentation

Index

Constants

View Source
const (
	AlwaysAsk = YNAuto(iota) // always prompt
	AssumeY                  // ignore prompt, automatically assume yes
	AssumeN                  // ignore prompt, automatically assume no
)
View Source
const (
	NoAuto   = YNPolicy(1 << iota) // ignore YNAuto, always show prompt
	DefaultY                       // assume 'y' when user provides empty input (hits RETURN key without typing anything)
	DefaultN                       // assume 'n' when user provides empty input (hits RETURN key without typing anything)
)

Variables

View Source
var ErrEmptyStringNotAllowed = errors.New("empty string is not allowed")

predefined validator errors

View Source
var ErrInvalidInput = errors.New("invalid input")
View Source
var ErrSpacesNotAllowed = errors.New("spaces are not allowed within the string")

Functions

func Ask

func Ask(prompt string, validate func(s string) error)

Ask is a show a generic prompt asking for user input, with validation

func AutoYN

func AutoYN(v YNAuto)

AutoYN configures automatic YN answers for non-interactive runs

func Choose

func Choose(prompt string, choices ...string) int

Choose shows a prompt as:

<prompt>
1: <choices[0]>
2: <choices[1]>
...
N: <choices[N-1]>
type a number [1...N]: _

Then it awaits user input and returns the numeric value within 1...N range

Notice, that the numbers shown and the returned values are 1...N, (not 0...N-1)

func Enum

func Enum(promptPrefix string, choices ...string) string

Enum shows a prompt as:

<promptPrefix> [<choices[0]>/<choices[1]>/.../<choices[N-1]>]: _

Then it awaits user input as text value and returns the matching chose as a string

func Int

func Int(prompt string, min, max int) int

Int shows a prompt and processes user input as a typed integer value, accepting values between min and max

func NoEmptyString

func NoEmptyString(s string) error

NoEmptyString can be used as validating callback for Text and TextTrimmed calls

func NoSpaces

func NoSpaces(s string) error

func Text

func Text(prompt string, validate ...func(string) error) string

Text shows a prompt as:

<prompt> _

Then it awaits user input as text, validates it with the provided validation callbacks and returns the typed value if validation was successful.

func TextTrimmed

func TextTrimmed(prompt string, validate ...func(string) error) string

TextTrimmed works similarly to Text, but trims spaces from typed string before it is validated and returned

some of the validators are defined below

note that you can also use Validate* functions from filesystem.stats

func YN

func YN(prompt string, policies ...YNPolicy) bool

YN displays a y/n prompt, returning a boolean value after the user types a response - 'y'|'yes' -> returns true - 'n'|'no' -> returns false

Automatic answers are configured globally with AutoYN() and can be overriden with NoAuto policy

Default answers (when user hits RETURN key without typing anything) can be configured with DefaultY or DefaultN policy

Types

type YNAuto

type YNAuto int

YNAuto allows to configure global automatic answers to YN, this may be useful for cli applications that expose '-y' or '-n' flags to bypass interactive Y/N choices (see AutoYN function)

type YNPolicy

type YNPolicy int

YNPolicy provides tuning options for AskYN prompts

Jump to

Keyboard shortcuts

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