Skip to content

Commit 8ac51b1

Browse files
authored
rev: terminate options with -- (#439)
* GNU and OpenBSD versions of rev use getopt(), so they support '--' for terminating options; add getopt() here too * Remove --help (-h); usage string is still printed * Update SYNOPSIS and usage() to indicate multiple input files can be processed * test1: "perl rev -- -v 01 02" --> 3 input files * test2: "perl rev -x" --> bad option
1 parent 7ed0b25 commit 8ac51b1

File tree

1 file changed

+41
-46
lines changed

1 file changed

+41
-46
lines changed

bin/rev

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ License: gpl
1414
use strict;
1515

1616
use File::Basename qw(basename);
17+
use Getopt::Std qw(getopts);
1718

1819
use constant EX_SUCCESS => 0;
1920
use constant EX_FAILURE => 1;
@@ -25,48 +26,33 @@ $|++;
2526

2627
my ($VERSION) = '1.4';
2728

28-
my $rc = EX_SUCCESS;
29-
if (scalar(@ARGV) == 0 || $ARGV[0] !~ m/^-./) {
30-
if (@ARGV) {
31-
foreach my $file (@ARGV) {
32-
if (-d $file) {
33-
warn "$Program: '$file' is a directory\n";
34-
$rc = EX_FAILURE;
35-
next;
36-
}
37-
my $fh;
38-
unless (open $fh, '<', $file) {
39-
warn "$Program: cannot open '$file': $!\n";
40-
$rc = EX_FAILURE;
41-
next;
42-
}
43-
rev($fh);
44-
if ($!) {
45-
warn "$Program: '$file': $!\n";
46-
$rc = EX_FAILURE;
47-
}
48-
}
49-
} else {
50-
rev(*STDIN);
51-
}
29+
my %opt;
30+
getopts('v', \%opt) or usage();
31+
if ($opt{'v'}) {
32+
print "$Program $VERSION\n";
33+
exit EX_SUCCESS;
5234
}
53-
elsif ($ARGV[0] eq '--version') {
54-
print " $Program $VERSION\n";
55-
}
56-
else {
57-
print <<EOF;
58-
Usage: $Program [OPTION] [FILE]
59-
60-
Reverses lines of the named file or the text input on STDIN
61-
62-
Options:
63-
--version: Print version number, then exit.
64-
--help || -h: Print usage, then exit;
6535

66-
EOF
67-
exit EX_FAILURE;
36+
my $rc = EX_SUCCESS;
37+
foreach my $file (@ARGV) {
38+
if (-d $file) {
39+
warn "$Program: '$file' is a directory\n";
40+
$rc = EX_FAILURE;
41+
next;
42+
}
43+
my $fh;
44+
unless (open $fh, '<', $file) {
45+
warn "$Program: cannot open '$file': $!\n";
46+
$rc = EX_FAILURE;
47+
next;
48+
}
49+
rev($fh);
50+
if ($!) {
51+
warn "$Program: '$file': $!\n";
52+
$rc = EX_FAILURE;
53+
}
6854
}
69-
55+
rev(*STDIN) unless @ARGV;
7056
exit $rc;
7157

7258
sub rev {
@@ -78,6 +64,19 @@ sub rev {
7864
}
7965
}
8066

67+
sub usage {
68+
print <<EOF;
69+
Usage: $Program [OPTION] [FILE]...
70+
71+
Reverse lines of the named file(s) or the text input on STDIN
72+
73+
Options:
74+
-v Print version number, then exit.
75+
76+
EOF
77+
exit EX_FAILURE;
78+
}
79+
8180
__END__
8281
8382
=pod
@@ -88,7 +87,7 @@ rev - reverse lines of a file
8887
8988
=head1 SYNOPSIS
9089
91-
rev [options] [file]
90+
rev [-v] [file]...
9291
9392
=head1 DESCRIPTION
9493
@@ -102,13 +101,9 @@ I<rev> accepts the following options:
102101
103102
=over 4
104103
105-
=item --help || -h
106-
107-
Print a short help message, then exits.
108-
109-
=item --version
104+
=item -v
110105
111-
Prints out its version number, then exits.
106+
Print version number then exit
112107
113108
=back
114109

0 commit comments

Comments
 (0)