From 081477eeb50fc347697b05612109620f9a14e64b Mon Sep 17 00:00:00 2001 From: Elizabeth Mattijsen Date: Tue, 30 Jul 2024 12:29:41 +0200 Subject: [PATCH] Add infrastructure for --not and --nots-from To allow specification of acceptance of patterns *not* including a pattern, so you can say "rak foo --not=bar" to find occurrences of lines with "foo" but without "bar" --- lib/App/Rak.rakumod | 113 ++++++++++++++++++++++++++++---------------- 1 file changed, 72 insertions(+), 41 deletions(-) diff --git a/lib/App/Rak.rakumod b/lib/App/Rak.rakumod index 1118382..f966d5f 100644 --- a/lib/App/Rak.rakumod +++ b/lib/App/Rak.rakumod @@ -22,9 +22,9 @@ my constant BON = "\e[1m"; # BOLD ON my constant BOFF = "\e[22m"; # BOLD OFF #- start of available options -------------------------------------------------- -#- Generated on 2024-05-12T20:26:03+02:00 by tools/makeOPTIONS.raku +#- Generated on 2024-07-30T12:20:12+02:00 by ./tools/makeOPTIONS.raku #- PLEASE DON'T CHANGE ANYTHING BELOW THIS LINE -my str @options = ; +my str @options = ; #- PLEASE DON'T CHANGE ANYTHING ABOVE THIS LINE #- end of available options ---------------------------------------------------- @@ -191,7 +191,8 @@ my $output-file; # process output file if defined my $output-dir; # process output directory if defined my $debug-rak; # process show rak args -my $pattern; # the pattern specified (if any) +my $pattern; # the acceptance pattern(s) specified (if any) +my $not; # the deny pattern(s) specified, if any my $smartcase; # --smartcase my $smartmark; # --smartmark my $ignorecase; # --ignorecase @@ -1506,6 +1507,64 @@ my sub set-listing-flag-or-Int(str $name, $value --> Nil) { } } +#------------------------------------------------------------------------------- +# Helper subs for handling pattern specifications + +my sub handle-pattern($value, :$handle-not --> Nil) { + my str $type = $handle-not ?? 'not' !! 'pattern'; + my $checkee := $handle-not ?? $not !! $pattern; + + Bool.ACCEPTS($value) + ?? meh "'--$type' must be a pattern specification, not a flag" + !! List.ACCEPTS($checkee) + ?? meh "Cannot specify --$type with --{$type}s-from at the same time" + !! $checkee.defined + ?? meh "Can only specify --$type once" + !! $handle-not + ?? ($not := $value) + !! ($pattern := $value); +} + +my sub handle-patterns-from($value, :$handle-not --> Nil) { + my str $type = $handle-not ?? '--not' !! '--pattern'; + my str $from = $type ~ 's-from'; + my $checkee := $handle-not ?? $not !! $pattern; + + # helper sub for getting pattern(s) from file + sub read-patterns($handle) { + if $handle.lines -> @patterns { + $handle-not + ?? ($not := $_) + !! ($pattern := $_) + with @patterns == 1 ?? @patterns.head !! @patterns; + } + else { + meh "No patterns found in file '$value' with '$from'"; + } + } + + if Bool.ACCEPTS($value) { + meh "'$from' must be a file specification, not a flag" + } + elsif List.ACCEPTS($pattern) { + meh "Can only specify '$from' once" + } + elsif $pattern.defined { + meh "Cannot specify '$from' with '$type' already specified"; + } + elsif $value eq '-' { + note "Reading from STDIN.\nPlease enter patterns for '$from' and ^D when done:" + unless $reading-from-stdin; + read-patterns($*IN); + } + elsif $value.IO.r { + read-patterns($value.IO); + } + else { + meh "Could not read from '$value' to obtain patterns"; + } +} + #------------------------------------------------------------------------------- # One subroutine for each supported option. Is assumed to do right thing for # that option by setting the appropriate global hashes. Not expected to return @@ -2009,6 +2068,14 @@ my sub option-module($value --> Nil) { !! @modules.push($value) } +my sub option-not($value --> Nil) { + handle-pattern($value, :handle-not); +} + +my sub option-nots-from($value --> Nil) { + handle-patterns-from($value, :handle-not); +} + my sub option-only-first($value --> Nil) { set-listing-flag-or-Int( 'only-first', @@ -2066,47 +2133,11 @@ my sub option-paths-from($value --> Nil) { } my sub option-pattern($value --> Nil) { - Bool.ACCEPTS($value) - ?? meh "'--pattern' must be a pattern specification, not a flag" - !! List.ACCEPTS($pattern) - ?? meh "Cannot specify --pattern with --patterns-from at the same time" - !! $pattern.defined - ?? meh "Can only specify --pattern once" - !! ($pattern := $value); + handle-pattern($value); } my sub option-patterns-from($value --> Nil) { - - # helper sub for getting pattern(s) from file - sub read-patterns($handle) { - if $handle.lines -> @patterns { - $pattern := @patterns == 1 ?? @patterns.head !! @patterns; - } - else { - meh "No patterns found, so no matches to be expected"; - } - } - - if Bool.ACCEPTS($value) { - meh "'--patterns-from' must be a file specification, not a flag" - } - elsif List.ACCEPTS($pattern) { - meh "Can only specify --patterns-from once" - } - elsif $pattern.defined { - meh "Cannot specify --patterns-from with a pattern already specified"; - } - elsif $value eq '-' { - note "Reading from STDIN, please enter patterns and ^D when done:" - unless $reading-from-stdin; - read-patterns($*IN); - } - elsif $value.IO.r { - read-patterns($value.IO); - } - else { - meh "Could not read from '$value' to obtain patterns"; - } + handle-patterns-from($value); } my sub option-pdf-info($value --> Nil) {