Getopt::Std::WithCheck - Perl extension for process command line arguments with custom check on them
use Getopt::Std::WithCheck; my %opts = ('d' => {'argument' => 0, 'default' => 0, 'description' => "Print debug info", }, ); my %CFG = %{Getopt::Std::WithCheck::getOpts('programName', "Example of Getopt::Std::WithCheck usage\n\n", \%opts)}; if ($CFG{'d'}) { print STDERR Getopt::Std::WithCheck::usage(); };
Getopt::Std::WithCheck provides a simple way to proccess command line arguments check.
Also, basic "usage" functionality provided.
getOpts($programName, $programDescription, $PARAMHASHREF);
Returns a hash contains all the parameters with values set in command line or provided by default.
checkRoutine (see below) is called for each parameter to check it.
checkRoutine
Die with usage message in case of required parameter is not defined.
usage
Parameters of this method are
$programName
Name of the program, will be used in usage message.
$programDescription
Description of the program, will be used in usage message
%{$PARAMHASHREF} contains the letters will be used for command line options as keys.
Values must be a reference to hash describing parameter
This parameter description should contain following keys:
argument
Boolean value indicated will this parameter followed by argument or not.
Default is not to take argument.
default
Default value for this parameter.
Note: you can set default to undef to indicate required parameter. In case required parameter is not provided getOpts(); method will die with usage message.
undef
getOpts();
Default is undef
description
Description of this parameter. will be used in usage message.
Default is empty string.
Reference to a subroutine called to check a validity of this paramneter.
Called with one argument - the parameter itself. Value returned is used as parameter value.
Default routine is simple return parameter back to use it as is.
Note: if 'h' parameter is not specified, default procedure is used for it. Default procedure is to print usage message to STDERR and exit with 0 exit code.
'h'
STDERR
0
In addition to parameters processing getOpts creatina an usage message. This message is stored inside of module and can be accesses by usage method.
getOpts
getOpts($programName, $programDescription, $PARAMLISTREF);
Another form of getOpts.
@{$PARAMLISTREF} should contain parameters description
@{$PARAMLISTREF}
$PARAMLISTREF->[0]
Parameter letter, like a key in %{$PARAMHASHREF} %{$PARAMHASHREF} is used as parameters description
%{$PARAMHASHREF}
$PARAMLISTREF->[1]
Boolean value indicated will this parameter followed by argument or not, like a argument in %{$PARAMHASHREF}
$PARAMLISTREF->[2]
Default value for this parameter, like a default in %{$PARAMHASHREF}
$PARAMLISTREF->[3]
Description for this parameter, like a description in %{$PARAMHASHREF}
$PARAMLISTREF->[4]
Check routine for this parameter, like a checkRoutine in %{$PARAMHASHREF}
usage($programName, $programDescription, $PARAMHASHREF);
returns a usage message based on parameters passed (see getOpts).
usage($programName, $programDescription, $PARAMLISTREF);
usage()
returns a usage message based on previous call of usage or getOpts.
None by default.
Getopt::Std
Daniel Podolsky, <tpaba@cpan.org>
Copyright (C) 2007 by Daniel Podolsky, <tpaba@cpan.org>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.4 or, at your option, any later version of Perl 5 you may have available.
To install Getopt::Std::WithCheck, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Getopt::Std::WithCheck
CPAN shell
perl -MCPAN -e shell install Getopt::Std::WithCheck
For more information on module installation, please visit the detailed CPAN module installation guide.