Implement default values for arguments. getArgument() and createArgument() now take two parameters like setArgument(). Changed the CommandLine module's createArgument() method so that if the user hits enter at the prompt, the default value is used. Added some comments to clarify what is going on with the 'batch' argument. Changed setArgument() and createArgument() in the Arguments module and createArgument() in the CommandLine module to be more efficient (less array copying).

This commit is contained in:
ian%hixie.ch 2001-11-08 17:09:11 +00:00
Родитель 49d6ec65d5
Коммит 003293a088
2 изменённых файлов: 18 добавлений и 7 удалений

Просмотреть файл

@ -46,7 +46,7 @@ sub getArgument {
my $self = shift;
my($argument) = @_;
if (not defined($self->{"argument $argument"})) {
$self->createArgument($argument);
$self->createArgument(@_);
}
if (wantarray) {
return @{$self->{"argument $argument"}};
@ -106,7 +106,7 @@ sub addArgument {
sub setArgument {
my $self = shift;
my($argument, @value) = @_;
$self->{"argument $argument"} = [@value];
$self->{"argument $argument"} = \@value;
}
# modifies the last value for this argument to the new value
@ -139,8 +139,8 @@ sub setCommandArgument {
sub createArgument {
my $self = shift;
my($argument) = @_;
$self->{"argument $argument"} = [];
my($argument, @default) = @_;
$self->{"argument $argument"} = \@default;
}
sub propertyExists {

Просмотреть файл

@ -79,21 +79,32 @@ sub splitArguments {
sub createArgument {
my $self = shift;
# @_ also contains @default, but to save copying it about we don't
# use it directly in this method
my($argument) = @_;
if ($argument eq 'batch') {
# if --batch was not set, then we assume that means that
# we are not in --batch mode... no point asking the user,
# cos if we are, he won't reply, and if he isn't, we know
# he'd say we aren't! :-)
$self->setArgument($argument, 0);
} else {
if ($self->getArgument('batch')) {
$self->SUPER::createArgument($argument);
$self->SUPER::createArgument(@_);
} else {
$self->warn(5, "going to request '$argument' from user!");
$self->app->output->request($argument);
# get input from user :-)
# get input from user
my $term = Term::ReadLine->new($self->app->name);
my $value = $term->readline(''); # (the parameter passed is the prompt, if any)
# if we cached the input device:
# $term->addhistory($value);
$self->setArgument($argument, $value);
if ($value eq '') {
# use default
$self->setArgument(@_);
} else {
$self->setArgument($argument, $value);
}
}
}
}