perl - GetOptions Check Option Values -
i updating existing perl script uses getoptions
getopt::long. want add option takes string parameter , can have 1 of 3 values: small, medium, or large. there way make perl throw error or kill script if other string value specified? far have:
my $value = 'small'; getoptions('size=s' => \$value);
you use subroutine handle processing of option. user-defined subroutines handle options
my $size = 'small'; # default getoptions('size=s' => \&size); print "$size\n"; sub size { %sizes = ( small => 1, medium => 1, large => 1 ); if (! exists $sizes{$_[1]}) { # die "$_[1] not valid size\n"; # changing use exit statement works expected print "$_[1] not valid size\n"; exit; } $size = $_[1]; }
i put sizes hash, use array , grep toolic showed.
Comments
Post a Comment