Friday, March 20, 2009

Option validation in perl

#!/usr/bin/perl
use Getopt::Long;

my ($help, @url, $size);

#-- prints usage if no command line parameters are passed or there is an unknown
# parameter or help option is passed
usage() if ( ! GetOptions('help|?' => \$help, 'url=s' => \@url, 'size=i' => \$size)
or $ARGV[0] or defined $help );
exit;

sub usage
{
print "Unknown option is here: @_\n" if ( @_ );
print "usage: program [--url URL] [--size SIZE] [--help|-?]\n";
exit;
}

No comments: