From 6de1023ee898fb65a64c5ee104652bc32ac02a00 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Wed, 30 Apr 2014 21:37:28 +0000 Subject: [PATCH] Use Getopt::Long to parse `rq` options --- PBot/Quotegrabs.pm | 30 ++++++++++++++++++------------ PBot/VERSION.pm | 4 ++-- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/PBot/Quotegrabs.pm b/PBot/Quotegrabs.pm index 057158c4..7c6b2f08 100644 --- a/PBot/Quotegrabs.pm +++ b/PBot/Quotegrabs.pm @@ -14,6 +14,7 @@ $VERSION = $PBot::PBot::VERSION; use HTML::Entities; use Time::Duration; use Time::HiRes qw(gettimeofday); +use Getopt::Long qw(GetOptionsFromString); use POSIX qw(strftime); @@ -414,19 +415,24 @@ sub show_random_quotegrab { return ""; } + my $usage = 'Usage: rq [nick regex] [-c,--channel ] [-t,--text ]'; + if(defined $arguments) { - $nick_search = $1 if $arguments =~ s/-nick\s+(\S+)//g; - $channel_search = $1 if $arguments =~ s/-channel\s+(\S+)//g; - $text_search = $1 if $arguments =~ s/-text\s+(\S+)//g; + my $getopt_error; + local $SIG{__WARN__} = sub { + $getopt_error = shift; + chomp $getopt_error; + }; - $arguments =~ s/^\s+//; - $arguments =~ s/\s+$//; + my ($ret, $args) = GetOptionsFromString($arguments, + '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; - $channel_search = $possible_channel_search if not defined $channel_search; - $text_search = $possible_text_search if not defined $text_search; + $nick_search = shift @$args; + $channel_search = shift @$args if not defined $channel_search; + $text_search = shift @$args if not defined $text_search; if($nick_search =~ m/^#/) { my $tmp = $channel_search; @@ -473,7 +479,7 @@ sub show_random_quotegrab { $result .= "matching text '$text_search' "; } - return $result . "yet (usage: rq [nick regex] [-channel ] [-text ]).";; + return $result . "yet ($usage).";; } my $quotegrab = $quotes[int rand($#quotes + 1)]; @@ -481,9 +487,9 @@ sub show_random_quotegrab { my ($first_nick) = split /\+/, $quotegrab->{nick}, 2; 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 { - 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"; } } diff --git a/PBot/VERSION.pm b/PBot/VERSION.pm index 864d5ec0..379c8431 100644 --- a/PBot/VERSION.pm +++ b/PBot/VERSION.pm @@ -13,8 +13,8 @@ use warnings; # These are set automatically by the build/commit script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 559, - BUILD_DATE => "2014-04-29", + BUILD_REVISION => 560, + BUILD_DATE => "2014-04-30", }; 1;