Sign in to PrePAN

Getopts::toHash Take/validate command line arguments

In Review


use Getopts::toHash;

my $Args = Args->get_em();

$Args->validate( '-a'=>[ 'ASCII','NOSPACES',{ 'MINMAX'=>{min=>20,max=>40} } ] );


my %spec = ( '-a'=>[ 'ASCII','NOSPACES', {'MINMAX'=>{min=>20,max=>40} } ] );


I have never submitted a module before but I think this could be useful to other people as I did a search on cpanm for something that would easily pass a hash back of command line arguments and validate them (Validation is optional). Future plans include custom die messages and aliases for aguments. Let me know what you guys think if this would be useful, If you see anything that wouldnt work, possibly some advice on namespace etc. Thanks

--Validation The Module can validate on 7 Different constraints REGEXP - {'REGEXP=>'^[-+]?[0-9]'} - Specify a regex the passed data must comply with. MINMAX - {'MINMAX'=>{min=>20,max=>40}} - Specify minimum and maximum numbers the argument must be. GREATER_THAN - {'GREATER_THAN'=>{min=>20}} - Specify a minimum number the argument must be. LESS_THAN - {'LESS_THAN'=>{max=>40}} - Specify a maximum number the argument must be lower than. ASCII - ['ASCII'] - The argument must be any ASCII text with spaces allowed. NOSPACES - ['NOSPACES'] - The argument must have no spaces. INT - ['INT'] - The argument must be a number. ANY - ['ANY'] - The argument must be defined.

You can validate each argument on as many types of validation as you want by stringing together each array ref or hash. The Argument must pass each constraint

--Example 1 - Validating with a inline object. $Args->validate( '-a'=>[ 'ASCII','NOSPACES',{ 'MINMAX'=>{min=>20,max=>40} } ] );

--Example 2 - Validating with a hash spec. my %spec = ( '-a'=>[ 'ASCII','NOSPACES', {'MINMAX'=>{min=>20,max=>40} } ] ); $Args->validate(%spec);

The module will display error messages and die on failed validation by default. If you dont want this you can pass... $Args->validate( no_errors=>'1','-a'=>[ 'ASCII','NOSPACES',{ 'MINMAX'=>{min=>20,max=>40} } ] );


hello, for sure the namespace is not so meaningful: what the AM in AM::Args means? I'm not a module guru but I suspect you can extend the Getopt::* namespace as already done by many modules on CPAN.
In particular see Getopt::Valid that seems close to your intentions.
Go on and good luck!
AM is how I organize locally, I just read the pause page on naming a module hopefully the change is a bit more fitting. Thanks for the input.
Note that the current top-level namespace is Getopt (singular). It would be better to slot into this than create another top-level that is just the plural.

Please sign up to post a review.