Skip to content
Snippets Groups Projects
Commit 26d3a00b authored by Tamer Tas's avatar Tamer Tas
Browse files

Add input scanning utilities

parent f363298b
No related branches found
No related tags found
No related merge requests found
package inpututil
import (
"fmt"
"strings"
)
var (
booleanValues = map[string]bool{
"y": true,
"yes": true,
"yup": true,
"n": false,
"no": false,
"nope": false,
}
)
func ScanYesOrNo(msg string, def bool) (bool, error) {
var choices string
if def {
choices = "Y/n"
} else {
choices = "y/N"
}
fmt.Print(msg, " ", choices, " ")
var choice string
_, err := fmt.Scanf("%s", &choice)
if err != nil {
return def, err
}
switch val, ok := booleanValues[strings.ToLower(choice)]; {
case ok:
return val, nil
case choice == "":
return def, nil
}
return def, fmt.Errorf("unrecognized choice %#q", choice)
}
func ScanValue(msg string) (string, error) {
fmt.Print(msg)
var value string
_, err := fmt.Scanf("%s", &value)
if err != nil {
return "", err
}
return value, nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment