Skip to content

Commit 2f381a8

Browse files
authored
ping: fatal error for unknown options (#477)
* Print usage and exit for invalid option -x * Include -n flag in usage string * Throw error if extra arguments appear (closer to BSD version) * test1: "perl ping" --> error; no host * test2: "perl ping -x" --> error; no -x flag * test3: "perl ping 127.0.0.1 10" --> success; localhost with timeout of 10 seconds * test4: "perl ping 8.8.8.8 10 11" --> error; 11 means nothing
1 parent 58c19c9 commit 2f381a8

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

bin/ping

+11-4
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,19 @@ use Getopt::Std;
1717
use Socket;
1818
use Net::Ping;
1919

20-
my %opt;
21-
getopts('nI:',\%opt);
22-
die "Usage: $0 host [timeout]\n" unless @ARGV;
20+
sub usage {
21+
require Pod::Usage;
22+
Pod::Usage::pod2usage({ -exitval => 1, -verbose => 0 });
23+
}
2324

25+
my %opt;
26+
getopts('nI:', \%opt) or usage();
2427
my $host = shift;
25-
my $timeout = (@ARGV) ? shift : 20;
28+
my $timeout = shift;
29+
usage() unless defined $host;
30+
$timeout = 20 unless defined $timeout;
31+
usage() if @ARGV;
32+
2633
my $a = gethostbyname($host);
2734

2835
if ( $a ) {

0 commit comments

Comments
 (0)