mirror of
https://github.com/pragma-/pbot.git
synced 2025-01-11 04:22:35 +01:00
Commands: add(): improve named-parameter validation
This commit is contained in:
parent
5aeb608052
commit
17e78cd7fc
@ -39,18 +39,30 @@ sub load_commands {
|
|||||||
sub add {
|
sub add {
|
||||||
my ($self, %args) = @_;
|
my ($self, %args) = @_;
|
||||||
|
|
||||||
$self->register(
|
# expected parameters
|
||||||
delete $args{subref},
|
my @valid = qw(subref name requires_cap help);
|
||||||
delete $args{name},
|
|
||||||
delete $args{requires_cap},
|
|
||||||
delete $args{help},
|
|
||||||
);
|
|
||||||
|
|
||||||
# die if any unhandled arguments were passed
|
# check for unexpected parameters
|
||||||
|
my @invalid;
|
||||||
foreach my $key (keys %args) {
|
foreach my $key (keys %args) {
|
||||||
$self->{pbot}->{logger}->log("Commands: error: extra arguments provided to add(): $key\n");
|
if (not grep { $_ eq $key } @valid) {
|
||||||
die;
|
push @invalid, $key;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# die if any unexpected parameters were passed
|
||||||
|
if (@invalid) {
|
||||||
|
$self->{pbot}->{logger}->log("Commands: error: invalid arguments provided to add(): @invalid\n");
|
||||||
|
die "Commands: error: invalid arguments provided to add(): @invalid";
|
||||||
|
}
|
||||||
|
|
||||||
|
# register command
|
||||||
|
$self->register(
|
||||||
|
$args{subref},
|
||||||
|
$args{name},
|
||||||
|
$args{requires_cap},
|
||||||
|
$args{help},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
# alias to unregister() for consistency
|
# alias to unregister() for consistency
|
||||||
|
@ -25,8 +25,8 @@ use PBot::Imports;
|
|||||||
# These are set by the /misc/update_version script
|
# These are set by the /misc/update_version script
|
||||||
use constant {
|
use constant {
|
||||||
BUILD_NAME => "PBot",
|
BUILD_NAME => "PBot",
|
||||||
BUILD_REVISION => 4329,
|
BUILD_REVISION => 4331,
|
||||||
BUILD_DATE => "2021-07-30",
|
BUILD_DATE => "2021-07-31",
|
||||||
};
|
};
|
||||||
|
|
||||||
sub initialize {}
|
sub initialize {}
|
||||||
|
Loading…
Reference in New Issue
Block a user