| 1 | package Getopt::Kingpin::Base; | ||||||||||||||||||
| 2 | 31 | ▶ | use 5.008001; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 3 | 31 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 31 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 31 | ▶ | use Object::Simple -base; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | 31 | ▶ | use Carp; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | 31 | ▶ | use Path::Tiny; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | 31 | our $VERSION = "0.10"; | |||||||||||||||||
| 10 | 31 | our $types; | |||||||||||||||||
| 11 | sub AUTOLOAD { | ||||||||||||||||||
| 12 | 511 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 13 | 511 | my $func = our $AUTOLOAD; | |||||||||||||||||
| 14 | 511 | $func =~ s/.*:://; | |||||||||||||||||
| 15 | 511 | my $type = _camelize($func); | |||||||||||||||||
| 16 | |||||||||||||||||||
| 17 | 511 | $self->_set_types($type); | |||||||||||||||||
| 18 | |||||||||||||||||||
| 19 | 510 | $self->type($type); | |||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | 510 | return $self; | |||||||||||||||||
| 22 | } | ||||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | sub DESTROY { | ||||||||||||||||||
| 25 | 351 | ▶ | return 1; | ||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 26 | } | ||||||||||||||||||
| 27 | |||||||||||||||||||
| 28 | sub _set_types { | ||||||||||||||||||
| 29 | 855 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
_set_types: called CC=8 SCAR=20.8
| |||||||||||||||||||
| 30 | 855 | my ($type) = @_; | |||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | 855 | ▶ | if (not exists $types->{$type}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 33 | 82 | my $module = sprintf "Getopt::Kingpin::Type::%s", $type; | |||||||||||||||||
| 34 | 82 | $module =~ s/(?:List|Hash)$//; | |||||||||||||||||
| 35 | 82 | ▶ | if (not $module->can('set_value')) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 36 | 71 | ▶ | croak "type error '$type'" unless eval "require $module"; ## no critic | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 37 | } | ||||||||||||||||||
| 38 | $types->{$type} = { | ||||||||||||||||||
| 39 | 80 | type => \&{"${module}::type"}, | |||||||||||||||||
| 40 | 80 | set_value => \&{"${module}::set_value"}, | |||||||||||||||||
| 41 | }; | ||||||||||||||||||
| 42 | } | ||||||||||||||||||
| 43 | |||||||||||||||||||
| 44 | 853 | ▶ | if ($type eq "Bool") { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 45 | 424 | ▶ | if (not defined $self->_default) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 46 | 247 | $self->_default(0); | |||||||||||||||||
| 47 | } | ||||||||||||||||||
| 48 | } | ||||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | 853 | ▶ | if ($type =~ /List$/) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 51 | 62 | $self->is_cumulative(1); | |||||||||||||||||
| 52 | } | ||||||||||||||||||
| 53 | |||||||||||||||||||
| 54 | elsif ($type =~ /Hash$/) { | ||||||||||||||||||
| 55 | 55 | $self->is_hash(1); | |||||||||||||||||
| 56 | } | ||||||||||||||||||
| 57 | } | ||||||||||||||||||
| 58 | |||||||||||||||||||
| 59 | sub _camelize { | ||||||||||||||||||
| 60 | 511 | ▶ | my $c = shift; | ||||||||||||||||
|
Subroutine
_camelize: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 61 | 511 | $c =~ s/(^|_)(.)/uc($2)/ge; | |||||||||||||||||
| 62 | 511 | return $c; | |||||||||||||||||
| 63 | } | ||||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | use overload ( | ||||||||||||||||||
| 66 | 457 | ▶ | '""' => sub {defined $_[0]->value ? $_[0]->value : ""}, | ||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||
| 67 | 31 | fallback => 1, | |||||||||||||||||
| 68 | 31 | ▶ | ); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 69 | |||||||||||||||||||
| 70 | 31 | has name => undef; | |||||||||||||||||
| 71 | 31 | has short_name => undef; | |||||||||||||||||
| 72 | 31 | has description => undef; | |||||||||||||||||
| 73 | 31 | has value => undef; | |||||||||||||||||
| 74 | 31 | has _defined => 0; | |||||||||||||||||
| 75 | 31 | has is_cumulative => 0; | |||||||||||||||||
| 76 | 31 | has is_hash => 0; | |||||||||||||||||
| 77 | 31 | has _default => undef; | |||||||||||||||||
| 78 | 31 | has _envar => undef; | |||||||||||||||||
| 79 | 31 | has type => "String"; | |||||||||||||||||
| 80 | 31 | has _required => 0; | |||||||||||||||||
| 81 | 31 | has index => 0; | |||||||||||||||||
| 82 | |||||||||||||||||||
| 83 | sub short { | ||||||||||||||||||
| 84 | 31 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
short: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 85 | 31 | my ($short_name) = @_; | |||||||||||||||||
| 86 | |||||||||||||||||||
| 87 | 31 | $self->short_name($short_name); | |||||||||||||||||
| 88 | |||||||||||||||||||
| 89 | 31 | return $self; | |||||||||||||||||
| 90 | } | ||||||||||||||||||
| 91 | |||||||||||||||||||
| 92 | sub default { | ||||||||||||||||||
| 93 | 43 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
default: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 94 | 43 | my ($default) = @_; | |||||||||||||||||
| 95 | |||||||||||||||||||
| 96 | 43 | $self->_default($default); | |||||||||||||||||
| 97 | |||||||||||||||||||
| 98 | 43 | return $self; | |||||||||||||||||
| 99 | } | ||||||||||||||||||
| 100 | |||||||||||||||||||
| 101 | sub override_default_from_envar { | ||||||||||||||||||
| 102 | 6 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
override_default_from_envar: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 103 | 6 | my ($envar_name) = @_; | |||||||||||||||||
| 104 | |||||||||||||||||||
| 105 | 6 | ▶ | if (exists $ENV{$envar_name}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 106 | 5 | $self->_envar($ENV{$envar_name}); | |||||||||||||||||
| 107 | } | ||||||||||||||||||
| 108 | |||||||||||||||||||
| 109 | 6 | return $self; | |||||||||||||||||
| 110 | } | ||||||||||||||||||
| 111 | |||||||||||||||||||
| 112 | sub required { | ||||||||||||||||||
| 113 | 52 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
required: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 114 | 52 | $self->_required(1); | |||||||||||||||||
| 115 | |||||||||||||||||||
| 116 | 52 | return $self; | |||||||||||||||||
| 117 | } | ||||||||||||||||||
| 118 | |||||||||||||||||||
| 119 | sub set_value { | ||||||||||||||||||
| 120 | 344 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
set_value: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||
| 121 | 344 | my $type = $self->type; | |||||||||||||||||
| 122 | 344 | $self->_set_types($type); | |||||||||||||||||
| 123 | 343 | ▶ | if ($self->is_cumulative) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 124 | 32 | my @values; | |||||||||||||||||
| 125 | 32 | ▶ | if ($self->_defined) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 126 | 17 | @values = @{$self->value}; | |||||||||||||||||
| 127 | } | ||||||||||||||||||
| 128 | 32 | my @ret = $types->{$type}->{set_value}->($self, @_); | |||||||||||||||||
| 129 | 32 | ▶ | if (scalar @ret > 1) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 130 | 2 | return undef, $ret[1]; | |||||||||||||||||
| 131 | } | ||||||||||||||||||
| 132 | 30 | push @values, $ret[0]; | |||||||||||||||||
| 133 | 30 | $self->_defined(1); | |||||||||||||||||
| 134 | 30 | $self->value(\@values); | |||||||||||||||||
| 135 | } elsif ($self->is_hash) { | ||||||||||||||||||
| 136 | 31 | my %values; | |||||||||||||||||
| 137 | 31 | ▶ | if ($self->_defined) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 138 | 18 | %values = %{$self->value}; | |||||||||||||||||
| 139 | } | ||||||||||||||||||
| 140 | 31 | ▶ | my ($key, $val) = (ref($_[0]) eq 'ARRAY') ? @{$_[0]} : split(/=/, $_[0], 2); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 141 | 31 | my @ret = $types->{$type}->{set_value}->($self, $val); | |||||||||||||||||
| 142 | 31 | ▶ | if (scalar @ret > 1) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 143 | 2 | return undef, $ret[1]; | |||||||||||||||||
| 144 | } | ||||||||||||||||||
| 145 | 29 | $values{$key} = $ret[0]; | |||||||||||||||||
| 146 | 29 | $self->_defined(1); | |||||||||||||||||
| 147 | 29 | $self->value(\%values); | |||||||||||||||||
| 148 | } elsif ($self->_defined) { | ||||||||||||||||||
| 149 | 2 | printf STDERR "error: flag '%s' cannot be repeated, try --help\n", $self->name; | |||||||||||||||||
| 150 | 2 | return undef, 1; | |||||||||||||||||
| 151 | } else { | ||||||||||||||||||
| 152 | 278 | $self->_defined(1); | |||||||||||||||||
| 153 | 278 | my @ret = $types->{$type}->{set_value}->($self, @_); | |||||||||||||||||
| 154 | 278 | ▶ | if (scalar @ret > 1) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 155 | # has error | ||||||||||||||||||
| 156 | 13 | return undef, $ret[1]; | |||||||||||||||||
| 157 | } | ||||||||||||||||||
| 158 | 265 | return $self->value($ret[0]); | |||||||||||||||||
| 159 | } | ||||||||||||||||||
| 160 | } | ||||||||||||||||||
| 161 | |||||||||||||||||||
| 162 | 31 | 1; | |||||||||||||||||
| 163 | __END__ | ||||||||||||||||||