Skip to content

Commit

Permalink
ping: fatal error for unknown options (#477)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mknos committed Mar 1, 2024
1 parent 58c19c9 commit 2f381a8
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions bin/ping
Expand Up @@ -17,12 +17,19 @@ use Getopt::Std;
use Socket;
use Net::Ping;

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

my %opt;
getopts('nI:', \%opt) or usage();
my $host = shift;
my $timeout = (@ARGV) ? shift : 20;
my $timeout = shift;
usage() unless defined $host;
$timeout = 20 unless defined $timeout;
usage() if @ARGV;

my $a = gethostbyname($host);

if ( $a ) {
Expand Down

0 comments on commit 2f381a8

Please sign in to comment.