From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id D146C1F934 for ; Mon, 4 Jan 2021 04:16:24 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 1/2] lei: fix opt_dash to pass non-dash args to @argv Date: Mon, 4 Jan 2021 04:16:23 +0000 Message-Id: <20210104041624.17510-2-e@80x24.org> In-Reply-To: <20210104041624.17510-1-e@80x24.org> References: <20210104041624.17510-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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(-) 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;