perl - How to use GetOptions to detect trailing strings? -
i absolutely new perl , trying figure out problem perl script parsing script arguments.
i have following perl script called sample-perl.pl:
use strict; use warnings; use 5.010; use getopt::long qw(getoptions); $source_address; $dest_address; getoptions('from=s' => \$source_address, 'to=s' => \$dest_address) or die "usage: $0 --from name --to name\n"; if ($source_address) { $source_address; } if ($dest_address) { $dest_address; }
and if use command (where forgot enter second option):
perl sample-perl.pl --from nyc lon output be: nyc
how can enforce if there additional string @ end, detected , error displayed instead?
solution:
adding case @ least:
if(@argv){ //throw error }
after calling getoptions
, check remaining command line options in @argv
array. assumes unexpected arguments generate error:
use strict; use warnings; use 5.010; use getopt::long qw(getoptions); $source_address; $dest_address; getoptions('from=s' => \$source_address, 'to=s' => \$dest_address) or die "usage: $0 --from name --to name\n"; @argv , die "error: unexpected args: @argv"; if ($source_address) { $source_address; } if ($dest_address) { $dest_address; }
Comments
Post a Comment