opts
view release on metacpan or search on metacpan
lib/opts.pm view on Meta::CPAN
package opts;
use strict;
use warnings;
our $VERSION = '0.08';
use Exporter 'import';
use PadWalker qw/var_name/;
use Getopt::Long;
use Carp ();
our @EXPORT = qw/opts/;
our $TYPE_CONSTRAINT = {
'Bool' => '!',
'Str' => '=s',
'Int' => '=i',
'Num' => '=f',
'ArrayRef' => '=s@',
'HashRef' => '=s%',
};
my %is_invocant = map{ $_ => undef } qw($self $class);
my $coerce_type_map = {
Multiple => 'ArrayRef',
};
my $coerce_generater = {
Multiple => sub { [ split(qr{,}, join(q{,}, @{ $_[0] })) ] },
};
sub opts {
{
package DB;
# call of caller in DB package sets @DB::args,
# which requires list context, but does not use return values
() = caller(1);
}
# method call
if(exists $is_invocant{ var_name(1, \$_[0]) || '' }){
$_[0] = shift @DB::args;
shift;
# XXX: should we provide ways to check the type of invocant?
}
# track our coderef defaults
my %default_subs;
my @options = ('help|h!' => \my $help);
my %requireds;
my %generaters;
my $usage;
my @option_help;
for(my $i = 0; $i < @_; $i++){
(my $name = var_name(1, \$_[$i]))
or Carp::croak('usage: opts my $var => TYPE, ...');
$name =~ s/^\$//;
my $rule = _compile_rule($_[$i+1]);
if ($name =~ /_/) {
# Name has underscores in it, which is annoying for command line
# arguments. Swap them and create / add to alias.
( run in 0.649 second using v1.01-cache-2.11-cpan-e1769b4cff6 )