line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Getopt::Kingpin::Base; |
2
|
31
|
|
|
31
|
|
487
|
use 5.008001; |
|
31
|
|
|
|
|
106
|
|
3
|
31
|
|
|
31
|
|
172
|
use strict; |
|
31
|
|
|
|
|
60
|
|
|
31
|
|
|
|
|
626
|
|
4
|
31
|
|
|
31
|
|
140
|
use warnings; |
|
31
|
|
|
|
|
54
|
|
|
31
|
|
|
|
|
1052
|
|
5
|
31
|
|
|
31
|
|
182
|
use Object::Simple -base; |
|
31
|
|
|
|
|
87
|
|
|
31
|
|
|
|
|
201
|
|
6
|
31
|
|
|
31
|
|
3548
|
use Carp; |
|
31
|
|
|
|
|
60
|
|
|
31
|
|
|
|
|
1929
|
|
7
|
31
|
|
|
31
|
|
24361
|
use Path::Tiny; |
|
31
|
|
|
|
|
376292
|
|
|
31
|
|
|
|
|
16488
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = "0.10"; |
10
|
|
|
|
|
|
|
our $types; |
11
|
|
|
|
|
|
|
sub AUTOLOAD { |
12
|
511
|
|
|
511
|
|
2902
|
my $self = shift; |
13
|
511
|
|
|
|
|
789
|
my $func = our $AUTOLOAD; |
14
|
511
|
|
|
|
|
1957
|
$func =~ s/.*:://; |
15
|
511
|
|
|
|
|
1321
|
my $type = _camelize($func); |
16
|
|
|
|
|
|
|
|
17
|
511
|
|
|
|
|
1503
|
$self->_set_types($type); |
18
|
|
|
|
|
|
|
|
19
|
510
|
|
|
|
|
9309
|
$self->type($type); |
20
|
|
|
|
|
|
|
|
21
|
510
|
|
|
|
|
4134
|
return $self; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub DESTROY { |
25
|
351
|
|
|
351
|
|
205413
|
return 1; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub _set_types { |
29
|
855
|
|
|
855
|
|
1239
|
my $self = shift; |
30
|
855
|
|
|
|
|
1508
|
my ($type) = @_; |
31
|
|
|
|
|
|
|
|
32
|
855
|
100
|
|
|
|
2188
|
if (not exists $types->{$type}) { |
33
|
82
|
|
|
|
|
377
|
my $module = sprintf "Getopt::Kingpin::Type::%s", $type; |
34
|
82
|
|
|
|
|
313
|
$module =~ s/(?:List|Hash)$//; |
35
|
82
|
100
|
|
|
|
725
|
if (not $module->can('set_value')) { |
36
|
71
|
100
|
|
|
|
4811
|
croak "type error '$type'" unless eval "require $module"; ## no critic |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
$types->{$type} = { |
39
|
80
|
|
|
|
|
560
|
type => \&{"${module}::type"}, |
40
|
80
|
|
|
|
|
278
|
set_value => \&{"${module}::set_value"}, |
|
80
|
|
|
|
|
469
|
|
41
|
|
|
|
|
|
|
}; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
853
|
100
|
|
|
|
1982
|
if ($type eq "Bool") { |
45
|
424
|
100
|
|
|
|
7069
|
if (not defined $self->_default) { |
46
|
247
|
|
|
|
|
6306
|
$self->_default(0); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
853
|
100
|
|
|
|
4902
|
if ($type =~ /List$/) { |
|
|
100
|
|
|
|
|
|
51
|
62
|
|
|
|
|
1108
|
$self->is_cumulative(1); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
elsif ($type =~ /Hash$/) { |
55
|
55
|
|
|
|
|
960
|
$self->is_hash(1); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub _camelize { |
60
|
511
|
|
|
511
|
|
800
|
my $c = shift; |
61
|
511
|
|
|
|
|
1847
|
$c =~ s/(^|_)(.)/uc($2)/ge; |
|
584
|
|
|
|
|
2450
|
|
62
|
511
|
|
|
|
|
1242
|
return $c; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
use overload ( |
66
|
457
|
100
|
|
457
|
|
72697
|
'""' => sub {defined $_[0]->value ? $_[0]->value : ""}, |
67
|
31
|
|
|
|
|
215
|
fallback => 1, |
68
|
31
|
|
|
31
|
|
337
|
); |
|
31
|
|
|
|
|
79
|
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
has name => undef; |
71
|
|
|
|
|
|
|
has short_name => undef; |
72
|
|
|
|
|
|
|
has description => undef; |
73
|
|
|
|
|
|
|
has value => undef; |
74
|
|
|
|
|
|
|
has _defined => 0; |
75
|
|
|
|
|
|
|
has is_cumulative => 0; |
76
|
|
|
|
|
|
|
has is_hash => 0; |
77
|
|
|
|
|
|
|
has _default => undef; |
78
|
|
|
|
|
|
|
has _envar => undef; |
79
|
|
|
|
|
|
|
has type => "String"; |
80
|
|
|
|
|
|
|
has _required => 0; |
81
|
|
|
|
|
|
|
has index => 0; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub short { |
84
|
31
|
|
|
31
|
1
|
87
|
my $self = shift; |
85
|
31
|
|
|
|
|
66
|
my ($short_name) = @_; |
86
|
|
|
|
|
|
|
|
87
|
31
|
|
|
|
|
512
|
$self->short_name($short_name); |
88
|
|
|
|
|
|
|
|
89
|
31
|
|
|
|
|
308
|
return $self; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub default { |
93
|
43
|
|
|
43
|
1
|
169
|
my $self = shift; |
94
|
43
|
|
|
|
|
93
|
my ($default) = @_; |
95
|
|
|
|
|
|
|
|
96
|
43
|
|
|
|
|
735
|
$self->_default($default); |
97
|
|
|
|
|
|
|
|
98
|
43
|
|
|
|
|
615
|
return $self; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub override_default_from_envar { |
102
|
6
|
|
|
6
|
1
|
13
|
my $self = shift; |
103
|
6
|
|
|
|
|
14
|
my ($envar_name) = @_; |
104
|
|
|
|
|
|
|
|
105
|
6
|
100
|
|
|
|
19
|
if (exists $ENV{$envar_name}) { |
106
|
5
|
|
|
|
|
88
|
$self->_envar($ENV{$envar_name}); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
6
|
|
|
|
|
78
|
return $self; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub required { |
113
|
52
|
|
|
52
|
1
|
90
|
my $self = shift; |
114
|
52
|
|
|
|
|
852
|
$self->_required(1); |
115
|
|
|
|
|
|
|
|
116
|
52
|
|
|
|
|
579
|
return $self; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub set_value { |
120
|
344
|
|
|
344
|
1
|
631
|
my $self = shift; |
121
|
344
|
|
|
|
|
5473
|
my $type = $self->type; |
122
|
344
|
|
|
|
|
2633
|
$self->_set_types($type); |
123
|
343
|
100
|
|
|
|
5897
|
if ($self->is_cumulative) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
124
|
32
|
|
|
|
|
201
|
my @values; |
125
|
32
|
100
|
|
|
|
510
|
if ($self->_defined) { |
126
|
17
|
|
|
|
|
115
|
@values = @{$self->value}; |
|
17
|
|
|
|
|
344
|
|
127
|
|
|
|
|
|
|
} |
128
|
32
|
|
|
|
|
346
|
my @ret = $types->{$type}->{set_value}->($self, @_); |
129
|
32
|
100
|
|
|
|
84
|
if (scalar @ret > 1) { |
130
|
2
|
|
|
|
|
10
|
return undef, $ret[1]; |
131
|
|
|
|
|
|
|
} |
132
|
30
|
|
|
|
|
59
|
push @values, $ret[0]; |
133
|
30
|
|
|
|
|
538
|
$self->_defined(1); |
134
|
30
|
|
|
|
|
667
|
$self->value(\@values); |
135
|
|
|
|
|
|
|
} elsif ($self->is_hash) { |
136
|
31
|
|
|
|
|
771
|
my %values; |
137
|
31
|
100
|
|
|
|
494
|
if ($self->_defined) { |
138
|
18
|
|
|
|
|
113
|
%values = %{$self->value}; |
|
18
|
|
|
|
|
279
|
|
139
|
|
|
|
|
|
|
} |
140
|
31
|
100
|
|
|
|
330
|
my ($key, $val) = (ref($_[0]) eq 'ARRAY') ? @{$_[0]} : split(/=/, $_[0], 2); |
|
16
|
|
|
|
|
36
|
|
141
|
31
|
|
|
|
|
103
|
my @ret = $types->{$type}->{set_value}->($self, $val); |
142
|
31
|
100
|
|
|
|
84
|
if (scalar @ret > 1) { |
143
|
2
|
|
|
|
|
10
|
return undef, $ret[1]; |
144
|
|
|
|
|
|
|
} |
145
|
29
|
|
|
|
|
74
|
$values{$key} = $ret[0]; |
146
|
29
|
|
|
|
|
521
|
$self->_defined(1); |
147
|
29
|
|
|
|
|
652
|
$self->value(\%values); |
148
|
|
|
|
|
|
|
} elsif ($self->_defined) { |
149
|
2
|
|
|
|
|
126
|
printf STDERR "error: flag '%s' cannot be repeated, try --help\n", $self->name; |
150
|
2
|
|
|
|
|
146
|
return undef, 1; |
151
|
|
|
|
|
|
|
} else { |
152
|
278
|
|
|
|
|
17617
|
$self->_defined(1); |
153
|
278
|
|
|
|
|
2165
|
my @ret = $types->{$type}->{set_value}->($self, @_); |
154
|
278
|
100
|
|
|
|
716
|
if (scalar @ret > 1) { |
155
|
|
|
|
|
|
|
# has error |
156
|
13
|
|
|
|
|
63
|
return undef, $ret[1]; |
157
|
|
|
|
|
|
|
} |
158
|
265
|
|
|
|
|
4445
|
return $self->value($ret[0]); |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
1; |
163
|
|
|
|
|
|
|
__END__ |