Update of /cvsroot/lxr/lxr/scripts
In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv10092/scripts
Modified Files:
QuestionAnswer.pm
Log Message:
scripts/QuestionAnswer.pm: add validation filter to answers returned after open question
Index: QuestionAnswer.pm
===================================================================
RCS file: /cvsroot/lxr/lxr/scripts/QuestionAnswer.pm,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- QuestionAnswer.pm 2 Sep 2013 17:06:03 -0000 1.3
+++ QuestionAnswer.pm 7 Nov 2013 16:35:52 -0000 1.4
@@ -72,13 +72,15 @@
# -1, open question with default answer
# 0, closed question, no default answer
# >0, closed question, default answer number (first is 1)
-# - $choices: ref to array of choice strings
+# - $choices: ref to array of choice strings or optional validation
+# pairs for dft < 0
# - $answers: ref to normalised answers
-# $choices and $answers must have same number of strings.
+# $choices and $answers must have same number of strings for dft >= 0.
# Both may be omitted if $default < 0.
sub get_user_choice {
my ($question, $default, $choices, $answers) = @_;
my @pats;
+ my @choices;
my @opendefault;
# Build the patterns associated with answers
@@ -95,27 +97,27 @@
exit 2;
}
@pats = find_unique_prefix ($choices);
- @$choices = map(lc, @$choices);
+ @choices = map(lc, @$choices);
+ # Uppercase default answer
+ $choices[$default] = $VTgreen . uc($$choices[$default]);
}
+
# Check open-with-default case
if ($default == -2) {
if (defined($answers)) {
- @opendefault[0] = $VTgreen . $$answers[0];
- $choices = \@opendefault;
+ @choices[0] = $VTgreen . $$answers[0];
} else {
print "${VTred}FATAL:${VTnorm} no default choice for \"$question\"!\n";
exit 2;
}
}
- # Uppercase default answer
- if ($default > -1) {
- $$choices[$default] = $VTgreen . uc($$choices[$default]);
- }
+
# Get answer from user and return a normalised one
+QLOOP:
while (1) {
print $question;
- if (defined($choices) && @$choices) {
- print " [${VTyellow}", join("${VTnorm}/${VTyellow}", @$choices), "${VTnorm}]";
+ if (@choices) {
+ print " [${VTyellow}", join("${VTnorm}/${VTyellow}", @choices), "${VTnorm}]";
}
print " ${VTslow}${VTyellow}>${VTnorm} ";
my $userentry = <STDIN>;
@@ -135,6 +137,17 @@
}
# If open question, return free text
if ($default < -1) {
+ if (defined($choices)) { # Any constraint check?
+ my ($chk, $msg, $i);
+ for ($i = 0; $i < $#$choices; $i++) {
+ $chk = $$choices[$i];
+ $msg = $$choices[++$i];
+ if ($userentry !~ m/$chk/) {
+ print "${VTred}ERROR:${VTnorm} $msg, try again ...\n";
+ next QLOOP;
+ }
+ }
+ }
return $userentry;
}
# Closed question: find which choice
|