line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Getopt::Kingpin::Base; |
2
|
31
|
|
|
31
|
|
521
|
use 5.008001; |
|
31
|
|
|
|
|
114
|
|
3
|
31
|
|
|
31
|
|
146
|
use strict; |
|
31
|
|
|
|
|
74
|
|
|
31
|
|
|
|
|
711
|
|
4
|
31
|
|
|
31
|
|
176
|
use warnings; |
|
31
|
|
|
|
|
72
|
|
|
31
|
|
|
|
|
1109
|
|
5
|
31
|
|
|
31
|
|
250
|
use Object::Simple -base; |
|
31
|
|
|
|
|
104
|
|
|
31
|
|
|
|
|
199
|
|
6
|
31
|
|
|
31
|
|
3660
|
use Carp; |
|
31
|
|
|
|
|
67
|
|
|
31
|
|
|
|
|
1934
|
|
7
|
31
|
|
|
31
|
|
29152
|
use Path::Tiny; |
|
31
|
|
|
|
|
438727
|
|
|
31
|
|
|
|
|
19772
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = "0.11"; |
10
|
|
|
|
|
|
|
our $types; |
11
|
|
|
|
|
|
|
sub AUTOLOAD { |
12
|
511
|
|
|
511
|
|
3098
|
my $self = shift; |
13
|
511
|
|
|
|
|
1008
|
my (@args) = @_; |
14
|
511
|
|
|
|
|
820
|
my $func = our $AUTOLOAD; |
15
|
511
|
|
|
|
|
2139
|
$func =~ s/.*:://; |
16
|
511
|
|
|
|
|
1236
|
my $type = _camelize($func); |
17
|
|
|
|
|
|
|
|
18
|
511
|
|
|
|
|
1472
|
$self->_set_types($type); |
19
|
|
|
|
|
|
|
|
20
|
510
|
|
|
|
|
1940
|
$self->_set_options(@args); |
21
|
|
|
|
|
|
|
|
22
|
510
|
|
|
|
|
9525
|
$self->type($type); |
23
|
|
|
|
|
|
|
|
24
|
510
|
|
|
|
|
4255
|
return $self; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub DESTROY { |
28
|
351
|
|
|
351
|
|
212786
|
return 1; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub _set_types { |
32
|
855
|
|
|
855
|
|
1348
|
my $self = shift; |
33
|
855
|
|
|
|
|
1571
|
my ($type) = @_; |
34
|
|
|
|
|
|
|
|
35
|
855
|
100
|
|
|
|
2183
|
if (not exists $types->{$type}) { |
36
|
82
|
|
|
|
|
407
|
my $module = sprintf "Getopt::Kingpin::Type::%s", $type; |
37
|
82
|
|
|
|
|
334
|
$module =~ s/(?:List|Hash)$//; |
38
|
82
|
100
|
|
|
|
738
|
if (not $module->can('set_value')) { |
39
|
71
|
100
|
|
|
|
4727
|
croak "type error '$type'" unless eval "require $module"; ## no critic |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
$types->{$type} = { |
42
|
80
|
|
|
|
|
609
|
type => \&{"${module}::type"}, |
43
|
80
|
|
|
|
|
279
|
set_value => \&{"${module}::set_value"}, |
|
80
|
|
|
|
|
503
|
|
44
|
|
|
|
|
|
|
}; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
853
|
100
|
|
|
|
2353
|
if ($type eq "Bool") { |
48
|
424
|
100
|
|
|
|
7677
|
if (not defined $self->_default) { |
49
|
247
|
|
|
|
|
6697
|
$self->_default(0); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
853
|
100
|
|
|
|
5339
|
if ($type =~ /List$/) { |
|
|
100
|
|
|
|
|
|
54
|
62
|
|
|
|
|
1096
|
$self->is_cumulative(1); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
elsif ($type =~ /Hash$/) { |
58
|
55
|
|
|
|
|
967
|
$self->is_hash(1); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub _set_options { |
63
|
510
|
|
|
510
|
|
799
|
my $self = shift; |
64
|
510
|
|
|
|
|
853
|
my @options = @_; |
65
|
|
|
|
|
|
|
|
66
|
510
|
|
|
|
|
1541
|
$self->{_options} = \@options; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub _camelize { |
70
|
511
|
|
|
511
|
|
832
|
my $c = shift; |
71
|
511
|
|
|
|
|
1899
|
$c =~ s/(^|_)(.)/uc($2)/ge; |
|
584
|
|
|
|
|
2583
|
|
72
|
511
|
|
|
|
|
1336
|
return $c; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
use overload ( |
76
|
457
|
100
|
|
457
|
|
76689
|
'""' => sub {defined $_[0]->value ? $_[0]->value : ""}, |
77
|
31
|
|
|
|
|
234
|
fallback => 1, |
78
|
31
|
|
|
31
|
|
350
|
); |
|
31
|
|
|
|
|
127
|
|
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
has name => undef; |
81
|
|
|
|
|
|
|
has short_name => undef; |
82
|
|
|
|
|
|
|
has description => undef; |
83
|
|
|
|
|
|
|
has value => undef; |
84
|
|
|
|
|
|
|
has _defined => 0; |
85
|
|
|
|
|
|
|
has is_cumulative => 0; |
86
|
|
|
|
|
|
|
has is_hash => 0; |
87
|
|
|
|
|
|
|
has _default => undef; |
88
|
|
|
|
|
|
|
has _envar => undef; |
89
|
|
|
|
|
|
|
has type => "String"; |
90
|
|
|
|
|
|
|
has _required => 0; |
91
|
|
|
|
|
|
|
has index => 0; |
92
|
|
|
|
|
|
|
has _options => sub { [] }; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub short { |
95
|
31
|
|
|
31
|
1
|
145
|
my $self = shift; |
96
|
31
|
|
|
|
|
68
|
my ($short_name) = @_; |
97
|
|
|
|
|
|
|
|
98
|
31
|
|
|
|
|
559
|
$self->short_name($short_name); |
99
|
|
|
|
|
|
|
|
100
|
31
|
|
|
|
|
378
|
return $self; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub default { |
104
|
43
|
|
|
43
|
1
|
209
|
my $self = shift; |
105
|
43
|
|
|
|
|
91
|
my ($default) = @_; |
106
|
|
|
|
|
|
|
|
107
|
43
|
|
|
|
|
781
|
$self->_default($default); |
108
|
|
|
|
|
|
|
|
109
|
43
|
|
|
|
|
616
|
return $self; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub override_default_from_envar { |
113
|
6
|
|
|
6
|
1
|
10
|
my $self = shift; |
114
|
6
|
|
|
|
|
13
|
my ($envar_name) = @_; |
115
|
|
|
|
|
|
|
|
116
|
6
|
100
|
|
|
|
20
|
if (exists $ENV{$envar_name}) { |
117
|
5
|
|
|
|
|
91
|
$self->_envar($ENV{$envar_name}); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
6
|
|
|
|
|
73
|
return $self; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub required { |
124
|
52
|
|
|
52
|
1
|
91
|
my $self = shift; |
125
|
52
|
|
|
|
|
879
|
$self->_required(1); |
126
|
|
|
|
|
|
|
|
127
|
52
|
|
|
|
|
723
|
return $self; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub set_value { |
131
|
344
|
|
|
344
|
1
|
671
|
my $self = shift; |
132
|
344
|
|
|
|
|
5595
|
my $type = $self->type; |
133
|
344
|
|
|
|
|
2794
|
$self->_set_types($type); |
134
|
343
|
100
|
|
|
|
6122
|
if ($self->is_cumulative) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
135
|
32
|
|
|
|
|
200
|
my @values; |
136
|
32
|
100
|
|
|
|
499
|
if ($self->_defined) { |
137
|
17
|
|
|
|
|
105
|
@values = @{$self->value}; |
|
17
|
|
|
|
|
263
|
|
138
|
|
|
|
|
|
|
} |
139
|
32
|
|
|
|
|
295
|
my @ret = $types->{$type}->{set_value}->($self, @_); |
140
|
32
|
100
|
|
|
|
96
|
if (scalar @ret > 1) { |
141
|
2
|
|
|
|
|
9
|
return undef, $ret[1]; |
142
|
|
|
|
|
|
|
} |
143
|
30
|
|
|
|
|
55
|
push @values, $ret[0]; |
144
|
30
|
|
|
|
|
523
|
$self->_defined(1); |
145
|
30
|
|
|
|
|
617
|
$self->value(\@values); |
146
|
|
|
|
|
|
|
} elsif ($self->is_hash) { |
147
|
31
|
|
|
|
|
801
|
my %values; |
148
|
31
|
100
|
|
|
|
489
|
if ($self->_defined) { |
149
|
18
|
|
|
|
|
154
|
%values = %{$self->value}; |
|
18
|
|
|
|
|
301
|
|
150
|
|
|
|
|
|
|
} |
151
|
31
|
100
|
|
|
|
331
|
my ($key, $val) = (ref($_[0]) eq 'ARRAY') ? @{$_[0]} : split(/=/, $_[0], 2); |
|
16
|
|
|
|
|
36
|
|
152
|
31
|
|
|
|
|
97
|
my @ret = $types->{$type}->{set_value}->($self, $val); |
153
|
31
|
100
|
|
|
|
82
|
if (scalar @ret > 1) { |
154
|
2
|
|
|
|
|
10
|
return undef, $ret[1]; |
155
|
|
|
|
|
|
|
} |
156
|
29
|
|
|
|
|
55
|
$values{$key} = $ret[0]; |
157
|
29
|
|
|
|
|
504
|
$self->_defined(1); |
158
|
29
|
|
|
|
|
590
|
$self->value(\%values); |
159
|
|
|
|
|
|
|
} elsif ($self->_defined) { |
160
|
2
|
|
|
|
|
138
|
printf STDERR "error: flag '%s' cannot be repeated, try --help\n", $self->name; |
161
|
2
|
|
|
|
|
194
|
return undef, 1; |
162
|
|
|
|
|
|
|
} else { |
163
|
278
|
|
|
|
|
18372
|
$self->_defined(1); |
164
|
278
|
|
|
|
|
2285
|
my @ret = $types->{$type}->{set_value}->($self, @_); |
165
|
278
|
100
|
|
|
|
810
|
if (scalar @ret > 1) { |
166
|
|
|
|
|
|
|
# has error |
167
|
13
|
|
|
|
|
102
|
return undef, $ret[1]; |
168
|
|
|
|
|
|
|
} |
169
|
265
|
|
|
|
|
4701
|
return $self->value($ret[0]); |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
1; |
174
|
|
|
|
|
|
|
__END__ |