Skip to content

Commit c01692a

Browse files
committed
prompt: Add prompt_for_yes_or_no
1 parent 0e23579 commit c01692a

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

lib/prompt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
# @go.prompt_for_input
1010
# Prompts the user for a line of input
1111
#
12+
# @go.prompt_for_yes_or_no
13+
# Prompts the user for a yes or no response
14+
#
1215
# @go.select_option
1316
# Prompts the user to select one item from a list of options
1417

@@ -58,6 +61,53 @@
5861
fi
5962
}
6063

64+
# Prompts the user for a yes or no response
65+
#
66+
# Arguments:
67+
# prompt Text prompt for user input
68+
# default (Optional) Default response; must be 'yes' or 'no'
69+
#
70+
# Returns:
71+
# Zero on 'y' or 'yes' (case- and space- insensitive), nonzero otherwise
72+
@go.prompt_for_yes_or_no() {
73+
local prompt="$1"
74+
local default="$2"
75+
local response
76+
77+
case "$default" in
78+
yes)
79+
@go.printf '%s [Y/n] ' "$prompt" >&2
80+
;;
81+
no)
82+
@go.printf '%s [y/N] ' "$prompt" >&2
83+
;;
84+
'')
85+
@go.printf '%s [y/n] ' "$prompt" >&2
86+
;;
87+
*)
88+
@go.printf 'Invalid `default` parameter "%s" for %s at:\n' \
89+
"$default" "$FUNCNAME" >&2
90+
@go.print_stack_trace '1' >&2
91+
exit 1
92+
;;
93+
esac
94+
95+
while true; do
96+
@go.read_prompt_response 'response' "$default"
97+
98+
if [[ "$response" =~ ^[Yy]([Ee][Ss])?$ ]]; then
99+
return 0
100+
elif [[ "$response" =~ ^[Nn]([Oo])?$ ]]; then
101+
return 1
102+
else
103+
if [[ -n "$response" ]]; then
104+
@go.printf '\n"%s" is an invalid response.\n' "$response" >&2
105+
fi
106+
@go.printf '\nPlease answer Y(es) or N(o): ' >&2
107+
fi
108+
done
109+
}
110+
61111
# Prompts the user to select one item from a list of options.
62112
#
63113
# This is a thin wrapper around the `select` builtin command for
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#! /usr/bin/env bats
2+
3+
load ../environment
4+
5+
setup() {
6+
test_filter
7+
@go.create_test_go_script '. "$_GO_USE_MODULES" "prompt"' \
8+
'declare default="$1"' \
9+
'if @go.prompt_for_yes_or_no "To be or not to be?" "$default"; then' \
10+
" printf \"\n'Tis nobler to suffer the slings and arrows of fortune!\n\"" \
11+
'else' \
12+
" printf \"\n'Tis nobler to take up arms against a sea of troubles!\n\"" \
13+
'fi'
14+
}
15+
16+
teardown() {
17+
@go.remove_test_go_rootdir
18+
}
19+
20+
@test "$SUITE: error if default value invalid" {
21+
run "$TEST_GO_SCRIPT" 'foobar'
22+
assert_failure
23+
assert_lines_match \
24+
'^Invalid `default` parameter "foobar" for @go.prompt_for_yes_or_no at:' \
25+
"^ $TEST_GO_SCRIPT:[0-9] main$"
26+
}
27+
28+
@test "$SUITE: default to yes" {
29+
run "$TEST_GO_SCRIPT" 'yes' <<<''
30+
assert_success 'To be or not to be? [Y/n] ' \
31+
"'Tis nobler to suffer the slings and arrows of fortune!"
32+
33+
run "$TEST_GO_SCRIPT" 'yes' <<<'no'
34+
assert_success 'To be or not to be? [Y/n] ' \
35+
"'Tis nobler to take up arms against a sea of troubles!"
36+
}
37+
38+
@test "$SUITE: default to no" {
39+
run "$TEST_GO_SCRIPT" 'no' <<<''
40+
assert_success 'To be or not to be? [y/N] ' \
41+
"'Tis nobler to take up arms against a sea of troubles!"
42+
43+
run "$TEST_GO_SCRIPT" 'no' <<<'yes'
44+
assert_success 'To be or not to be? [y/N] ' \
45+
"'Tis nobler to suffer the slings and arrows of fortune!"
46+
}
47+
48+
@test "$SUITE: default to neither, prompt for an answer if first is empty" {
49+
run "$TEST_GO_SCRIPT" <<<'yes'
50+
assert_success 'To be or not to be? [y/n] ' \
51+
"'Tis nobler to suffer the slings and arrows of fortune!"
52+
}
53+
54+
@test "$SUITE: prompt repeatedly until valid answer given" {
55+
run "$TEST_GO_SCRIPT" <<<$'\nfoobar\nyes'
56+
assert_success 'To be or not to be? [y/n] ' \
57+
'Please answer Y(es) or N(o): ' \
58+
'"foobar" is an invalid response.' \
59+
'' \
60+
'Please answer Y(es) or N(o): ' \
61+
"'Tis nobler to suffer the slings and arrows of fortune!"
62+
}
63+
64+
@test "$SUITE: match 'yes' patterns, trim input" {
65+
run "$TEST_GO_SCRIPT" <<<' yes '
66+
assert_success 'To be or not to be? [y/n] ' \
67+
"'Tis nobler to suffer the slings and arrows of fortune!"
68+
69+
run "$TEST_GO_SCRIPT" <<<' Yes '
70+
assert_success 'To be or not to be? [y/n] ' \
71+
"'Tis nobler to suffer the slings and arrows of fortune!"
72+
73+
run "$TEST_GO_SCRIPT" <<<' YES '
74+
assert_success 'To be or not to be? [y/n] ' \
75+
"'Tis nobler to suffer the slings and arrows of fortune!"
76+
77+
run "$TEST_GO_SCRIPT" <<<' y '
78+
assert_success 'To be or not to be? [y/n] ' \
79+
"'Tis nobler to suffer the slings and arrows of fortune!"
80+
81+
run "$TEST_GO_SCRIPT" <<<' Y '
82+
assert_success 'To be or not to be? [y/n] ' \
83+
"'Tis nobler to suffer the slings and arrows of fortune!"
84+
}
85+
86+
@test "$SUITE: match 'no' patterns, trim input" {
87+
run "$TEST_GO_SCRIPT" <<<' no '
88+
assert_success 'To be or not to be? [y/n] ' \
89+
"'Tis nobler to take up arms against a sea of troubles!"
90+
91+
run "$TEST_GO_SCRIPT" <<<' No '
92+
assert_success 'To be or not to be? [y/n] ' \
93+
"'Tis nobler to take up arms against a sea of troubles!"
94+
95+
run "$TEST_GO_SCRIPT" <<<' NO '
96+
assert_success 'To be or not to be? [y/n] ' \
97+
"'Tis nobler to take up arms against a sea of troubles!"
98+
99+
run "$TEST_GO_SCRIPT" <<<' n '
100+
assert_success 'To be or not to be? [y/n] ' \
101+
"'Tis nobler to take up arms against a sea of troubles!"
102+
103+
run "$TEST_GO_SCRIPT" <<<' N '
104+
assert_success 'To be or not to be? [y/n] ' \
105+
"'Tis nobler to take up arms against a sea of troubles!"
106+
}

0 commit comments

Comments
 (0)