From 17e78cd7fc8cc58c3cfc3327d2ef3831f756c6d8 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Sat, 31 Jul 2021 12:04:50 -0700 Subject: [PATCH] Commands: add(): improve named-parameter validation --- lib/PBot/Core/Commands.pm | 30 +++++++++++++++++++++--------- lib/PBot/VERSION.pm | 4 ++-- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/PBot/Core/Commands.pm b/lib/PBot/Core/Commands.pm index 1df63a98..545732c8 100644 --- a/lib/PBot/Core/Commands.pm +++ b/lib/PBot/Core/Commands.pm @@ -39,18 +39,30 @@ sub load_commands { sub add { my ($self, %args) = @_; - $self->register( - delete $args{subref}, - delete $args{name}, - delete $args{requires_cap}, - delete $args{help}, - ); + # expected parameters + my @valid = qw(subref name requires_cap help); - # die if any unhandled arguments were passed + # check for unexpected parameters + my @invalid; foreach my $key (keys %args) { - $self->{pbot}->{logger}->log("Commands: error: extra arguments provided to add(): $key\n"); - die; + if (not grep { $_ eq $key } @valid) { + 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 diff --git a/lib/PBot/VERSION.pm b/lib/PBot/VERSION.pm index f3e294fc..a1ad1ade 100644 --- a/lib/PBot/VERSION.pm +++ b/lib/PBot/VERSION.pm @@ -25,8 +25,8 @@ use PBot::Imports; # These are set by the /misc/update_version script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 4329, - BUILD_DATE => "2021-07-30", + BUILD_REVISION => 4331, + BUILD_DATE => "2021-07-31", }; sub initialize {}