From: bw1 Date: Tue, 19 Feb 2019 15:17:57 +0000 (+0100) Subject: [file] Getopt::Long X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=96c379e22c79c6d6e6f00d0a4388b26ee52773ea;p=irssi-scripts.irssi.org.git [file] Getopt::Long --- diff --git a/scripts/file.pl b/scripts/file.pl index 95c573e..fe58bcc 100644 --- a/scripts/file.pl +++ b/scripts/file.pl @@ -1,5 +1,6 @@ use strict; use vars qw($VERSION %IRSSI); +use Getopt::Long qw/GetOptionsFromString/; my $help = < "David Leadbeater", name => "file.pl", @@ -36,36 +37,44 @@ Irssi::command_bind('file', sub { return; } - my($type, $target, $prefix, $postfix); + my($type, $target, $prefix, $postfix, $echo); $type = 'msg'; $target = '*'; $prefix = ''; $postfix = ''; - while($data =~ s/^-([^ ]+) //g) { - last if $data eq '-'; - - if($1 eq 'msg' || $1 eq 'notice') { - $type = $1; - next unless $data =~ / /; # >1 params left - $data =~ s/^([^ ]+) //; - next unless $1; - $target = $1; - }elsif($1 eq 'prefix') { - $data =~ s/^(?:\"([^"]+)\"|([^ ]+)) //; - $prefix = $1 || $2 . ' '; - }elsif($1 eq 'postfix') { - $data =~ s/^(?:\"([^"]+)\"|([^ ]+)) //; - $postfix = ' ' . $1 || $2; - }else{ # Other options are automatic - $type = $1; + my ($raw,$command,$msg,$notice,$filename); + + my ($ret, $args) = GetOptionsFromString($data, + 'raw' => \$raw, + 'command' => \$command, + 'msg:s' => \$msg, + 'notice:s' => \$notice, + 'prefix=s' => \$prefix, + 'postfix=s' => \$postfix, + 'echo' => \$echo, + ); + $filename = $$args[-1]; + $type ='raw' if (defined $raw); + $type ='command' if (defined $command); + $type ='echo' if (defined $echo); + if (defined $notice) { + $type ='notice'; + if ($notice ne '') { + $target = $notice; + } + } + if (defined $msg) { + $type ='msg'; + if ($msg ne '') { + $target = $msg; } } # or do borrowed from one of juerd's scripts (needs 5.6 though) - open(FILE, "<", $data) or do { - print "Error opening '$data': $!"; + open(FILE, "<", $filename) or do { + print "Error opening '$filename': $!"; return; }; @@ -76,6 +85,8 @@ Irssi::command_bind('file', sub { Irssi::active_server->send_raw($prefix . $_ . $postfix); }elsif($type eq 'command') { Irssi::active_win->command($prefix . $_ . $postfix); + }elsif($type eq 'echo') { + Irssi::active_win->print($prefix . $_ . $postfix); }else{ Irssi::active_win->command("$type $target $prefix$_$postfix"); } @@ -86,5 +97,6 @@ Irssi::command_bind('file', sub { } ); # little known way to get -options to tab complete :) -Irssi::command_set_options('file','raw command prefix postfix msg notice'); +Irssi::command_set_options('file','raw command prefix postfix msg notice echo'); +# vim:set ts=3 sw=3 expandtab: