Lines 76-95
Link Here
|
76 |
exit 1; |
76 |
exit 1; |
77 |
} |
77 |
} |
78 |
|
78 |
|
79 |
=item ask_yesno($question, $default) |
79 |
=item ask_yesno($question, $default, $quiet_mode) |
80 |
|
80 |
|
81 |
Asks the user a yes/no question. Default to $default if they just |
81 |
Asks the user a yes/no question. Default to $default if they just |
82 |
press [return]. Returns 1 for a yes answer and 0 for no. |
82 |
press [return]. Returns 1 for a yes answer and 0 for no. |
83 |
|
83 |
|
|
|
84 |
If $quiet_mode is true the default answer is returned without |
85 |
asking for user input (useful for unattended execution where |
86 |
appropriate default values can be passed to this sub). |
87 |
|
88 |
Be careful when using ask_yesno in quiet mode: if you're asking |
89 |
confirmation for a potentially dangerous task either be sure |
90 |
to set the "forget it" answer as default or don't allow |
91 |
quiet mode at all. |
92 |
|
84 |
=cut |
93 |
=cut |
85 |
|
94 |
|
86 |
sub ask_yesno { |
95 |
sub ask_yesno { |
87 |
my ($question, $default) = @_; |
96 |
my ($question, $default, $quiet_mode) = @_; |
88 |
my $tries = 1; |
97 |
my $tries = 1; |
89 |
local $| = 1; |
98 |
local $| = 1; |
90 |
while (1) { |
99 |
while (1) { |
91 |
print $question; |
100 |
print $question; |
92 |
my $answer = <STDIN>; |
101 |
# just print a newline after the question to keep |
|
|
102 |
# output tidy, if we are in quiet mode |
103 |
print "\n" if $quiet_mode; |
104 |
|
105 |
my $answer= ''; |
106 |
# do not wait for user input if we are in quiet mode |
107 |
$answer = <STDIN> unless $quiet_mode; |
93 |
chomp($answer); |
108 |
chomp($answer); |
94 |
return $default if not length $answer; |
109 |
return $default if not length $answer; |
95 |
return 0 if $answer and $answer =~ /^no?$/i; |
110 |
return 0 if $answer and $answer =~ /^no?$/i; |
Lines 100-149
Link Here
|
100 |
} |
115 |
} |
101 |
|
116 |
|
102 |
|
117 |
|
103 |
=item ask_confirm($description, $ref_to_setting) |
118 |
=item ask_confirm($description, $ref_to_setting, $quiet_mode) |
104 |
|
119 |
|
105 |
Asks the user to confirm a setting. If they enter a new value asks |
120 |
Asks the user to confirm a setting. If they enter a new value asks |
106 |
"are you sure." Directly updates the setting and returns when done. |
121 |
"are you sure." Directly updates the setting and returns when done. |
107 |
|
122 |
|
108 |
A default setting of "NONE" will force the user to enter a value. |
123 |
A default setting of "NONE" will force the user to enter a value. |
109 |
|
124 |
|
|
|
125 |
If $quiet_mode is true the default answer is returned without |
126 |
asking for user input (useful for unattended execution where |
127 |
appropriate default values can be passed to this sub). |
128 |
|
129 |
The question is printed anyway, so that the default value appears |
130 |
in the output. |
131 |
|
110 |
=cut |
132 |
=cut |
111 |
|
133 |
|
112 |
sub ask_confirm { |
134 |
sub ask_confirm { |
113 |
my ($desc, $ref) = @_; |
135 |
my ($desc, $ref, $quiet_mode) = @_; |
114 |
my $tries = 1; |
136 |
my $tries = 1; |
115 |
local $| = 1; |
137 |
local $| = 1; |
116 |
while (1) { |
138 |
while (1) { |
117 |
print $desc, " [", $$ref, "] "; |
139 |
print $desc, " [", $$ref, "] "; |
118 |
my $answer = <STDIN>; |
140 |
# just print a newline after the question to keep |
|
|
141 |
# output tidy, if we are in quiet mode |
142 |
print "\n" if $quiet_mode; |
143 |
|
144 |
my $answer= ''; |
145 |
# do not wait for user input if we are in quiet mode |
146 |
$answer = <STDIN> unless $quiet_mode; |
119 |
chomp($answer); |
147 |
chomp($answer); |
120 |
if (not length $answer or $answer eq $$ref) { |
148 |
if (not length $answer or $answer eq $$ref) { |
|
|
149 |
if($quiet_mode and $$ref eq 'NONE') { |
150 |
print "No default is available for this question: ", |
151 |
"cannot continue quiet mode execution"; |
152 |
return; |
153 |
} |
121 |
return unless $$ref eq 'NONE'; |
154 |
return unless $$ref eq 'NONE'; |
122 |
print "No default is available for this question, ", |
155 |
print "No default is available for this question, ", |
123 |
"please enter a value.\n"; |
156 |
"please enter a value.\n"; |
124 |
next; |
157 |
next; |
125 |
} |
158 |
} |
126 |
if (ask_yesno("Are you sure you want to use '$answer'? [yes] ", 1)) { |
159 |
if (ask_yesno("Are you sure you want to use '$answer'? [yes] ", 1, $quiet_mode)) { |
127 |
$$ref = $answer; |
160 |
$$ref = $answer; |
128 |
return; |
161 |
return; |
129 |
} |
162 |
} |
130 |
} |
163 |
} |
131 |
} |
164 |
} |
132 |
|
165 |
|
133 |
=item ask_choice($question, [ "opt1", "opt2" ], "default") |
166 |
=item ask_choice($question, [ "opt1", "opt2" ], "default", $quiet_mode) |
134 |
|
167 |
|
135 |
Asks the user to choose from a list of options. Returns the option |
168 |
Asks the user to choose from a list of options. Returns the option |
136 |
selected. |
169 |
selected. |
137 |
|
170 |
|
|
|
171 |
If $quiet_mode is true the default answer is returned without |
172 |
asking for user input (useful for unattended execution where |
173 |
appropriate default values can be passed to this sub). |
174 |
|
175 |
The question is printed anyway, so that the default value appears |
176 |
in the output. |
177 |
|
138 |
=cut |
178 |
=cut |
139 |
|
179 |
|
140 |
sub ask_choice { |
180 |
sub ask_choice { |
141 |
my ($desc, $choices, $default) = @_; |
181 |
my ($desc, $choices, $default, $quiet_mode) = @_; |
142 |
my $tries = 1; |
182 |
my $tries = 1; |
143 |
local $| = 1; |
183 |
local $| = 1; |
144 |
while (1) { |
184 |
while (1) { |
145 |
print $desc, " [", $default, "] "; |
185 |
print $desc, " [", $default, "] "; |
146 |
my $answer = <STDIN>; |
186 |
# just print a newline after the question to keep |
|
|
187 |
# output tidy, if we are in quiet mode |
188 |
print "\n" if $quiet_mode; |
189 |
|
190 |
my $answer= ''; |
191 |
# do not wait for user input if we are in quiet mode |
192 |
$answer = <STDIN> unless $quiet_mode; |
147 |
chomp($answer); |
193 |
chomp($answer); |
148 |
$answer = lc $answer; |
194 |
$answer = lc $answer; |
149 |
return $default if not length $answer; |
195 |
return $default if not length $answer; |