forked from manifoldco/promptui
-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_main_test.go
54 lines (44 loc) · 1.21 KB
/
example_main_test.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
package promptui
import (
"errors"
"fmt"
"strconv"
)
// This is an example for the Prompt mode of promptui. In this example, a prompt is created
// with a validator function that validates the given value to make sure its a number.
// If successful, it will output the chosen number in a formatted message.
func Example_prompt() {
validate := func(input string) error {
_, err := strconv.ParseFloat(input, 64)
if err != nil {
return errors.New("Invalid number")
}
return nil
}
prompt := Prompt{
Label: "Number",
Validate: validate,
}
result, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
fmt.Printf("You choose %q\n", result)
}
// This is an example for the Select mode of promptui. In this example, a select is created with
// the days of the week as its items. When an item is selected, the selected day will be displayed
// in a formatted message.
func Example_select() {
prompt := Select{
Label: "Select Day",
Items: []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday", "Sunday"},
}
_, result, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
fmt.Printf("You choose %q\n", result)
}