App-Spec
view release on metacpan - search on metacpan
view release on metacpan or search on metacpan
lib/App/Spec/Run/Validator.pm view on Meta::CPAN
# ABSTRACT: Processes and validates options and parameters
use strict;
use warnings;
package App::Spec::Run::Validator;
our $VERSION = '0.013'; # VERSION;
use List::Util qw/ any /;
use List::MoreUtils qw/ uniq /;
use Ref::Util qw/ is_arrayref is_hashref /;
use Moo;
has options => ( is => 'ro' );
has option_specs => ( is => 'ro' );
has parameters => ( is => 'ro' );
has param_specs => ( is => 'ro' );
my %validate = (
string => sub { length($_[0]) > 0 },
file => sub { $_[0] eq '-' or -f $_[0] },
filename => sub { 1 },
dir => sub { -d $_[0] },
dirname => sub { 1 },
integer => sub { $_[0] =~ m/^[+-]?\d+$/ },
float => sub { $_[0] =~ m/^[+-]?\d+(?:\.\d+)?$/ },
flag => sub { 1 },
enum => sub {
my ($value, $list) = @_;
any { $value eq $_ } @$list;
},
);
sub process {
my ($self, $run, $errs) = @_;
my ($ok) = $self->_process( $errs, type => "parameters", app => $run );
$ok &&= $self->_process( $errs, type => "options", app => $run );
return $ok;
}
sub _process {
my ($self, $errs, %args) = @_;
my $run = $args{app};
my $type = $args{type};
my ($items, $specs);
if ($args{type} eq "parameters") {
$items = $self->parameters;
$specs = $self->param_specs;
}
else {
$items = $self->options;
$specs = $self->option_specs;
}
# TODO: iterate over parameters in original cmdline order
for my $name (sort keys %$specs) {
my $spec = $specs->{ $name };
my $value = $items->{ $name };
my $param_type = $spec->type;
my $enum = $spec->enum;
if ($spec->type eq "flag") {
if ($spec->multiple) {
if (defined $value and $value !~ m/^\d+$/) {
die "Value for '$name': '$value' shouldn't happen";
}
}
else {
if (defined $value and $value != 1) {
die "Value for '$name': '$value' shouldn't happen";
}
view all matches for this distributionview release on metacpan - search on metacpan
( run in 1.571 second using v1.00-cache-2.02-grep-82fe00e-cpan-72ae3ad1e6da )