-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.go
65 lines (57 loc) · 1.22 KB
/
input.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func parseInput() string {
buf := bufio.NewReader(os.Stdin)
fmt.Print("> ")
sentence, err := buf.ReadBytes('\n')
if err != nil {
print(say.DEFAULT(err))
}
return strings.Trim(string(sentence), "\n")
}
func getCommandAndArgs() (command, args string) {
sentence := parseInput()
argsArray := strings.Split(string(sentence), " ")
//remove any empty strings at beginning
for len(argsArray) > 0 && argsArray[0] == "" {
argsArray = argsArray[1:]
}
if len(argsArray) < 1 {
return
}
command = strings.ToUpper(argsArray[0])
args = strings.Join(argsArray[1:], " ")
return
}
func getYesOrNo() (answer string) {
for answer != YES && answer != NO {
print(say.DIRECTION("Please send YES or NO "))
answer = strings.ToUpper(parseInput())
}
return
}
func getBackResponse() (answer bool) {
answer = strings.ToUpper(parseInput()) == BACK
return
}
func getInteger() (answer int) {
noGoodAnswer := true
var err error
for noGoodAnswer {
print(say.DIRECTION("Please send a valid whole number"))
answerStr := parseInput()
answer, err = strconv.Atoi(answerStr)
if err == nil {
noGoodAnswer = false
} else {
print(say.WARNING(err))
}
}
return
}