3
0
mirror of https://github.com/pragma-/pbot.git synced 2025-01-11 12:32:37 +01:00

Replace GetOptionsFromString() with superior split_line() and GetOptionsFromArray()

This commit is contained in:
Pragmatic Software 2020-02-16 14:13:41 -08:00
parent c125ddb434
commit e2cb6e5d5e
7 changed files with 62 additions and 53 deletions

View File

@ -16,7 +16,7 @@ use feature 'unicode_strings';
use Time::Duration; use Time::Duration;
use Time::HiRes qw(gettimeofday); use Time::HiRes qw(gettimeofday);
use Getopt::Long qw(GetOptionsFromString); use Getopt::Long qw(GetOptionsFromArray);
use POSIX qw(strftime); use POSIX qw(strftime);
use Storable; use Storable;
use LWP::UserAgent; use LWP::UserAgent;
@ -311,17 +311,18 @@ sub factundo {
}; };
my ($list_undos, $goto_revision); my ($list_undos, $goto_revision);
my ($ret, $args) = GetOptionsFromString( my @opt_args = $self->{pbot}->{interpreter}->split_line($arguments, strip_quotes => 1);
$arguments, GetOptionsFromArray(
\@opt_args,
'l:i' => \$list_undos, 'l:i' => \$list_undos,
'r=i' => \$goto_revision 'r=i' => \$goto_revision
); );
return "/say $getopt_error -- $usage" if defined $getopt_error; return "/say $getopt_error -- $usage" if defined $getopt_error;
return $usage if @$args > 2; return $usage if @opt_args > 2;
return $usage if not @$args; return $usage if not @opt_args;
$arguments = join(' ', map { $_ = "'$_'" if $_ =~ m/ /; $_; } @$args); $arguments = join(' ', map { $_ = "'$_'" if $_ =~ m/ /; $_; } @opt_args);
my $arglist = $self->{pbot}->{interpreter}->make_args($arguments); my $arglist = $self->{pbot}->{interpreter}->make_args($arguments);
my ($channel, $trigger) = $self->find_factoid_with_optional_channel($from, $arguments, 'factundo', explicit => 1, exact_channel => 1); my ($channel, $trigger) = $self->find_factoid_with_optional_channel($from, $arguments, 'factundo', explicit => 1, exact_channel => 1);
@ -411,17 +412,18 @@ sub factredo {
}; };
my ($list_undos, $goto_revision); my ($list_undos, $goto_revision);
my ($ret, $args) = GetOptionsFromString( my @opt_args = $self->{pbot}->{interpreter}->split_line($arguments, strip_quotes => 1);
$arguments, GetOptionsFromArray(
\@opt_args,
'l:i' => \$list_undos, 'l:i' => \$list_undos,
'r=i' => \$goto_revision 'r=i' => \$goto_revision
); );
return "/say $getopt_error -- $usage" if defined $getopt_error; return "/say $getopt_error -- $usage" if defined $getopt_error;
return $usage if @$args > 2; return $usage if @opt_args > 2;
return $usage if not @$args; return $usage if not @opt_args;
$arguments = join(' ', map { $_ = "'$_'" if $_ =~ m/ /; $_; } @$args); $arguments = join(' ', map { $_ = "'$_'" if $_ =~ m/ /; $_; } @opt_args);
my ($channel, $trigger) = $self->find_factoid_with_optional_channel($from, $arguments, 'factredo', explicit => 1, exact_channel => 1); my ($channel, $trigger) = $self->find_factoid_with_optional_channel($from, $arguments, 'factredo', explicit => 1, exact_channel => 1);
return $channel if not defined $trigger; # if $trigger is not defined, $channel is an error message return $channel if not defined $trigger; # if $trigger is not defined, $channel is an error message
@ -955,18 +957,19 @@ sub factshow {
}; };
my ($paste); my ($paste);
my ($ret, $args) = GetOptionsFromString( my @opt_args = $self->{pbot}->{interpreter}->split_line($arguments, strip_quotes => 1);
$arguments, GetOptionsFromArray(
\@opt_args,
'p' => \$paste 'p' => \$paste
); );
return "/say $getopt_error -- $usage" if defined $getopt_error; return "/say $getopt_error -- $usage" if defined $getopt_error;
return "Too many arguments -- $usage" if @$args > 2; return "Too many arguments -- $usage" if @opt_args > 2;
return "Missing argument -- $usage" if not @$args; return "Missing argument -- $usage" if not @opt_args;
my ($chan, $trig) = @$args; my ($chan, $trig) = @opt_args;
$chan = $from if not defined $trig; $chan = $from if not defined $trig;
$args = join(' ', map { $_ = "'$_'" if $_ =~ m/ /; $_; } @$args); my $args = join(' ', map { $_ = "'$_'" if $_ =~ m/ /; $_; } @opt_args);
my ($channel, $trigger) = $self->find_factoid_with_optional_channel($from, $args, 'factshow', usage => $usage); my ($channel, $trigger) = $self->find_factoid_with_optional_channel($from, $args, 'factshow', usage => $usage);
return $channel if not defined $trigger; # if $trigger is not defined, $channel is an error message return $channel if not defined $trigger; # if $trigger is not defined, $channel is an error message
@ -1005,17 +1008,18 @@ sub factlog {
}; };
my ($show_hostmask, $actual_timestamp); my ($show_hostmask, $actual_timestamp);
my ($ret, $args) = GetOptionsFromString( my @opt_args = $self->{pbot}->{interpreter}->split_line($arguments, strip_quotes => 1);
$arguments, GetOptionsFromArray(
\@opt_args,
'h' => \$show_hostmask, 'h' => \$show_hostmask,
't' => \$actual_timestamp 't' => \$actual_timestamp
); );
return "/say $getopt_error -- $usage" if defined $getopt_error; return "/say $getopt_error -- $usage" if defined $getopt_error;
return "Too many arguments -- $usage" if @$args > 2; return "Too many arguments -- $usage" if @opt_args > 2;
return "Missing argument -- $usage" if not @$args; return "Missing argument -- $usage" if not @opt_args;
$args = join(' ', map { $_ = "'$_'" if $_ =~ m/ /; $_; } @$args); my $args = join(' ', map { $_ = "'$_'" if $_ =~ m/ /; $_; } @opt_args);
my ($channel, $trigger) = $self->find_factoid_with_optional_channel($from, $args, 'factlog', usage => $usage, exact_channel => 1); my ($channel, $trigger) = $self->find_factoid_with_optional_channel($from, $args, 'factlog', usage => $usage, exact_channel => 1);

