blib/lib/Getopt/EX/Config.pm

filter: Statementstmt 73.3% 80 / 109 Branchbran 50.0% 17 / 34 Conditioncond 50.0% 11 / 22 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 70.8% 17 / 24 Podpod 45.4% 5 / 11 totaltotal 62.7% 133 / 212 CC 30 SCAR 21.7
CC
30
Cov
65%
set
42.0
getopt
9.0
split_argv
7.0
CRAP 8.8
↑ Summary
1 package Getopt::EX::Config;
2  
3 4 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = '1.03';
7  
8 4 our $REPLACE_UNDERSCORE = 1;
9  
10 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Hash::Util qw(lock_keys);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 our %CONFIG;
17  
18 5 ▶sub import {
Subroutine import: called CC=3 SCAR=11.1
19 5 my $class = shift;
20 5 my $caller = caller;
21 5 ▶ my @names = @_ ? @_ : 'config';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 5 5
22 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 for my $name (@names) {
24 5 ▶ *{"$caller\::$name"} = sub {
Subroutine __ANON__: not called CC=1 SCAR=1.2
25 0 $CONFIG{$caller}->config(@_);
26 5 };
27 };
28 }
29  
30 12 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
Pod documented
31 12 my $class = shift;
32 12 ▶ my $config = ref $_[0] eq 'HASH' ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 12 12
33 12 for my $key (keys %$config) {
34 17 ▶ $key =~ /^[a-zA-Z]/ or die "$key: config key must start with a letter\n";
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /^[a-zA-Z]/u or die "${key}: config key must start with a letter\n" 17 0 17
35 }
36 # Initialize internal keys for lock_keys compatibility
37 12 $config->{_argv} = [];
38 12 $config->{_configure} = [];
39 12 my $caller = caller;
40 12 $CONFIG{$caller} = bless $config, $class;
41 12 $config;
42 }
43  
44 0 ▶sub object {
Subroutine object: not called CC=1 SCAR=6.9
Pod undocumented
45 0 $CONFIG{+caller};
46 }
47  
48 15 ▶sub deal_with {
Subroutine deal_with: called CC=4 SCAR=15.9
Pod documented
49 15 my $obj = shift;
50 15 ▶ if ($obj eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj eq __PACKAGE__) 0 15 15
51 0 ▶ $obj = $CONFIG{+caller} // die;
Condition0%0/2
$Getopt::EX::Config::CONFIG{caller()} // die()
l!l
00
MC/DC0%0/1
$Getopt::EX::Config::CONFIG{caller()} // die()
$Getopt::EX::Config::CONFIG{caller()}
Truth table0%0/2
A
A= $Getopt::EX::Config::CONFIG{caller()}
Aresult
11
00
52 }
53 15 my($my_argv, $argv) = split_argv(shift);
54 15 ▶ $obj->getopt($my_argv, @_) if @$my_argv;
Branch: 1 branch
BranchTrueFalseTotal
if @$my_argv 15 0 15
55 14 $obj->{_argv} = [ @$my_argv ];
56 14 return $obj;
57 }
58  
59 9 ▶sub argv {
Subroutine argv: called CC=2 SCAR=7.1
Pod documented
60 9 my $obj = shift;
61 9 ▶ @{ $obj->{_argv} // [] };
Condition50%1/2
$obj->{"_argv"} // []
l!l
90
MC/DC0%0/1
$obj->{"_argv"} // []
$obj->{"_argv"}
Truth table50%1/2
A
A= $obj->{"_argv"}
Aresult
11
00
62 }
63  
64 6 ▶sub configure {
Subroutine configure: called CC=1 SCAR=0.0
Pod documented
65 6 my $obj = shift;
66 6 push @{$obj->{_configure}}, @_;
67 6 $obj;
68 }
69  
70 4 ▶use Getopt::EX::Func;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 4 *arg2kvlist = \&Getopt::EX::Func::arg2kvlist;
72  
73 15 ▶sub getopt {
Subroutine getopt: called CC=8 SCAR=22.0
Pod undocumented
74 15 my $obj = shift;
75 15 ▶ my $argv = shift // [];
Condition50%1/2
shift() // []
l!l
150
MC/DC0%0/1
shift() // []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
76 15 ▶ return if @{ $argv } == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @{$argv;} == 0 0 15 15
77  
78 # Filter out internal keys (must start with a letter)
79 15 ▶ @_ = grep { ref($_) || /^[a-zA-Z]/ } @_;
Condition33%1/3
ref $_ || /^[a-zA-Z]/u
l!l&&r!l&&!r
0200
MC/DC0%0/2
ref $_ || /^[a-zA-Z]/u
ref $_ /^[a-zA-Z]/u
Truth table33%1/3
A || B
A= ref $_B= /^[a-zA-Z]/u
ABresult
1X1
011
000
80  
81 # Convert underscore options to underscore|dash format
82 15 ▶ @_ = map { ref($_) ? $_ : s/^(\w*_[\w_]*)/"$1|" . ($1 =~ s:_:-:gr)/er } @_
Branch: 2 branches
BranchTrueFalseTotal
if $REPLACE_UNDERSCORE 13 2 15
ref $_ ? : 0 16 16
83 if $REPLACE_UNDERSCORE;
84  
85 my $p = Getopt::Long::Parser->new(
86 15 ▶ config => [ 'bundling', @{$obj->{_configure} // []} ]
Condition50%1/2
$obj->{"_configure"} // []
l!l
150
MC/DC0%0/1
$obj->{"_configure"} // []
$obj->{"_configure"}
Truth table50%1/2
A
A= $obj->{"_configure"}
Aresult
11
00
87 );
88 $p->getoptionsfromarray(
89 $argv,
90 $obj,
91 0 ▶ "config=s" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
92 0 $obj->config(arg2kvlist($_[1]));
93 },
94 @_
95 15 ▶ ) or die "Option parse error.\n";
Branch: 1 branch
BranchTrueFalseTotal
$p->getoptionsfromarray($argv, $obj, "config=s", sub { $obj->config(arg2kvlist($_[1])); } , @_) or die "Option parse error.\n" 14 1 15
96 }
97  
98 0 ▶sub config {
Subroutine config: not called CC=2 SCAR=17.9
Pod documented
99 0 my $obj = shift;
100 0 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
101 0 $obj->get(@_);
102 } else {
103 0 $obj->set(@_);
104 }
105 }
106  
107 ######################################################################
108  
109 0 ▶sub set {
Subroutine set: not called CC=6 SCAR=37.4
Pod undocumented
110 0 my $obj = shift;
111 0 ▶ while (my($k, $v) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 0 0 0
112 0 my $c = $obj;
113 0 my @names = split /\./, $k;
114 0 my $name = pop @names;
115 0 for (@names) {
116 0 ▶ $c = $c->{$_} // die "$k: invalid name.\n";
Condition0%0/2
$c->{$_} // die("${k}: invalid name.\n")
l!l
00
MC/DC0%0/1
$c->{$_} // die("${k}: invalid name.\n")
$c->{$_}
Truth table0%0/2
A
A= $c->{$_}
Aresult
11
00
117 }
118 0 ▶ exists $c->{$name} or die "$k: invalid name.\n";
Branch: 1 branch
BranchTrueFalseTotal
exists $c->{$name} or die "${k}: invalid name.\n" 0 0 0
119 0 ▶ if (ref $c->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c->{$name}) { } 0 0 0
120 0 ${$c->{$name}} = $v;
121 } else {
122 0 $c->{$name} = $v;
123 }
124 }
125 0 ();
126 }
127  
128 0 ▶sub get :lvalue {
Subroutine get: not called CC=2 SCAR=17.9
Pod undocumented
129 0 my $c = shift;
130 0 my $key = shift;
131 0 ▶ if (ref $c->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c->{$key}) { } 0 0 0
132 0 ${$c->{$key}};
133 } else {
134 0 $c->{$key};
135 }
136 }
137  
138 0 ▶sub mod_argv {
Subroutine mod_argv: not called CC=1 SCAR=6.9
Pod undocumented
139 0 my($mod, $argv) = splice @_, 0, 2;
140 0 ($mod, split_argv($argv), @_);
141 }
142  
143 23 ▶sub split_argv {
Subroutine split_argv: called CC=7 SCAR=19.5
Pod undocumented
144 23 my $argv = shift;
145 23 my @my_argv;
146 23 ▶ if (@$argv and $argv->[0] =~ /\A-{3,}\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$argv and $argv->[0] =~ /\A-{3,}\z/u) { } 9 14 23
elsif (@$argv and not $argv->[0] =~ /^-M/u and defined(my $i = &first(sub { $argv->[$_] eq '--'; } , keys @$argv))) { } 12 3 15
Condition66%2/3
@$argv and $argv->[0] =~ /\A-{3,}\z/u
!ll&&!rl&&r
0149
MC/DC50%1/2
@$argv and $argv->[0] =~ /\A-{3,}\z/u
@$argv $argv->[0] =~ /\A-{3,}\z/u
Truth table66%2/3
A and B
A= @$argvB= $argv->[0] =~ /\A-{3,}\z/u
ABresult
0X0
100
111
147 # leading terminator: ---, ----, ...
148 9 my $term = shift @$argv;
149 9 ▶ defined(my $i = first { $argv->[$_] eq $term } keys @$argv)
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
defined(my $i = &first(sub { $argv->[$_] eq $term; } , keys @$argv)) or die "${term}: terminator not found.\n" 8 1 9
150 or die "$term: terminator not found.\n";
151 8 splice @$argv, $i, 1;
152 8 @my_argv = splice @$argv, 0, $i;
153 }
154 ▶ elsif (@$argv and
Condition83%5/6
@$argv and not $argv->[0] =~ /^-M/u and defined(my $i = &first(sub { $argv->[$_] eq "--"; } , keys @$argv))
!ll&&!rl&&r
1112
@$argv and not $argv->[0] =~ /^-M/u
!ll&&!rl&&r
0113
MC/DC66%2/3
@$argv and not $argv->[0] =~ /^-M/u and defined(my $i = &first(sub { $argv->[$_] eq "--"; } , keys @$argv))
@$argv not $argv->[0] =~ /^-M/u defined(my $i = &first(sub { $argv->[$_] eq "--"; } , keys @$argv))
Truth table75%3/4
A and not(B) and C
A= @$argvB= not $argv->[0] =~ /^-M/uC= defined(my $i = &first(sub { $argv->[$_] eq "--"; } , keys @$argv))
ABCresult
0XX0
10X0
1100
1111
155 $argv->[0] !~ /^-M/ and
156 33 ▶ defined(my $i = first { $argv->[$_] eq '--' } keys @$argv)) {
Subroutine __ANON__: called CC=1 SCAR=0.0
157 12 splice @$argv, $i, 1; # remove '--'
158 12 @my_argv = splice @$argv, 0, $i;
159 }
160 22 (\@my_argv, $argv);
161 }
162  
163 4 1;
164  
165 =encoding utf-8
166  
167 =head1 NAME
168  
169 Getopt::EX::Config - Getopt::EX module configuration interface
170  
171 =head1 SYNOPSIS
172  
173 example -Mfoo::config(foo=yabaa,bar=dabba) ...
174  
175 example -Mfoo::config(foo=yabba) --config bar=dabba ... -- ...
176  
177 example -Mfoo::config(foo=yabba) --bar=dabba ... -- ...
178  
179 example -Mfoo --foo=yabaa --bar=dabba -- ...
180  
181 =head1 VERSION
182  
183 Version 1.03
184  
185 =head1 DESCRIPTION
186  
187 This module provides an interface to define configuration information
188 for C<Getopt::EX> modules. In the traditional way, in order to set
189 options for a module, it was necessary to define dedicated command
190 line options for them. To do so, it is necessary to avoid name
191 conflicts with existing command options or with other modules used
192 together.
193  
194 Using this module, it is possible to define configuration information
195 only for the module and to define module-specific command options.
196  
197 You can create config object like this:
198  
199 use Getopt::EX::Config;
200 my $config = Getopt::EX::Config->new(
201 char => 0,
202 width => 0,
203 code => 1,
204 name => "Franky",
205 );
206  
207 This call returns hash object and each member can be accessed like
208 C<< $config->{width} >>.
209  
210 You can set these configuration values by calling C<config()> function
211 with module declaration.
212  
213 example -Mfoo::config(width,code=0) ...
214  
215 Parameter list is given by key-value pairs, and C<1> is assumed when
216 value is not given. Above code set C<width> to C<1> and C<code> to
217 C<0>.
218  
219 Also module specific options can be taken care of by calling
220 C<deal_with> method from module startup funciton C<intialize> or
221 C<finalize>.
222  
223 sub finalize {
224 our($mod, $argv) = @_;
225 $config->deal_with($argv);
226 }
227  
228 Then you can use C<--config> module option like this:
229  
230 example -Mfoo --config width,code=0 -- ...
231  
232 The module startup function is executed between the C<initialize()>
233 and C<finalize()> calls. Therefore, if you want to give priority to
234 module-specific options over the startup function, you must call
235 C<deal_with> in the C<finalize()> function.
236  
237 If you want to make module private option, say C<--width> to set C<<
238 $config->{width} >> value, C<deal_with> method takes C<Getopt::Long>
239 style option specifications.
240  
241 sub finalize {
242 our($mod, $argv) = @_;
243 $config->deal_with(
244 $argv,
245 "width!",
246 "code!",
247 "name=s",
248 );
249 }
250  
251 Then you can use module private option like this:
252  
253 example -Mcharcode --width --no-code --name=Benjy -- ...
254  
255 By default, option names with underscores are automatically aliased with
256 dash equivalents. For example, if you specify C<long_lc!>, both C<--long_lc>
257 and C<--long-lc> will work. This conversion can be disabled by setting
258 C<$Getopt::EX::Config::REPLACE_UNDERSCORE> to 0.
259  
260 The reason why it is not necessary to specify the destination of the
261 value is that the hash object is passed when calling the
262 C<Getopt::Long> library. The above code is equivalent to the
263 following code. See L<Getopt::Long/Storing options values in a hash>
264 for detail.
265  
266 =head2 Argument Terminator
267  
268 The module arguments end at the first C<-->. When they have to
269 contain C<--> themselves, for example when they are a command which
270 takes its own C<-->, put a word of three or more dashes at the start
271 of them. The word is removed and the arguments end at the same word
272 instead of C<-->:
273  
274 example -Mfoo --- cmd -x -- --- ...
275  
276 Here C<cmd -x --> is given to the module. The terminator applies
277 only to this use of the module, and it is an error if it is not
278 found. Use more dashes, like C<---->, when C<---> is also needed in
279 the arguments.
280  
281 The C<--> inside the module arguments is left in them only when the
282 module stops parsing before it, as with C<require_order> or
283 C<pass_through> configuration. Otherwise L<Getopt::Long> takes it as
284 the end of options.
285  
286 =head2 Nested Hash Configuration
287  
288 Config values can be hash references for structured configuration:
289  
290 my $config = Getopt::EX::Config->new(
291 mode => '',
292 hashed => { h3 => 0, h4 => 0, h5 => 0, h6 => 0 },
293 );
294  
295 Nested values can be accessed using dot notation in the C<config()>
296 function:
297  
298 example -Mfoo::config(hashed.h3=1,hashed.h4=1) ...
299  
300 example -Mfoo --config hashed.h3=1 -- ...
301  
302 The dot notation navigates into nested hashes: C<hashed.h3=1> sets
303 C<< $config->{hashed}{h3} >> to C<1>. The intermediate key
304 (C<hashed>) must exist as a hash reference, and the leaf key (C<h3>)
305 must already exist in that hash.
306  
307 Hash options can also be defined as module private options using
308 L<Getopt::Long> hash type (C<%>):
309  
310 $config->deal_with($argv, "hashed=s%");
311  
312 This allows:
313  
314 example -Mfoo --hashed h3=1 --hashed h4=1 -- ...
315  
316 Note that the C<Getopt::Long> hash type auto-vivifies keys, so
317 C<--hashed h3=1> works even when C<h3> does not pre-exist in the hash.
318  
319 The dot notation and nested hash support are designed with future
320 extensibility in mind. For example, a configuration file under
321 F<~/.config> could store module settings in YAML-like format:
322  
323 # ~/.config/example/foo.yml
324 mode: dark
325 hashed:
326 h3: 1
327 h4: 1
328 h5: 1
329 h6: 1
330  
331 This would map naturally to the nested hash structure and dot notation
332 already supported by this module.
333  
334 sub finalize {
335 our($mod, $argv) = @_;
336 $config->deal_with(
337 $argv,
338 "width!" => \$config->{width},
339 "code!" => \$config->{code},
340 "name=s" => \$config->{name},
341 );
342 }
343  
344 =head1 FUNCTIONS
345  
346 =over 7
347  
348 =item B<config>(I<key> => I<value>, ...)
349  
350 This module exports the function C<config> by default. As explained
351 above, this is why the C<config> function can be executed with module
352 declaration.
353  
354 If you want to use a function with a different name, specify it
355 explicitly. In the following example, the function C<set> is defined
356 and can be used in the same way as C<config>.
357  
358 use Getopt::EX::Config qw(config set);
359  
360 =item B<config>(I<key>)
361  
362 The C<config> function may also be used to refer parameters in the
363 program. In this case, specify single argument.
364  
365 my $width = config('width');
366  
367 Parameter value references can also be used as left-hand side values,
368 so values can be assigned.
369  
370 config('width') = 42;
371  
372 =back
373  
374 =head1 METHODS
375  
376 =over 7
377  
378 =item B<new>(I<key-value list>)
379  
380 =item B<new>(I<hash reference>)
381  
382 Return configuration object.
383  
384 Call with key-value list like this:
385  
386 my $config = Getopt::EX::Config->new(
387 char => 0,
388 width => 0,
389 code => 1,
390 name => "Franky",
391 );
392  
393 Or call with hash reference.
394  
395 my %config = (
396 char => 0,
397 width => 0,
398 code => 1,
399 name => "Franky",
400 );
401 my $config = Getopt::EX::Config->new(\%config);
402  
403 In this case, C<\%config> and C<$config> should be identical.
404 Do not apply C<lock_keys> to the hash before calling C<new>.
405  
406 Config keys must start with a letter (a-z, A-Z). Keys starting with
407 underscore or other characters are reserved for internal use.
408  
409 =item B<deal_with>
410  
411 You can get argument reference in C<initialize()> or C<finalize()>
412 function declared in C<Getopt::EX> module. Call C<deal_with> method
413 with that reference.
414  
415 sub finalize {
416 our($mod, $argv) = @_;
417 $config->deal_with($argv);
418 }
419  
420 You can define module specific options by giving L<Getopt::Long> style
421 definition with that call.
422  
423 sub finalize {
424 our($mod, $argv) = @_;
425 $config->deal_with($argv,
426 "width!", "code!", "name=s");
427 }
428  
429 =item B<configure>(I<options>)
430  
431 Set L<Getopt::Long> configuration options. Returns the object itself
432 for method chaining. Internally uses L<Getopt::Long::Parser> so that
433 global configuration is not affected.
434  
435 $config->configure('pass_through');
436 $config->deal_with($argv, "width!", "name=s");
437  
438 Or with method chaining:
439  
440 $config->configure('pass_through')->deal_with($argv, ...);
441  
442 =item B<argv>
443  
444 Returns the remaining arguments after C<deal_with> processing. When
445 used with C<pass_through> configuration, unrecognized options are
446 preserved and can be retrieved with this method.
447  
448 sub finalize {
449 our($mod, $argv) = @_;
450 $config->configure('pass_through')->deal_with($argv, "width!", "name=s");
451 my @extra = $config->argv; # unrecognized options
452 }
453  
454 =back
455  
456 =head1 VARIABLES
457  
458 =over 7
459  
460 =item B<$REPLACE_UNDERSCORE>
461  
462 When set to true (default), option names with underscores are automatically
463 aliased with dash equivalents. For example, C<long_lc!> becomes
464 C<long_lc|long-lc!>, allowing both C<--long_lc> and C<--long-lc> to work.
465  
466 Set to false to disable this conversion:
467  
468 $Getopt::EX::Config::REPLACE_UNDERSCORE = 0;
469  
470 =back
471  
472 =head1 SEE ALSO
473  
474 L<Getopt::EX>
475  
476 L<Getopt::Long>
477  
478 =head1 AUTHOR
479  
480 Kazumasa Utashiro
481  
482 =head1 COPYRIGHT
483  
484 The following copyright notice applies to all the files provided in
485 this distribution, including binary files, unless explicitly noted
486 otherwise.
487  
488 Copyright ©︎ 2025-2026 Kazumasa Utashiro
489  
490 =head1 LICENSE
491  
492 This library is free software; you can redistribute it and/or modify
493 it under the same terms as Perl itself.
494  
495 =cut
↑ Summary