From 2ea2b609f46a51bab56bccb7cbc93c27b8a3b5a6 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 4 Jan 2021 04:16:23 +0000 Subject: lei: fix opt_dash to pass non-dash args to @argv The special "<>" handling in Getopt::Long actually invokes the callback for every single command-line arg, not just those prefixed by "-". This will let us pass arbitrary non-dashed words for search queries so users can type queries naturally without quoting (unless they want phrase search). --- lib/PublicInbox/LEI.pm | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index f41f63ed..50453dde 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -38,18 +38,27 @@ our %PATH2CFG; # persistent for socket daemon sub pass_through { $GLP_PASS } my $OPT; -sub opt_dash { +sub opt_dash ($$) { my ($spec, $re_str) = @_; # 'limit|n=i', '([0-9]+)' my ($key) = ($spec =~ m/\A([a-z]+)/g); my $cb = sub { # Getopt::Long "<>" catch-all handler my ($arg) = @_; if ($arg =~ /\A-($re_str)\z/) { $OPT->{$key} = $1; + } elsif ($arg eq '--') { # "--" arg separator, ignore first + push @{$OPT->{-argv}}, $arg if $OPT->{'--'}++; + # lone (single) dash is handled elsewhere + } elsif (substr($arg, 0, 1) eq '-') { + if ($OPT->{'--'}) { + push @{$OPT->{-argv}}, $arg; + } else { + die "bad argument: $arg\n"; + } } else { - die "bad argument for --$key: $arg\n"; + push @{$OPT->{-argv}}, $arg; } }; - ($spec, '<>' => $cb, $GLP_PASS) + ($spec, '<>' => $cb, $GLP_PASS) # for Getopt::Long } sub _store_path ($) { @@ -360,6 +369,8 @@ sub optparse ($$$) { return _help($self, "bad arguments or options for $cmd"); return _help($self) if $OPT->{help}; + push @$argv, @{$OPT->{-argv}} if defined($OPT->{-argv}); + # "-" aliases "stdin" or "clear" $OPT->{$lone_dash} = ${$OPT->{$lone_dash}} if defined $lone_dash; -- cgit v1.2.3-24-ge0c7