-
Notifications
You must be signed in to change notification settings - Fork 1
Custom Prompt Types
Defining a custom prompt type is a simple as just creating a new Prompt
instance and configuring the parameters to match your requirements.
As a simple example we will do a super quick run through through of the built-in YesNoPrompt
.
The Prompt
itself is defined pretty simply:
func YesNoPrompt(message string, hint string, required Optionality,
defaultValue bool) Prompt
Prompt{
Message: message,
ValueHint: hint,
DefaultValue: defaultValue,
Required: required,
RetryIfNoMatch: true,
FailIfNoMatch: true,
CaseSensitiveMatch: false,
ValidationFunc: func(prompt *Prompt, value string) (string, Validity) {
_, validity := isValidYesOrNoValue(value)
return value, validity
},
ValueConverter: func(prompt *Prompt, value string) interface{} {
return isPositiveStringValue(value, false)
},
}
The display options (Message
and Hint
) and the DefaultValue
are provided through the functional call, but the validation and conversion functions are designed to match the entered value to a collection of valid yes or no responses and in the end the value is converted to a bool
value.
The other parameters are configured so that the matching and failure logic lines up with the appropriate user experience.
To dig deeper, take a look at the built-in Prompt
configurations in the prompts.go file.