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