Skip to content

Commit 87636ea

Browse files
authored
apply: improve parsing of -a option (#806)
* When testing against OpenBSD, the -a option argument can be given with or without a leading space * Allow this version to accept "-a c" form to avoid confusion * Sync usage string with BSD version: "[-a c]" instead of [-ac] which could be confused as grouped options -a and -c * If a bad option was given, e.g. -x, print the bad option before the usage string * If the argument to -a is not 1 character, reject it and terminate the program * test1: perl apply -a'!' 'echo HELLO, !1' e*.c # say hello to my e files * test2: perl apply -a '!' 'echo HELLO, !1' e*.c # same * test3: perl apply 'echo HELLO, %1' e*.c # same, without -a
1 parent 77ce1d4 commit 87636ea

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

bin/apply

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use constant EX_SUCCESS => 0;
2121
use constant EX_FAILURE => 1;
2222

2323
my $Program = basename($0);
24-
my ($VERSION) = '1.4';
24+
my ($VERSION) = '1.5';
2525

2626
my $argc = 1;
2727
my $debug = 0;
@@ -36,8 +36,20 @@ while (@ARGV) {
3636
last;
3737
}
3838

39-
if ($ARGV[0] =~ m/\A\-a(.)\Z/) {
40-
$magic = $1;
39+
if ($ARGV[0] =~ s/\A\-a//) {
40+
$magic = $ARGV[0];
41+
unless (length $magic) {
42+
shift;
43+
$magic = $ARGV[0];
44+
}
45+
unless (length $magic) {
46+
warn "$Program: option -a requires an argument\n";
47+
usage();
48+
}
49+
if (length($magic) > 1) {
50+
warn "$Program: invalid magic specification\n";
51+
usage();
52+
}
4153
}
4254
elsif ($ARGV[0] =~ m/\A\-(\d+)\Z/) {
4355
$argc = $1;
@@ -46,6 +58,7 @@ while (@ARGV) {
4658
$debug = 1;
4759
}
4860
else {
61+
warn "$Program: invalid option: $ARGV[0]\n";
4962
usage(); # usage will exit
5063
}
5164
shift;
@@ -98,8 +111,7 @@ sub run_cmd {
98111
}
99112

100113
sub usage {
101-
warn "$Program (Perl bin utils) $VERSION\n";
102-
warn "usage: $Program [-ac] [-d] [-#] command argument [argument ...]\n";
114+
warn "usage: $Program [-a c] [-d] [-#] command argument [argument ...]\n";
103115
exit EX_FAILURE;
104116
}
105117

@@ -113,7 +125,7 @@ apply - Run a command many times with different arguments
113125
114126
=head1 SYNOPSIS
115127
116-
apply [-aB<c>] [-d] [-#] command argument [argument ...]
128+
apply [-a B<c>] [-d] [-#] command argument [argument ...]
117129
118130
=head1 DESCRIPTION
119131
@@ -128,7 +140,7 @@ I<apply> accepts the following options:
128140
129141
=over 4
130142
131-
=item -aB<c>
143+
=item -a B<c>
132144
133145
Use the character B<c> instead of B<%> for interpolation of arguments.
134146

0 commit comments

Comments
 (0)