my @choices = ("yes", "no", "y", "n"); my $choice = query("Can you use an array for the choices?", "YES", @ch +oices); my $otherChoice = query("Can you use an invalid default to force them +to type in their choice?", "yes/no", "yes", "y"); my $finalChoice = query("Oh, just enter whatever you want.");
# &query(Prompt String, optional default, optional list of restrictive + choices) # If the choices are provided at the end, only one of those can be ch +osen. # This is currently case insensitive. # # Example: my $val = query("Do you like config scripts?", "yes", "yes" +, "no", "y", "n"); # Will print: Do you like config scripts? [yes]: # The user can either press enter for yes, or enter one of the other a +rguments. sub query { my $prompt = (defined $_[0]) ? $_[0] : ""; my $default = (defined $_[1]) ? $_[1] : ""; my $answer; while(1) { print "$prompt [$default]: "; chomp($answer = <STDIN>); $answer = $default if ($answer eq ""); last unless $#_ > 1; last if grep $answer =~ /^\Q$_\E$/i, @_[2 .. $#_]; } return $answer; }
|
---|
Replies are listed 'Best First'. | |
---|---|
(jeffa) Re: Prompting via shell with default arguments
by jeffa (Bishop) on Feb 02, 2003 at 01:18 UTC | |
by Azhrarn (Friar) on Feb 02, 2003 at 02:18 UTC | |
by Azhrarn (Friar) on Feb 02, 2003 at 20:24 UTC |