blib/lib/Getopt/EX/Hashed.pm

filter: Statementstmt 88.6% 94 / 106 Branchbran 80.0% 24 / 30 Conditioncond 71.4% 5 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 83.3% 15 / 18 Podpod 85.7% 6 / 7 totaltotal 84.8% 146 / 172 CC 23 SCAR 15.6
CC
23
Cov
86%
_optspec
9.2
has
6.0
configure
6.0
CRAP 4.7
↑ Summary Build »
1 package Getopt::EX::Hashed;
2  
3 3 our $VERSION = '0.9902';
4  
5 =head1 NAME
6  
7 Getopt::EX::Hashed - Hash store object automation
8  
9 =head1 SYNOPSIS
10  
11 use App::foo;
12 App::foo->new->run();
13  
14 package App::foo;
15  
16 use Getopt::EX::Hashed;
17 has start => ( spec => "=i s begin", default => 1 );
18 has end => ( spec => "=i e" );
19 no Getopt::EX::Hashed;
20  
21 sub run {
22 my $app = shift;
23 use Getopt::Long;
24 $app->getopt or pod2usage();
25 if ($app->{start}) {
26 ...
27  
28 =head1 DESCRIPTION
29  
30 B<Getopt::EX::Hashed> is a module to automate a hash object to store
31 command line option values. Major objective of this module is to
32 integrate initialization and specification into single place. Module
33 name shares B<Getopt::EX>, but it works independently from other
34 modules included in B<Getopt::EX>, so far.
35  
36 In the current implementation, using B<Getopt::Long>, or compatible
37 module such as B<Getopt::EX::Long> is assumed. It is configurable,
38 but no other module is supported now.
39  
40 =head1 FUNCTION
41  
42 =head2 B<has>
43  
44 Declare option parameters in a form of:
45  
46 has option_name => ( param => value, ... );
47  
48 If array reference is given, multiple names can be declared at once.
49  
50 has [ 'left', 'right' ] => ( param => value, ... );
51  
52 If the name start with plus (C<+>), given parameters are added to
53 current value.
54  
55 has '+left' => ( default => 1 );
56  
57 Following parameters are available.
58  
59 =over 7
60  
61 =item B<spec> => I<string>
62  
63 Give option specification. Option spec and alias names are separated
64 by white space, and can show up in any order.
65  
66 Declaration
67  
68 has start => ( spec => "=i s begin" );
69  
70 will be compiled into string:
71  
72 start|s|begin:i
73  
74 which conform to C<Getopt::Long> definition. Of course, you can write
75 as this:
76  
77 has start => ( spec => "s|begin=i" );
78  
79 If the name and aliases contain underscore (C<_>), another alias name
80 is defined with dash (C<->) in place of underscores. So
81  
82 has a_to_z => ( spec => "=s" );
83  
84 will be compiled into:
85  
86 a_to_z|a-to-z:s
87  
88 If nothing special is necessary, give empty (or white space only)
89 string as a value. Otherwise, it is not considered as an option.
90  
91 =item B<alias> => I<string>
92  
93 Additional alias names can be specified by B<alias> parameter too.
94 There is no difference with ones in B<spec> parameter.
95  
96 =item B<default> => I<value>
97  
98 Set default value. If no default is given, the member is initialized
99 as C<undef>.
100  
101 If the value is code reference, hash object is passed by C<$_>.
102  
103 has [ qw(left right both) ] => spec => '=i';
104 has "+both" => default => sub {
105 $_->{left} = $_->{right} = $_[1];
106 } ;
107  
108 You can use this for C<< "<>" >> too, and spec parameter is not
109 required in this case.
110  
111 has ARGV => default => [];
112 has "<>" => default => sub {
113 push @{$_->{ARGV}}, $_[0];
114 };
115  
116 =back
117  
118 =head1 METHOD
119  
120 =over 7
121  
122 =item B<new>
123  
124 Class method to get initialized hash object.
125  
126 =item B<configure>
127  
128 There should be some configurable variables, but not fixed yet.
129  
130 =item B<getopt>
131  
132 Call C<GetOptions> function defined in caller's context with
133 appropriate parameters.
134  
135 $obj->getopt
136  
137 is just a shortcut for:
138  
139 GetOptions($obj, $obj->optspec)
140  
141 =item B<optspec>
142  
143 Return option specification list which can be given to C<GetOptions>
144 function with the hash object.
145  
146 =item B<use_keys>
147  
148 Because hash keys are protected by C<Hash::Util::lock_keys>, accessing
149 non-existing member causes an error. Use this function to declare new
150 member key before use.
151  
152 $obj->use_keys( qw(foo bar) );
153  
154 If you want to access arbitrary keys, unlock the object.
155  
156 use Hash::Util 'unlock_keys';
157 unlock_keys %{$obj};
158  
159 =back
160  
161 =head1 SEE ALSO
162  
163 L<Getopt::Long>
164  
165 L<Getopt::EX>, L<Getopt::EX::Long>
166  
167 =head1 AUTHOR
168  
169 Kazumasa Utashiro
170  
171 =head1 COPYRIGHT
172  
173 The following copyright notice applies to all the files provided in
174 this distribution, including binary files, unless explicitly noted
175 otherwise.
176  
177 Copyright 2021 Kazumasa Utashiro
178  
179 =head1 LICENSE
180  
181 This library is free software; you can redistribute it and/or modify
182 it under the same terms as Perl itself.
183  
184 =cut
185  
186 3 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
187 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
188 3 ▶use Hash::Util qw(lock_keys lock_keys_plus unlock_keys);
Subroutine BEGIN: called CC=1 SCAR=0.0
189 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
190 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
191  
192 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
193 3 our @EXPORT = qw(has new);
194  
195 3 my @Member;
196 my %Member;
197  
198 3 my %Config = (
199 LOCK_KEYS => 1,
200 REPLACE_UNDERSCORE => 1,
201 GETOPT => 'GetOptions',
202 );
203 3 lock_keys %Config;
204  
205 sub configure {
206 0 ▶ my $obj = shift;
Subroutine configure: not called CC=2 SCAR=17.9
Pod documented
207 0 ▶ while (my($key, $value) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 0 0 0
208 0 $Config{$key} = $value;
209 }
210 0 return $obj;
211 }
212  
213 sub unimport {
214 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
215 2 ▶ my $caller = caller;
Subroutine unimport: called CC=1 SCAR=0.0
216 2 delete ${"$caller\::"}{has};
217 }
218  
219 sub reset {
220 0 ▶ @Member = %Member = ();
Subroutine reset: not called CC=1 SCAR=6.9
Pod undocumented
221 0 return $_[0];
222 }
223  
224 sub has {
225 29 ▶ my($name, %param) = @_;
Subroutine has: called CC=6 SCAR=17.9
Pod documented
226 29 ▶ my @name = ref $name eq 'ARRAY' ? @$name : $name;
Branch: 1 branch
BranchTrueFalseTotal
ref $name eq 'ARRAY' ? : 2 27 29
227 29 for (@name) {
228 33 my $append = s/^\+//;
229 33 ▶ if ($append) {
Branch: 1 branch
BranchTrueFalseTotal
if ($append) { } 3 30 33
230 3 ▶ $Member{$_} or die "$_: Not defined\n";
Branch: 1 branch
BranchTrueFalseTotal
$Member{$_} or die "${_}: Not defined\n" 1 2 3
231 2 $Member{$_} = { %{$Member{$_}}, %param };
232 } else {
233 30 ▶ $Member{$_} and die "$_: Duplicated\n";
Branch: 1 branch
BranchTrueFalseTotal
$Member{$_} and die "${_}: Duplicated\n" 0 30 30
234 30 push @Member, $_;
235 30 $Member{$_} = \%param;
236 }
237 }
238 }
239  
240 sub new {
241 1 ▶ my $class = shift;
Subroutine new: called CC=5 SCAR=16.1
Pod documented
242 1 my $obj = bless {}, __PACKAGE__;
243 1 my $member = $obj->{__Hash__} = { %Member };
244 1 my $order = $obj->{__Order__} = [ @Member ];
245 1 our @ISA = $class;
246 1 for my $key (@{$order}) {
247 16 my $m = $member->{$key};
248 16 $obj->{$key} = do {
249 16 ▶ if (ref($m->{default}//'') eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($m->{'default'} // '') eq 'CODE') { } 2 14 16
Condition100%2/2
$m->{"default"} // ""
l!l
412
MC/DC100%1/1
$m->{"default"} // ""
$m->{"default"}
Truth table100%2/2
A
A= $m->{"default"}
Aresult
11
00
250 sub {
251 5 ▶ &{$m->{default}} for $obj;
Subroutine __ANON__: called CC=2 SCAR=6.9
252 2 };
253 } else {
254 14 $m->{default};
255 }
256 };
257 }
258 1 ▶ lock_keys %{$obj} if $Config{LOCK_KEYS};
Branch: 1 branch
BranchTrueFalseTotal
if $Config{"LOCK_KEYS"} 1 0 1
259 1 @Member = %Member = ();
260 1 $obj;
261 }
262  
263 sub optspec {
264 1 ▶ my $obj = shift;
Subroutine optspec: called CC=4 SCAR=13.9
Pod documented
265 1 my $member = $obj->{__Hash__};
266 1 do {
267 15 map { _optspec($obj, @$_) }
268 map {
269 15 ▶ if (my $alias = $member->{$_->[0]}->{alias}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $alias = $$member{$_->[0]}{"alias"}) 1 14 15
270 1 $_->[1] .= " $alias";
271 }
272 15 $_;
273 }
274 grep {
275 16 ▶ $_->[0] eq '<>' and $_->[1] //= '';
Branch: 1 branch
BranchTrueFalseTotal
$_->[0] eq "<>" and $_->[1] //= "" 1 15 16
Condition50%1/2
$_->[1] //= ""
l!l
01
MC/DC0%0/1
$_->[1] //= ""
$_->[1]
Truth table50%1/2
A
A= $_->[1]
Aresult
11
00
276 16 defined $_->[1];
277 }
278 16 map { [ $_ => $member->{$_}->{spec} ] }
279 1 @{$obj->{__Order__}};
280 };
281 }
282  
283 3 my $spec_re = qr/[!+=:]/;
284  
285 sub _optspec {
286 15 ▶ my $obj = shift;
Subroutine _optspec: called CC=9 SCAR=22.1
287 15 my($name, $args) = @_;
288  
289 15 ▶ return $name if $name eq '<>';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "<>" 1 14 15
290  
291 14 my @args = split ' ', $args;
292 14 my @spec = grep /$spec_re/, @args;
293 14 my $spec = do {
294 14 ▶ if (@spec == 0) { '' }
Branch: 2 branches
BranchTrueFalseTotal
if (@spec == 0) { } 2 12 14
elsif (@spec == 1) { } 12 0 12
295 12 elsif (@spec == 1) { $spec[0] }
296 0 else { die }
297 };
298 14 my @alias = grep !/$spec_re/, @args;
299 14 my @names = ($name, @alias);
300 14 ▶ if ($Config{REPLACE_UNDERSCORE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Config{"REPLACE_UNDERSCORE"}) 14 0 14
301 14 for ($name, @alias) {
302 19 ▶ push @names, tr[_][-]r if /_/;
Branch: 1 branch
BranchTrueFalseTotal
if /_/u 1 18 19
303 }
304 }
305 14 ▶ push @names, '' if @names and $spec !~ /^($spec_re|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if @names and not $spec =~ /^($spec_re|$)/u 1 13 14
Condition66%2/3
@names and not $spec =~ /^($spec_re|$)/u
!ll&&!rl&&r
0131
MC/DC50%1/2
@names and not $spec =~ /^($spec_re|$)/u
@names not $spec =~ /^($spec_re|$)/u
Truth table66%2/3
A and not(B)
A= @namesB= not $spec =~ /^($spec_re|$)/u
ABresult
0X0
100
111
306 14 join('|', @names) . $spec;
307 }
308  
309 sub getopt {
310 1 ▶ my $obj = shift;
Subroutine getopt: called CC=1 SCAR=0.0
Pod documented
311 1 my $getopt = caller . "::" . $Config{GETOPT};
312 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
313 1 &{$getopt}($obj, $obj->optspec);
314 }
315  
316 sub use_keys {
317 0 ▶ my $obj = shift;
Subroutine use_keys: not called CC=1 SCAR=6.9
Pod documented
318 0 unlock_keys %{$obj};
319 0 lock_keys_plus %{$obj}, @_;
320 }
321  
322 3 1;
↑ Summary Build »