From 45ed00aef983ccdb77d2611e576cb177866f03d1 Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Sun, 2 Feb 2025 15:26:47 -0500 Subject: bsgrep: add -r and --version options. --- bsgrep | 47 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 8 deletions(-) (limited to 'bsgrep') diff --git a/bsgrep b/bsgrep index 31492a3..ad89e29 100755 --- a/bsgrep +++ b/bsgrep @@ -3,6 +3,7 @@ $VERSION = "0.0.1"; use Getopt::Std; +use File::Find; ($self = $0) =~ s,.*/,,; @@ -21,12 +22,11 @@ $SIG{__WARN__} = sub { }; sub grep_usage { - print "usage: $self [--help | -[iklnsvwq] [-d char] ...] [ ...]\n"; + print "usage: $self [--help | --version | -[iklnrsvwq] [-d char] ...] [ ...]\n"; } sub grep_options { getopts('hd:vlkinrwsq', \%opt) || exit 1; - die("$self: -r option not supported yet (TODO)\n") if $opt{r}; if($opt{h}) { grep_usage(); exit(0); @@ -40,7 +40,7 @@ sub print_line { } sub join_help { - print "usage: $self [-knw] [ ...]\n"; + print "usage: $self [--help | --version | -[knw] [-d char] ...] [ ...]\n"; exit 0; } @@ -73,6 +73,9 @@ if(defined($ARGV[0])) { } elsif($ARGV[0] eq '--man') { exec "pod2man --stderr -s1 -cUrchlaysStuff -r$VERSION -u $0"; exit(1); + } elsif($ARGV[0] eq '--version') { + print "bsgrep $VERSION\n"; + exit(0); } } @@ -88,6 +91,22 @@ if($self =~ /join/) { $regex = "(?i)$regex" if $opt{i}; } +if($opt{r}) { + for(@ARGV) { + if(-d $_) { + find({ + wanted => sub { push @nargv, $_ if -f _; }, + follow => 0, + no_chdir => 1 }, + $_); + } else { + push @nargv, $_; + } + } + + @ARGV = @nargv; +} + $ret = 1; # return value from main(), set to 0 if anything matched. $filecount = @ARGV; # used to decide whether to print filename prefixes. @@ -165,7 +184,7 @@ are given. Output goes to standard output. If B is run as B (via symbolic or hard link, or just copying the executable), it will simply join together continued lines without searching for anything. In this mode, only the B<-k>, B<-n>, -B<-w>, B<-h>, and B<--help> options are supported. +B<-w>, B<-h>, B<--version>, and B<--help> options are supported. =head1 OPTIONS @@ -197,6 +216,10 @@ the Perl B<\b> syntax in the regex. Prints a short help message and exits. Not compatible with B, which uses B<-h> for something else. +=item --version + +Print the version of B and exit. + =item --help Prints this help text, via B(1). @@ -222,6 +245,11 @@ Case-insensitive search (same as B). Instead of printing lines that match, print only the names of files that contain matches (same as B). +=item -r + +Recursively read all files under each directory, following symlinks +only if they're on the command line (same as B). + =item -v Print only lines that do I match (same as B). @@ -305,11 +333,14 @@ were errors (e.g. nonexistent file). However, with B<-s>, the exit status will be 0 or 1 even if there were errors. This is the same as B's exit status. -=head1 BUGS +=head1 LIMITATIONS + +Not all b options are supported. Options that aren't implemented +but might be someday include B<--color>, B<-a>, B<-A>, B<-B>, B<-C>. +I don't intend to implement every single option B has, there are +too many of them. -The main bug is that B's B<-r> (recursive) option is not -supported. Other options that aren't implemented but might be -someday include B<--color>, B<-a>, B<-A>, B<-B>, B<-C>. +There are no long options other than B<--help> and B<--version>. =head1 AUTHOR -- cgit v1.2.3