3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-10-03 01:48:38 +02:00

Use Getopt::Long to parse rq options

This commit is contained in:
Pragmatic Software 2014-04-30 21:37:28 +00:00
parent cebcafba70
commit 6de1023ee8
2 changed files with 20 additions and 14 deletions

View File

@ -14,6 +14,7 @@ $VERSION = $PBot::PBot::VERSION;
use HTML::Entities; use HTML::Entities;
use Time::Duration; use Time::Duration;
use Time::HiRes qw(gettimeofday); use Time::HiRes qw(gettimeofday);
use Getopt::Long qw(GetOptionsFromString);
use POSIX qw(strftime); use POSIX qw(strftime);
@ -414,19 +415,24 @@ sub show_random_quotegrab {
return ""; return "";
} }
my $usage = 'Usage: rq [nick regex] [-c,--channel <channel regex>] [-t,--text <text regex>]';
if(defined $arguments) { if(defined $arguments) {
$nick_search = $1 if $arguments =~ s/-nick\s+(\S+)//g; my $getopt_error;
$channel_search = $1 if $arguments =~ s/-channel\s+(\S+)//g; local $SIG{__WARN__} = sub {
$text_search = $1 if $arguments =~ s/-text\s+(\S+)//g; $getopt_error = shift;
chomp $getopt_error;
};
$arguments =~ s/^\s+//; my ($ret, $args) = GetOptionsFromString($arguments,
$arguments =~ s/\s+$//; 'channel|c=s' => \$channel_search,
'text|t=s' => \$text_search);
my ($possible_nick_search, $possible_channel_search, $possible_text_search) = split /\s+/, $arguments; return "$getopt_error -- $usage" if defined $getopt_error;
$nick_search = $possible_nick_search if not defined $nick_search; $nick_search = shift @$args;
$channel_search = $possible_channel_search if not defined $channel_search; $channel_search = shift @$args if not defined $channel_search;
$text_search = $possible_text_search if not defined $text_search; $text_search = shift @$args if not defined $text_search;
if($nick_search =~ m/^#/) { if($nick_search =~ m/^#/) {
my $tmp = $channel_search; my $tmp = $channel_search;
@ -473,7 +479,7 @@ sub show_random_quotegrab {
$result .= "matching text '$text_search' "; $result .= "matching text '$text_search' ";
} }
return $result . "yet (usage: rq [nick regex] [-channel <channel regex>] [-text <text regex>]).";; return $result . "yet ($usage).";;
} }
my $quotegrab = $quotes[int rand($#quotes + 1)]; my $quotegrab = $quotes[int rand($#quotes + 1)];
@ -481,9 +487,9 @@ sub show_random_quotegrab {
my ($first_nick) = split /\+/, $quotegrab->{nick}, 2; my ($first_nick) = split /\+/, $quotegrab->{nick}, 2;
if($text =~ s/^\/me\s+//) { if($text =~ s/^\/me\s+//) {
return "$quotegrab->{id}: " . ($channel_search eq '.*' ? "[$quotegrab->{channel}] " : "") . "* $first_nick $text"; return "$quotegrab->{id}: " . (($channel_search eq '.*' or $quotegrab->{channel} ne $from) ? "[$quotegrab->{channel}] " : "") . "* $first_nick $text";
} else { } else {
return "$quotegrab->{id}: " . ($channel_search eq '.*' ? "[$quotegrab->{channel}] " : "") . "<$first_nick> $text"; return "$quotegrab->{id}: " . (($channel_search eq '.*' or $quotegrab->{channel} ne $from) ? "[$quotegrab->{channel}] " : "") . "<$first_nick> $text";
} }
} }

View File

@ -13,8 +13,8 @@ use warnings;
# These are set automatically by the build/commit script # These are set automatically by the build/commit script
use constant { use constant {
BUILD_NAME => "PBot", BUILD_NAME => "PBot",
BUILD_REVISION => 559, BUILD_REVISION => 560,
BUILD_DATE => "2014-04-29", BUILD_DATE => "2014-04-30",
}; };
1; 1;