Permalink
Browse files

Add value validation and variable argument count.

  • Loading branch information...
1 parent 29b8981 commit 7aa25123b9861a61f31971fa9ef863234767f4c8 @okxchg okxchg committed Jun 4, 2012
@@ -109,15 +109,40 @@ sub _handle_cmd {
if (defined $self->{Commands}->{$cmd}) {
if (ref $self->{Commands}->{$cmd} eq 'HASH') {
my @args_array = defined $args ? split /\s+/, $args : ();
- if (@args_array != @{ $self->{Commands}->{$cmd}->{args} }) {
- $irc->yield($self->{Method}, $where,
- "Not enough or too many arguments. See help for $cmd");
- return;
+ if (@args_array < @{ $self->{Commands}->{$cmd}->{args} } ||
+ (!defined $self->{Commands}->{$cmd}->{variable} &&
+ @args_array > @{ $self->{Commands}->{$cmd}->{args} })
+ ) {
+ $irc->yield($self->{Method}, $where,
+ "Not enough or too many arguments. See help for $cmd");
+ return;
}
$args = {};
for (@{ $self->{Commands}->{$cmd}->{args} }) {
- $args->{$_} = shift @args_array;
+ my $in_arg = shift @args_array;
+ if (ref $self->{Commands}->{$cmd}->{$_} eq 'ARRAY') {
+ my @values = @{ $self->{Commands}->{$cmd}->{$_} };
+ shift @values;
+
+ use List::MoreUtils qw(none);
+ # Check if argument has one of possible values
+ if (none { $_ eq $in_arg} @values) {
+ $irc->yield($self->{Method}, $where,
+ "$_ can be one of ".join '|', @values);
+ return;
+ }
+
+ }
+ $args->{$_} = $in_arg;
+ }
+
+ # Process remaining arguments if variable is set
+ my $arg_cnt = 0;
+ if (defined $self->{Commands}->{$cmd}->{variable}) {
+ for (@args_array) {
+ $args->{"opt".$arg_cnt++} = $_;
+ }
}
}
}
@@ -163,12 +188,25 @@ sub _get_help {
my $cmd = (split /\s+/, $args, 2)[0];
if (exists $self->{Commands}->{$cmd}) {
if (ref $self->{Commands}->{$cmd} eq 'HASH') {
- push @help, "Syntax: $cmd ".join ' ', @{ $self->{Commands}->{$cmd}->{args} };
- push @help, split /\015?\012/, "Description: ".$self->{Commands}->{$cmd}->{info};
+ push @help, "Syntax: $cmd ".
+ (join ' ', @{ $self->{Commands}->{$cmd}->{args} }).
+ (defined $self->{Commands}->{$cmd}->{variable} ?
+ " ..." : "");
+ push @help, split /\015?\012/,
+ "Description: ".$self->{Commands}->{$cmd}->{info};
push @help, "Arguments:";
+
for my $arg (@{ $self->{Commands}->{$cmd}->{args} }) {
- push @help, " $arg: ".$self->{Commands}->{$cmd}->{$arg}
- if defined $self->{Commands}->{$cmd}->{$arg};
+ next if not defined $self->{Commands}->{$cmd}->{$arg};
+ if (ref $self->{Commands}->{$cmd}->{$arg} eq 'ARRAY') {
+ my @arg_usage = @{$self->{Commands}->{$cmd}->{$arg}};
+ push @help, " $arg: ".$arg_usage[0].
+ " (".(join '|', @arg_usage[1..$#arg_usage]).")"
+ }
+ else {
+ push @help, " $arg: ".
+ $self->{Commands}->{$cmd}->{$arg};
+ }
}
}
else {
@@ -93,8 +93,10 @@ sub irc_001 {
cmd2 => {
info => 'First test command with argument count checking',
args => [qw(test_arg test_arg2)],
- test_arg => 'Description of first arg',
+ variable => 1,
+ test_arg => ['Description of first arg', qw(value1 value2)],
test_arg2 => 'Description of second arg',
+ optional_arg => 'Description of optional arg',
},
foo => 'This will get removed',
},
@@ -125,7 +127,7 @@ sub irc_join {
$bot2->yield(privmsg => $where, "TestBot1: cmd1 foo bar");
# try command with predefined arguments
- $bot2->yield(privmsg => $where, "TestBot1: cmd2 foo bar");
+ $bot2->yield(privmsg => $where, "TestBot1: cmd2 value1 bar opt_arg");
# and one with color
$bot2->yield(privmsg => $where, "\x0302TestBot1\x0f: \x02cmd3\x0f");
@@ -148,7 +150,7 @@ sub irc_botcmd_cmd2 {
is($nick, $bot2->nick_name(), 'Command with args (user)');
is($where, '#testchannel', 'Command with args (channel)');
- is_deeply($args, { test_arg => 'foo', test_arg2 => 'bar' },
+ is_deeply($args, { test_arg => 'value1', test_arg2 => 'bar', opt0 => 'opt_arg'},
'Command with args (arguments)');
}
@@ -36,10 +36,10 @@ POE::Session->create(
);
my @bar_help = (
- "Syntax: bar arg1 arg2",
+ "Syntax: bar arg1 arg2 ...",
"Description: Test command2",
"Arguments:",
- " arg1: What to bar",
+ " arg1: What to bar (table|chair)",
" arg2: Where to bar"
);
@@ -132,7 +132,9 @@ sub irc_notice {
ok($p->add(bar => {
info => 'Test command2',
args => [qw(arg1 arg2)],
- arg1 => 'What to bar', arg2 => 'Where to bar'
+ arg1 => ['What to bar', qw(table chair)],
+ arg2 => 'Where to bar',
+ variable => 1,
}), 'Add command bar');
$irc->yield(privmsg => $where, "TestBot1: help");
$irc->yield(privmsg => $where, "TestBot1: help bar");

0 comments on commit 7aa2512

Please sign in to comment.