diff options
author | B. Watson <urchlay@slackware.uk> | 2025-02-02 15:26:47 -0500 |
---|---|---|
committer | B. Watson <urchlay@slackware.uk> | 2025-02-02 15:26:47 -0500 |
commit | 45ed00aef983ccdb77d2611e576cb177866f03d1 (patch) | |
tree | 119844cb5e28a1b26bb6d2dd920f8c0bbb9cd443 /bsgrep | |
parent | dd9d723a60449ecc6148ffe89fe100e2d5b74035 (diff) | |
download | misc-scripts-45ed00aef983ccdb77d2611e576cb177866f03d1.tar.gz |
bsgrep: add -r and --version options.
Diffstat (limited to 'bsgrep')
-rwxr-xr-x | bsgrep | 47 |
1 files changed, 39 insertions, 8 deletions
@@ -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] ...] <regex> [<file> ...]\n"; + print "usage: $self [--help | --version | -[iklnrsvwq] [-d char] ...] <regex> [<file> ...]\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] [<file> ...]\n"; + print "usage: $self [--help | --version | -[knw] [-d char] ...] [<file> ...]\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<bsgrep> is run as B<bsjoin> (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<grep>, which uses B<-h> for something else. +=item --version + +Print the version of B<bsgrep> and exit. + =item --help Prints this help text, via B<perldoc>(1). @@ -222,6 +245,11 @@ Case-insensitive search (same as B<grep>). Instead of printing lines that match, print only the names of files that contain matches (same as B<grep>). +=item -r + +Recursively read all files under each directory, following symlinks +only if they're on the command line (same as B<grep>). + =item -v Print only lines that do I<not> match (same as B<grep>). @@ -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<grep>'s exit status. -=head1 BUGS +=head1 LIMITATIONS + +Not all b<grep> 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<grep> has, there are +too many of them. -The main bug is that B<grep>'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 |