Skip to content

Commit 48dabd1

Browse files
authored
cp: print usage for bad options (#609)
* Preserve control structure of returning from run() by passing NOEXIT to pod2usage() * Remove empty line in SYNOPSIS and Bquote which didn't look right in the pod2usage() output * Tested for "perl cp -x" and "perl cp src"
1 parent 3009735 commit 48dabd1

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

bin/cp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ sub run {
4848
return EX_USAGE unless defined $opts;
4949
unless (@files) {
5050
warn "$0: missing file operand\n";
51+
usage();
5152
return EX_USAGE;
5253
}
5354

@@ -101,20 +102,25 @@ sub process_arguments {
101102
my %opts;
102103

103104
require Getopt::Long;
104-
my $ret = Getopt::Long::GetOptionsFromArray(
105+
Getopt::Long::GetOptionsFromArray(
105106
\@args,
106107
'f' => \$opts{'f'},
107108
'i' => \$opts{'i'},
108109
'n' => \$opts{'n'},
109110
'p' => \$opts{'p'},
110111
'v' => \$opts{'v'},
111-
);
112-
113-
return unless $ret;
114-
112+
) or do {
113+
usage();
114+
return;
115+
};
115116
return ( \%opts, @args )
116117
}
117118

119+
sub usage {
120+
require Pod::Usage;
121+
Pod::Usage::pod2usage({ -exitval => 'NOEXIT', -verbose => 0 });
122+
}
123+
118124
__PACKAGE__;
119125

120126
=pod
@@ -127,9 +133,8 @@ cp - copy files and/or directories
127133
128134
=head1 SYNOPSIS
129135
130-
% cp [ B<-fipv> ] source_file target_file
131-
132-
% cp [ B<-fipv> ] source... target_dir
136+
% cp [-fipv] source_file target_file
137+
% cp [-fipv] source... target_dir
133138
134139
=head1 DESCRIPTION
135140

0 commit comments

Comments
 (0)