| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Getopt::Kingpin::Base; | 
| 2 | 28 |  |  | 28 |  | 402 | use 5.008001; | 
|  | 28 |  |  |  |  | 90 |  | 
| 3 | 28 |  |  | 28 |  | 137 | use strict; | 
|  | 28 |  |  |  |  | 45 |  | 
|  | 28 |  |  |  |  | 549 |  | 
| 4 | 28 |  |  | 28 |  | 150 | use warnings; | 
|  | 28 |  |  |  |  | 45 |  | 
|  | 28 |  |  |  |  | 918 |  | 
| 5 | 28 |  |  | 28 |  | 148 | use Object::Simple -base; | 
|  | 28 |  |  |  |  | 68 |  | 
|  | 28 |  |  |  |  | 151 |  | 
| 6 | 28 |  |  | 28 |  | 2924 | use Carp; | 
|  | 28 |  |  |  |  | 54 |  | 
|  | 28 |  |  |  |  | 1600 |  | 
| 7 | 28 |  |  | 28 |  | 20008 | use Path::Tiny; | 
|  | 28 |  |  |  |  | 310851 |  | 
|  | 28 |  |  |  |  | 12435 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | our $VERSION = "0.09"; | 
| 10 |  |  |  |  |  |  | our $types; | 
| 11 |  |  |  |  |  |  | sub AUTOLOAD { | 
| 12 | 401 |  |  | 401 |  | 2386 | my $self = shift; | 
| 13 | 401 |  |  |  |  | 635 | my $func = our $AUTOLOAD; | 
| 14 | 401 |  |  |  |  | 1533 | $func =~ s/.*:://; | 
| 15 | 401 |  |  |  |  | 1016 | my $type = _camelize($func); | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 401 |  |  |  |  | 1123 | $self->_set_types($type); | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 400 |  |  |  |  | 6876 | $self->type($type); | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 400 |  |  |  |  | 3103 | return $self; | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | sub DESTROY { | 
| 25 | 259 |  |  | 259 |  | 137385 | return 1; | 
| 26 |  |  |  |  |  |  | } | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | sub _set_types { | 
| 29 | 659 |  |  | 659 |  | 902 | my $self = shift; | 
| 30 | 659 |  |  |  |  | 1133 | my ($type) = @_; | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 659 | 100 |  |  |  | 1586 | if (not exists $types->{$type}) { | 
| 33 | 68 |  |  |  |  | 317 | my $module = sprintf "Getopt::Kingpin::Type::%s", $type; | 
| 34 | 68 |  |  |  |  | 174 | $module =~ s/List$//; | 
| 35 | 68 | 100 |  |  |  | 609 | if (not $module->can('set_value')) { | 
| 36 | 61 | 100 |  |  |  | 3719 | croak "type error '$type'" unless eval "require $module"; ## no critic | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  | $types->{$type} = { | 
| 39 | 66 |  |  |  |  | 427 | type      => \&{"${module}::type"}, | 
| 40 | 66 |  |  |  |  | 204 | set_value => \&{"${module}::set_value"}, | 
|  | 66 |  |  |  |  | 389 |  | 
| 41 |  |  |  |  |  |  | }; | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 657 | 100 |  |  |  | 1421 | if ($type eq "Bool") { | 
| 45 | 349 | 100 |  |  |  | 5723 | if (not defined $self->_default) { | 
| 46 | 205 |  |  |  |  | 4927 | $self->_default(0); | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 657 | 100 |  |  |  | 3355 | if ($type =~ /List$/) { | 
| 51 | 45 |  |  |  |  | 661 | $self->is_cumulative(1); | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | sub _camelize { | 
| 56 | 401 |  |  | 401 |  | 626 | my $c = shift; | 
| 57 | 401 |  |  |  |  | 1443 | $c =~ s/(^|_)(.)/uc($2)/ge; | 
|  | 441 |  |  |  |  | 1791 |  | 
| 58 | 401 |  |  |  |  | 972 | return $c; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | use overload ( | 
| 62 | 371 | 100 |  | 371 |  | 60019 | '""' => sub {defined $_[0]->value ? $_[0]->value : ""}, | 
| 63 | 28 |  |  |  |  | 200 | fallback => 1, | 
| 64 | 28 |  |  | 28 |  | 275 | ); | 
|  | 28 |  |  |  |  | 72 |  | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | has name          => undef; | 
| 67 |  |  |  |  |  |  | has short_name    => undef; | 
| 68 |  |  |  |  |  |  | has description   => undef; | 
| 69 |  |  |  |  |  |  | has value         => undef; | 
| 70 |  |  |  |  |  |  | has _defined      => 0; | 
| 71 |  |  |  |  |  |  | has is_cumulative => 0; | 
| 72 |  |  |  |  |  |  | has _default      => undef; | 
| 73 |  |  |  |  |  |  | has _envar        => undef; | 
| 74 |  |  |  |  |  |  | has type          => "String"; | 
| 75 |  |  |  |  |  |  | has _required     => 0; | 
| 76 |  |  |  |  |  |  | has index         => 0; | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | sub short { | 
| 79 | 31 |  |  | 31 | 1 | 81 | my $self = shift; | 
| 80 | 31 |  |  |  |  | 55 | my ($short_name) = @_; | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 31 |  |  |  |  | 485 | $self->short_name($short_name); | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 31 |  |  |  |  | 296 | return $self; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | sub default { | 
| 88 | 22 |  |  | 22 | 1 | 36 | my $self = shift; | 
| 89 | 22 |  |  |  |  | 44 | my ($default) = @_; | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 22 |  |  |  |  | 365 | $self->_default($default); | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 22 |  |  |  |  | 304 | return $self; | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | sub override_default_from_envar { | 
| 97 | 6 |  |  | 6 | 1 | 9 | my $self = shift; | 
| 98 | 6 |  |  |  |  | 12 | my ($envar_name) = @_; | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 6 | 100 |  |  |  | 18 | if (exists $ENV{$envar_name}) { | 
| 101 | 5 |  |  |  |  | 77 | $self->_envar($ENV{$envar_name}); | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  |  | 
| 104 | 6 |  |  |  |  | 72 | return $self; | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | sub required { | 
| 108 | 49 |  |  | 49 | 1 | 87 | my $self = shift; | 
| 109 | 49 |  |  |  |  | 779 | $self->_required(1); | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 49 |  |  |  |  | 511 | return $self; | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | sub set_value { | 
| 115 | 258 |  |  | 258 | 1 | 1112 | my $self = shift; | 
| 116 | 258 |  |  |  |  | 3682 | my $type = $self->type; | 
| 117 | 258 |  |  |  |  | 1840 | $self->_set_types($type); | 
| 118 | 257 | 100 |  |  |  | 4013 | if ($self->is_cumulative) { | 
|  |  | 100 |  |  |  |  |  | 
| 119 | 24 |  |  |  |  | 125 | my @values; | 
| 120 | 24 | 100 |  |  |  | 323 | if ($self->_defined) { | 
| 121 | 13 |  |  |  |  | 68 | @values = @{$self->value}; | 
|  | 13 |  |  |  |  | 178 |  | 
| 122 |  |  |  |  |  |  | } | 
| 123 | 24 |  |  |  |  | 200 | my @ret = $types->{$type}->{set_value}->($self, @_); | 
| 124 | 24 | 100 |  |  |  | 47 | if (scalar @ret > 1) { | 
| 125 | 2 |  |  |  |  | 8 | return undef, $ret[1]; | 
| 126 |  |  |  |  |  |  | } | 
| 127 | 22 |  |  |  |  | 37 | push @values, $ret[0]; | 
| 128 | 22 |  |  |  |  | 304 | $self->_defined(1); | 
| 129 | 22 |  |  |  |  | 369 | $self->value([@values]); | 
| 130 |  |  |  |  |  |  | } elsif ($self->_defined) { | 
| 131 | 1 |  |  |  |  | 39 | printf STDERR "error: flag '%s' cannot be repeated, try --help\n", $self->name; | 
| 132 | 1 |  |  |  |  | 70 | return undef, 1; | 
| 133 |  |  |  |  |  |  | } else { | 
| 134 | 232 |  |  |  |  | 9376 | $self->_defined(1); | 
| 135 | 232 |  |  |  |  | 1856 | my @ret = $types->{$type}->{set_value}->($self, @_); | 
| 136 | 232 | 100 |  |  |  | 582 | if (scalar @ret > 1) { | 
| 137 |  |  |  |  |  |  | # has error | 
| 138 | 12 |  |  |  |  | 53 | return undef, $ret[1]; | 
| 139 |  |  |  |  |  |  | } | 
| 140 | 220 |  |  |  |  | 3556 | return $self->value($ret[0]); | 
| 141 |  |  |  |  |  |  | } | 
| 142 |  |  |  |  |  |  | } | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | 1; | 
| 145 |  |  |  |  |  |  | __END__ |