line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Getopt::Param; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
82955
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
65
|
|
4
|
2
|
|
|
2
|
|
13
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
68
|
|
5
|
2
|
|
|
2
|
|
12
|
use Carp; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
195
|
|
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
1990
|
use version; our $VERSION = qv('0.0.5'); |
|
2
|
|
|
|
|
4413
|
|
|
2
|
|
|
|
|
12
|
|
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
2180
|
use Locale::Maketext::Pseudo; |
|
2
|
|
|
|
|
1733
|
|
|
2
|
|
|
|
|
106
|
|
10
|
2
|
|
|
2
|
|
2641
|
use Class::Std; |
|
2
|
|
|
|
|
32353
|
|
|
2
|
|
|
|
|
16
|
|
11
|
2
|
|
|
2
|
|
2750
|
use Class::Std::Utils; |
|
2
|
|
|
|
|
2110
|
|
|
2
|
|
|
|
|
13
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
{ |
14
|
|
|
|
|
|
|
my %lang; |
15
|
|
|
|
|
|
|
my %opts; |
16
|
|
|
|
|
|
|
my %quiet; |
17
|
|
|
|
|
|
|
my %args_ar; |
18
|
|
|
|
|
|
|
my %help_cr; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub BUILD { |
21
|
12
|
|
|
12
|
1
|
5832
|
my ($prm, $ident, $arg_ref) = @_; |
22
|
|
|
|
|
|
|
|
23
|
12
|
50
|
33
|
|
|
88
|
$lang{ $ident } = ref $arg_ref->{'lang_obj'} && $arg_ref->{'lang_obj'}->can('maketext') |
24
|
|
|
|
|
|
|
? $arg_ref->{'lang_obj'} : Locale::Maketext::Pseudo->new(); |
25
|
|
|
|
|
|
|
|
26
|
12
|
|
|
|
|
193
|
$opts{ $ident } = {}; |
27
|
12
|
|
100
|
|
|
64
|
$quiet{ $ident } = $arg_ref->{'quiet'} || 0; |
28
|
12
|
100
|
|
|
|
42
|
my $args_ar = ref $arg_ref->{'array_ref'} ne 'ARRAY' ? \@ARGV : $arg_ref->{'array_ref'}; |
29
|
12
|
|
|
|
|
17
|
my @nodestruct = @{ $args_ar }; |
|
12
|
|
|
|
|
34
|
|
30
|
12
|
|
|
|
|
25
|
$args_ar{ $ident } = \@nodestruct; |
31
|
|
|
|
|
|
|
|
32
|
12
|
|
|
|
|
19
|
my $idx = 0; |
33
|
12
|
|
|
|
|
29
|
ARG_ITEM: |
34
|
12
|
|
|
|
|
17
|
for my $arg ( @{ $args_ar{ $ident } } ) { |
35
|
26
|
100
|
|
|
|
209
|
last ARG_ITEM if $arg eq '--'; |
36
|
|
|
|
|
|
|
|
37
|
25
|
50
|
|
|
|
134
|
my $rg = $arg_ref->{'strict'} ? qr{^--([^-])} : qr{^--(.)}; |
38
|
|
|
|
|
|
|
|
39
|
25
|
100
|
|
|
|
209
|
if( $arg =~ s/$rg/$1/ ) { |
40
|
23
|
|
|
|
|
66
|
my($flag, $value) = split /=/, $arg, 2; |
41
|
23
|
100
|
|
|
|
31
|
push @{ $opts{ $ident }->{ $flag } }, defined $value ? $value : '--' . $flag; |
|
23
|
|
|
|
|
126
|
|
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
else { |
44
|
2
|
50
|
|
|
|
8
|
carp $lang{ $ident }->maketext('Argument [_1] did not match [_2]', $idx, $rg) if !$quiet{ $ident }; |
45
|
|
|
|
|
|
|
} |
46
|
25
|
|
|
|
|
75
|
$idx++; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
12
|
50
|
33
|
|
|
50
|
if ( $opts{ $ident }->{'help'} && $arg_ref->{'help_coderef'} ) { |
50
|
0
|
|
|
|
|
0
|
$arg_ref->{'help_coderef'}->($prm); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
12
|
|
100
|
0
|
|
64
|
$help_cr{$ident} = $arg_ref->{'help_coderef'} || sub { croak $lang{ $ident }->maketext(q{No '[_1]' function defined}, 'help') }; |
|
0
|
|
|
|
|
0
|
|
54
|
|
|
|
|
|
|
|
55
|
12
|
100
|
66
|
|
|
18
|
if ( !keys %{$opts{$ident}} && $arg_ref->{'no_args_help'} ) { |
|
12
|
|
|
|
|
54
|
|
56
|
1
|
|
|
|
|
5
|
$help_cr{$ident}->($prm); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
12
|
100
|
|
|
|
58
|
if( ref $arg_ref->{'known_only'} eq 'ARRAY') { |
60
|
2
|
|
|
|
|
3
|
my %lookup; |
61
|
2
|
|
|
|
|
4
|
@lookup{ @{$arg_ref->{'known_only'}} } = (); |
|
2
|
|
|
|
|
6
|
|
62
|
|
|
|
|
|
|
|
63
|
2
|
|
|
|
|
4
|
my $unknown = 0; |
64
|
2
|
|
|
|
|
4
|
for my $k (keys %{$opts{$ident}}) { |
|
2
|
|
|
|
|
5
|
|
65
|
2
|
100
|
|
|
|
9
|
if (!exists $lookup{$k}) { |
66
|
1
|
|
|
|
|
1
|
$unknown++; |
67
|
|
|
|
|
|
|
# $k =~ s{\W}{?}g; # or quotemeta() |
68
|
1
|
|
|
|
|
7
|
carp $lang{ $ident }->maketext(q{Unknown argument '[_1]'}, quotemeta($k)); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
2
|
100
|
|
|
|
508
|
$help_cr{$ident}->($prm) if $unknown; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
12
|
100
|
|
|
|
41
|
if( ref $arg_ref->{'required'} eq 'ARRAY') { |
75
|
|
|
|
|
|
|
|
76
|
2
|
|
|
|
|
3
|
my $missing = 0; |
77
|
2
|
|
|
|
|
3
|
for my $k (@{$arg_ref->{'required'}}) { |
|
2
|
|
|
|
|
5
|
|
78
|
2
|
100
|
|
|
|
8
|
if (!exists $opts{$ident}->{$k}) { |
79
|
1
|
|
|
|
|
2
|
$missing++; |
80
|
|
|
|
|
|
|
# $k =~ s{\W}{?}g; # or quotemeta() |
81
|
1
|
|
|
|
|
17
|
carp $lang{ $ident }->maketext(q{Missing argument '[_1]'}, quotemeta($k)); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
} |
84
|
2
|
100
|
|
|
|
323
|
$help_cr{$ident}->($prm) if $missing; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
12
|
100
|
|
|
|
42
|
if( ref $arg_ref->{'validate'} eq 'CODE') { |
88
|
2
|
100
|
|
|
|
9
|
$arg_ref->{'validate'}->($prm) || $help_cr{$ident}->($prm); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
12
|
100
|
|
|
|
68
|
if ( ref $arg_ref->{'actions'} eq 'ARRAY' ) { |
92
|
2
|
|
|
|
|
6
|
for my $k ($arg_ref->{'actions'}) { |
93
|
2
|
50
|
|
|
|
9
|
if (exists $opts{$ident}->{$k->[0]}) { |
94
|
2
|
100
|
|
|
|
6
|
if (ref $k->[1] eq 'CODE') { |
95
|
1
|
|
|
|
|
4
|
$k->[1]->($prm); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
else { |
98
|
1
|
|
|
|
|
6
|
$help_cr{$ident}->($prm); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub help { |
106
|
1
|
|
|
1
|
1
|
482
|
my ($prm) = @_; |
107
|
1
|
|
|
|
|
17
|
$help_cr{ ident $prm }->(); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub get_param { |
111
|
27
|
|
|
27
|
1
|
427
|
my ($prm, $name) = @_; |
112
|
27
|
100
|
|
|
|
164
|
return if !exists $opts{ ident $prm }->{ $name }; # do not auto vivify it |
113
|
25
|
50
|
|
|
|
98
|
$opts{ ident $prm }->{ $name } = [] if ref $opts{ ident $prm }->{ $name } ne 'ARRAY'; |
114
|
25
|
100
|
|
|
|
125
|
return wantarray ? @{ $opts{ ident $prm }->{ $name } } |
|
10
|
|
|
|
|
76
|
|
115
|
|
|
|
|
|
|
: $opts{ ident $prm }->{ $name }->[0]; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub set_param { |
119
|
5
|
|
|
5
|
1
|
467
|
my ($prm, $name, @val) = @_; |
120
|
5
|
|
|
|
|
34
|
$opts{ ident $prm }->{ $name } = [ @val ]; |
121
|
|
|
|
|
|
|
# = ref $val->[0] eq 'ARRAY' && @val == 1 ? [ @{ $val->[0] } ] : [@val]; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub list_params { |
125
|
2
|
|
|
2
|
1
|
5
|
my ($prm) = @_; |
126
|
2
|
|
|
|
|
28
|
return wantarray ? keys %{ $opts{ ident $prm } } |
|
0
|
|
|
|
|
0
|
|
127
|
2
|
50
|
|
|
|
7
|
: [ keys %{ $opts{ ident $prm } } ] |
128
|
|
|
|
|
|
|
; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub append_param { |
132
|
2
|
|
|
2
|
1
|
1027
|
my ($prm, $name, @val) = @_; |
133
|
2
|
50
|
|
|
|
18
|
$opts{ ident $prm }->{ $name } = [] if ref $opts{ ident $prm }->{ $name } ne 'ARRAY'; |
134
|
2
|
|
|
|
|
5
|
$opts{ ident $prm }->{ $name } = [ @{ $opts{ ident $prm }->{ $name } }, @val ]; |
|
2
|
|
|
|
|
15
|
|
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub prepend_param { |
138
|
2
|
|
|
2
|
1
|
875
|
my ($prm, $name, @val) = @_; |
139
|
2
|
50
|
|
|
|
24
|
$opts{ ident $prm }->{ $name } = [] if ref $opts{ ident $prm }->{ $name } ne 'ARRAY'; |
140
|
2
|
|
|
|
|
5
|
$opts{ ident $prm }->{ $name } = [ @val, @{ $opts{ ident $prm }->{ $name } } ]; |
|
2
|
|
|
|
|
18
|
|
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub param { |
144
|
26
|
|
|
26
|
1
|
6717
|
my ($prm, $name, @val) = @_; |
145
|
26
|
100
|
|
|
|
75
|
return $prm->list_params() if !$name; |
146
|
24
|
100
|
|
|
|
58
|
$prm->set_param( $name, @val ) if @val; |
147
|
24
|
|
|
|
|
55
|
return $prm->get_param( $name ); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub delete_param { |
151
|
2
|
|
|
2
|
1
|
4
|
my ($prm, $name) = @_; |
152
|
2
|
|
|
|
|
25
|
delete $opts{ ident $prm }->{ $name }; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub exists_param { |
156
|
6
|
|
|
6
|
1
|
1070
|
my ($prm, $name) = @_; |
157
|
6
|
100
|
|
|
|
67
|
return 1 if exists $opts{ ident $prm }->{ $name }; |
158
|
3
|
|
|
|
|
26
|
return; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub get_param_hashref { |
162
|
3
|
|
|
3
|
1
|
433
|
my ($prm) = @_; |
163
|
3
|
|
|
|
|
4
|
my %new_hash = %{ $opts{ ident $prm } }; |
|
3
|
|
|
|
|
21
|
|
164
|
3
|
|
|
|
|
9
|
return \%new_hash; # deref first so the internal one does not risk ferdidling |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
1; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
__END__ |