Documentation ¶
Index ¶
- Constants
- Variables
- func Ask(prompt string, validate func(s string) error)
- func AutoYN(v YNAuto)
- func Choose(prompt string, choices ...string) int
- func Enum(promptPrefix string, choices ...string) string
- func Int(prompt string, min, max int) int
- func NoEmptyString(s string) error
- func NoSpaces(s string) error
- func Text(prompt string, validate ...func(string) error) string
- func TextTrimmed(prompt string, validate ...func(string) error) string
- func YN(prompt string, policies ...YNPolicy) bool
- type YNAuto
- type YNPolicy
Constants ¶
const ( AlwaysAsk = YNAuto(iota) // always prompt AssumeY // ignore prompt, automatically assume yes AssumeN // ignore prompt, automatically assume no )
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 ¶
var ErrEmptyStringNotAllowed = errors.New("empty string is not allowed")
predefined validator errors
var ErrInvalidInput = errors.New("invalid input")
var ErrSpacesNotAllowed = errors.New("spaces are not allowed within the string")
Functions ¶
func Choose ¶
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 ¶
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 ¶
Int shows a prompt and processes user input as a typed integer value, accepting values between min and max
func NoEmptyString ¶
NoEmptyString can be used as validating callback for Text and TextTrimmed calls
func Text ¶
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 ¶
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 ¶
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