Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rev: terminate options with -- #439

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 41 additions & 46 deletions bin/rev
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ License: gpl
use strict;

use File::Basename qw(basename);
use Getopt::Std qw(getopts);

use constant EX_SUCCESS => 0;
use constant EX_FAILURE => 1;
Expand All @@ -25,48 +26,33 @@ $|++;

my ($VERSION) = '1.4';

my $rc = EX_SUCCESS;
if (scalar(@ARGV) == 0 || $ARGV[0] !~ m/^-./) {
if (@ARGV) {
foreach my $file (@ARGV) {
if (-d $file) {
warn "$Program: '$file' is a directory\n";
$rc = EX_FAILURE;
next;
}
my $fh;
unless (open $fh, '<', $file) {
warn "$Program: cannot open '$file': $!\n";
$rc = EX_FAILURE;
next;
}
rev($fh);
if ($!) {
warn "$Program: '$file': $!\n";
$rc = EX_FAILURE;
}
}
} else {
rev(*STDIN);
}
my %opt;
getopts('v', \%opt) or usage();
if ($opt{'v'}) {
print "$Program $VERSION\n";
exit EX_SUCCESS;
}
elsif ($ARGV[0] eq '--version') {
print " $Program $VERSION\n";
}
else {
print <<EOF;
Usage: $Program [OPTION] [FILE]

Reverses lines of the named file or the text input on STDIN

Options:
--version: Print version number, then exit.
--help || -h: Print usage, then exit;

EOF
exit EX_FAILURE;
my $rc = EX_SUCCESS;
foreach my $file (@ARGV) {
if (-d $file) {
warn "$Program: '$file' is a directory\n";
$rc = EX_FAILURE;
next;
}
my $fh;
unless (open $fh, '<', $file) {
warn "$Program: cannot open '$file': $!\n";
$rc = EX_FAILURE;
next;
}
rev($fh);
if ($!) {
warn "$Program: '$file': $!\n";
$rc = EX_FAILURE;
}
}

rev(*STDIN) unless @ARGV;
exit $rc;

sub rev {
Expand All @@ -78,6 +64,19 @@ sub rev {
}
}

sub usage {
print <<EOF;
Usage: $Program [OPTION] [FILE]...

Reverse lines of the named file(s) or the text input on STDIN

Options:
-v Print version number, then exit.

EOF
exit EX_FAILURE;
}

__END__

=pod
Expand All @@ -88,7 +87,7 @@ rev - reverse lines of a file

=head1 SYNOPSIS

rev [options] [file]
rev [-v] [file]...

=head1 DESCRIPTION

Expand All @@ -102,13 +101,9 @@ I<rev> accepts the following options:

=over 4

=item --help || -h

Print a short help message, then exits.

=item --version
=item -v

Prints out its version number, then exits.
Print version number then exit

=back

Expand Down
Loading