View File

@ -18,7 +18,7 @@ use parent 'PBot::Class';
use warnings; use strict; use warnings; use strict;
use feature 'unicode_strings'; use feature 'unicode_strings';
use Getopt::Long qw(GetOptionsFromString); use Getopt::Long qw(GetOptionsFromArray);
use Time::HiRes qw(gettimeofday tv_interval); use Time::HiRes qw(gettimeofday tv_interval);
use Time::Duration; use Time::Duration;
@ -125,8 +125,9 @@ sub list_also_known_as {
Getopt::Long::Configure("bundling"); Getopt::Long::Configure("bundling");
my ($show_hostmasks, $show_gecos, $show_nickserv, $show_id, $show_relationship, $show_weak, $dont_use_aliases_table); my ($show_hostmasks, $show_gecos, $show_nickserv, $show_id, $show_relationship, $show_weak, $dont_use_aliases_table);
my ($ret, $args) = GetOptionsFromString( my @opt_args = $self->{pbot}->{interpreter}->split_line($arguments, strip_quotes => 1);
$arguments, GetOptionsFromArray(
\@opt_args,
'h' => \$show_hostmasks, 'h' => \$show_hostmasks,
'n' => \$show_nickserv, 'n' => \$show_nickserv,
'r' => \$show_relationship, 'r' => \$show_relationship,
@ -137,13 +138,13 @@ sub list_also_known_as {
); );
return "/say $getopt_error -- $usage" if defined $getopt_error; return "/say $getopt_error -- $usage" if defined $getopt_error;
return "Too many arguments -- $usage" if @$args > 1; return "Too many arguments -- $usage" if @opt_args > 1;
return "Missing argument -- $usage" if @$args != 1; return "Missing argument -- $usage" if @opt_args != 1;
my %akas = $self->{database}->get_also_known_as(@$args[0], $dont_use_aliases_table); my %akas = $self->{database}->get_also_known_as($opt_args[0], $dont_use_aliases_table);
if (%akas) { if (%akas) {
my $result = "@$args[0] also known as:\n"; my $result = "$opt_args[0] also known as:\n";
my %nicks; my %nicks;
my $sep = ""; my $sep = "";
@ -178,7 +179,7 @@ sub list_also_known_as {
} }
return $result; return $result;
} else { } else {
return "I don't know anybody named @$args[0]."; return "I don't know anybody named $opt_args[0].";
} }
} }
@ -211,8 +212,9 @@ sub recall_message {
foreach my $recall (@recalls) { foreach my $recall (@recalls) {
my ($recall_nick, $recall_history, $recall_channel, $recall_before, $recall_after, $recall_context, $recall_count); my ($recall_nick, $recall_history, $recall_channel, $recall_before, $recall_after, $recall_context, $recall_count);
my ($ret, $args) = GetOptionsFromString( my @opt_args = $self->{pbot}->{interpreter}->split_line($arguments, strip_quotes => 1);
$recall, GetOptionsFromArray(
\@opt_args,
'channel|c:s' => \$recall_channel, 'channel|c:s' => \$recall_channel,
'text|t|history|h:s' => \$recall_history, 'text|t|history|h:s' => \$recall_history,
'before|b:i' => \$recall_before, 'before|b:i' => \$recall_before,
@ -226,9 +228,9 @@ sub recall_message {
my $channel_arg = 1 if defined $recall_channel; my $channel_arg = 1 if defined $recall_channel;
my $history_arg = 1 if defined $recall_history; my $history_arg = 1 if defined $recall_history;
$recall_nick = shift @$args if @$args; $recall_nick = shift @opt_args if @opt_args;
$recall_history = shift @$args if @$args and not defined $recall_history; $recall_history = shift @opt_args if @opt_args and not defined $recall_history;
$recall_channel = "@$args" if @$args and not defined $recall_channel; $recall_channel = "@opt_args" if @opt_args and not defined $recall_channel;
$recall_count = 1 if (not defined $recall_count) || ($recall_count <= 0); $recall_count = 1 if (not defined $recall_count) || ($recall_count <= 0);
return "You may only select a count of up to 50 messages." if $recall_count > 50; return "You may only select a count of up to 50 messages." if $recall_count > 50;

View File

@ -45,7 +45,7 @@ sub ps_cmd {
Getopt::Long::Configure("bundling"); Getopt::Long::Configure("bundling");
my ($show_all, $show_user, $show_running_time); my ($show_all, $show_user, $show_running_time);
my @opt_args = $self->{pbot}->{interpreter}->split_line($arguments, preserve_escapes => 1, strip_quotes => 1); my @opt_args = $self->{pbot}->{interpreter}->split_line($arguments, strip_quotes => 1);
GetOptionsFromArray( GetOptionsFromArray(
\@opt_args, \@opt_args,
'all|a' => \$show_all, 'all|a' => \$show_all,

View File

@ -13,7 +13,7 @@ use parent 'Plugins::Plugin';
use warnings; use strict; use warnings; use strict;
use feature 'unicode_strings'; use feature 'unicode_strings';
use Getopt::Long qw(GetOptionsFromString); use Getopt::Long qw(GetOptionsFromArray);
sub initialize { sub initialize {
my ($self, %conf) = @_; my ($self, %conf) = @_;
@ -38,15 +38,16 @@ sub datecmd {
Getopt::Long::Configure("bundling"); Getopt::Long::Configure("bundling");
my ($user_override, $show_usage); my ($user_override, $show_usage);
my ($ret, $args) = GetOptionsFromString( my @opt_args = $self->{pbot}->{interpreter}->split_line($arguments, strip_quotes => 1);
$arguments, GetOptionsFromArray(
\@opt_args,
'u=s' => \$user_override, 'u=s' => \$user_override,
'h' => \$show_usage 'h' => \$show_usage
); );
return $usage if $show_usage; return $usage if $show_usage;
return "/say $getopt_error -- $usage" if defined $getopt_error; return "/say $getopt_error -- $usage" if defined $getopt_error;
$arguments = "@$args"; $arguments = "@opt_args";
my $hostmask = defined $user_override ? $user_override : "$nick!$user\@$host"; my $hostmask = defined $user_override ? $user_override : "$nick!$user\@$host";
my $tz_override = $self->{pbot}->{users}->get_user_metadata($from, $hostmask, 'timezone') // ''; my $tz_override = $self->{pbot}->{users}->get_user_metadata($from, $hostmask, 'timezone') // '';

View File

@ -14,7 +14,7 @@ no if $] >= 5.018, warnings => "experimental::smartmatch";
use DBI; use DBI;
use Time::Duration qw/concise duration/; use Time::Duration qw/concise duration/;
use Time::HiRes qw/gettimeofday/; use Time::HiRes qw/gettimeofday/;
use Getopt::Long qw(GetOptionsFromString); use Getopt::Long qw(GetOptionsFromArray);
sub initialize { sub initialize {
my ($self, %conf) = @_; my ($self, %conf) = @_;
@ -186,9 +186,9 @@ sub remindme {
Getopt::Long::Configure("bundling"); Getopt::Long::Configure("bundling");
$arguments =~ s/(?<!\\)'/\\'/g; my @opt_args = $self->{pbot}->{interpreter}->split_line($arguments, strip_quotes => 1);
my ($ret, $args) = GetOptionsFromString( GetOptionsFromArray(
$arguments, \@opt_args,
'r:i' => \$repeat, 'r:i' => \$repeat,
't:s' => \$alarm, 't:s' => \$alarm,
'c:s' => \$target, 'c:s' => \$target,
@ -259,7 +259,7 @@ sub remindme {
else { return "Could not delete reminder $delete_id."; } else { return "Could not delete reminder $delete_id."; }
} }
$text = join ' ', @$args if not defined $text; $text = join ' ', @opt_args if not defined $text;
return "Please specify a point in time for this reminder." if not $alarm; return "Please specify a point in time for this reminder." if not $alarm;
return "Please specify a reminder message." if not $text; return "Please specify a reminder message." if not $text;

View File

@ -16,7 +16,7 @@ use feature 'unicode_strings';
use PBot::Utils::LWPUserAgentCached; use PBot::Utils::LWPUserAgentCached;
use XML::LibXML; use XML::LibXML;
use Getopt::Long qw(GetOptionsFromString); use Getopt::Long qw(GetOptionsFromArray);
sub initialize { sub initialize {
my ($self, %conf) = @_; my ($self, %conf) = @_;
@ -40,15 +40,16 @@ sub weathercmd {
Getopt::Long::Configure("bundling"); Getopt::Long::Configure("bundling");
my ($user_override, $show_usage); my ($user_override, $show_usage);
my ($ret, $args) = GetOptionsFromString( my @opt_args = $self->{pbot}->{interpreter}->split_line($arguments, strip_quotes => 1);
$arguments, GetOptionsFromArray(
\@opt_args,
'u=s' => \$user_override, 'u=s' => \$user_override,
'h' => \$show_usage 'h' => \$show_usage
); );
return $usage if $show_usage; return $usage if $show_usage;
return "/say $getopt_error -- $usage" if defined $getopt_error; return "/say $getopt_error -- $usage" if defined $getopt_error;
$arguments = "@$args"; $arguments = "@opt_args";
my $hostmask = defined $user_override ? $user_override : "$nick!$user\@$host"; my $hostmask = defined $user_override ? $user_override : "$nick!$user\@$host";
my $location_override = $self->{pbot}->{users}->get_user_metadata($from, $hostmask, 'location') // ''; my $location_override = $self->{pbot}->{users}->get_user_metadata($from, $hostmask, 'location') // '';

View File

@ -22,7 +22,7 @@ no if $] >= 5.018, warnings => "experimental::smartmatch";
use PBot::Utils::LWPUserAgentCached; use PBot::Utils::LWPUserAgentCached;
use JSON; use JSON;
use URI::Escape qw/uri_escape_utf8/; use URI::Escape qw/uri_escape_utf8/;
use Getopt::Long qw(GetOptionsFromString); use Getopt::Long qw(GetOptionsFromArray);
sub initialize { sub initialize {
my ($self, %conf) = @_; my ($self, %conf) = @_;
@ -67,8 +67,9 @@ sub wttrcmd {
Getopt::Long::Configure("bundling_override", "ignorecase_always"); Getopt::Long::Configure("bundling_override", "ignorecase_always");
my %options; my %options;
my ($ret, $args) = GetOptionsFromString( my @opt_args = $self->{pbot}->{interpreter}->split_line($arguments, strip_quotes => 1);
$arguments, GetOptionsFromArray(
\@opt_args,
\%options, \%options,
'u=s', 'u=s',
'h', 'h',
@ -77,7 +78,7 @@ sub wttrcmd {
return "/say $getopt_error -- $usage" if defined $getopt_error; return "/say $getopt_error -- $usage" if defined $getopt_error;
return $usage if exists $options{h}; return $usage if exists $options{h};
$arguments = "@$args"; $arguments = "@opt_args";
my $hostmask = defined $options{u} ? $options{u} : "$nick!$user\@$host"; my $hostmask = defined $options{u} ? $options{u} : "$nick!$user\@$host";
my $location_override = $self->{pbot}->{users}->get_user_metadata($from, $hostmask, 'location') // ''; my $location_override = $self->{pbot}->{users}->get_user_metadata($from, $hostmask, 'location') // '';