]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei: fix opt_dash to pass non-dash args to @argv
authorEric Wong <e@80x24.org>
Mon, 4 Jan 2021 04:16:23 +0000 (04:16 +0000)
committerEric Wong <e@80x24.org>
Mon, 4 Jan 2021 19:00:47 +0000 (19:00 +0000)
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

index f41f63ed126870683e4f93fd499e50c345605053..50453dde934551d4221651fbca26bebe2aa9038d 100644 (file)
@@ -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;