gouick/helpers/input/bool.go
Matthieu 'JP' DERASSE dccbb30a85
All checks were successful
continuous-integration/drone/push Build is passing
feat(input): Full refacto of the user input system
2022-09-16 20:22:19 +00:00

30 lines
607 B
Go

package input
import (
"fmt"
"strings"
)
// YesOrNo ask user for a yes or no reply and try until we get a possible answer.
func YesOrNo() bool {
value := Ask(true, func(in string) (string, error) {
lUserInput := strings.ToLower(in)
for _, positiveAnswer := range []string{"yes", "y", "1", "true"} {
if lUserInput == positiveAnswer {
return "y", nil
}
}
for _, negativeAnswer := range []string{"no", "n", "0", "false"} {
if lUserInput == negativeAnswer {
return "n", nil
}
}
return "", fmt.Errorf("expecting a yes or no answer, try again")
})
return value == "y"
}