EX/Long.pm

filter: Statementstmt 87.6% 113 / 129 Branchbran 43.3% 13 / 30 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 80.6% 25 / 31 Podpod 42.8% 3 / 7 totaltotal 74.0% 157 / 212 CC 20 SCAR 17.3
CC
20
Cov
77%
ExConfigure
30.0
GetOptionsFromArray
6.6
__ANON__
6.0
CRAP 5.7
« EX/Colormap.pm ↑ Summary EX/Util.pm »
1 package Getopt::EX::Long;
2  
3 4 our $VERSION = "3.03";
4  
5 4 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 {
10 4 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 *REQUIRE_ORDER = \$Getopt::Long::REQUIRE_ORDER;
12 4 *PERMUTE = \$Getopt::Long::PERMUTE;
13 4 *RETURN_IN_ORDER = \$Getopt::Long::RETURN_IN_ORDER;
14  
15 4 *Configure = \&Getopt::Long::Configure;
16 4 *HelpMessage = \&Getopt::Long::HelpMessage;
17 4 *VersionMessage = \&Getopt::Long::VersionMessage;
18 }
19  
20 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 our @EXPORT = qw(&GetOptions $REQUIRE_ORDER $PERMUTE $RETURN_IN_ORDER);
22 4 our @EXPORT_OK = ( '&GetOptionsFromArray',
23 # '&GetOptionsFromString',
24 '&Configure',
25 '&HelpMessage',
26 '&VersionMessage',
27 '&ExConfigure',
28 '&ExReset',
29 );
30 4 ▶use parent qw(Getopt::Long);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 4 ▶use Getopt::Long();
Subroutine BEGIN: called CC=1 SCAR=0.0
34 4 ▶use Getopt::EX::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 4 ▶use Getopt::EX::Func qw(parse_func);
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 4 my %DefaultConfigOption = ( AUTO_DEFAULT => 1 );
38 4 my %ConfigOption = %DefaultConfigOption;
39 4 my @ValidOptions = ('AUTO_DEFAULT' , @Getopt::EX::Loader::OPTIONS);
40  
41 4 my $loader;
42 4 my $loader_pid = $$; # Set at module load time for fork detection
43  
44 0 ▶sub ExReset {
Subroutine ExReset: not called CC=1 SCAR=6.9
Pod documented
45 0 undef $loader;
46 0 %ConfigOption = %DefaultConfigOption;
47 0 $loader_pid = $$;
48 }
49  
50 2 ▶sub GetOptions {
Subroutine GetOptions: called CC=1 SCAR=0.0
Pod documented
51 2 unshift @_, \@ARGV;
52 2 goto &GetOptionsFromArray;
53 }
54  
55 2 ▶sub GetOptionsFromArray {
Subroutine GetOptionsFromArray: called CC=6 SCAR=18.8
Pod undocumented
56 2 my $argv = $_[0];
57  
58 2 ▶ ExReset() if $loader_pid != $$;
Branch: 1 branch
BranchTrueFalseTotal
if $loader_pid != $$ 0 2 2
59  
60 2 ▶ set_default() if $ConfigOption{AUTO_DEFAULT};
Branch: 1 branch
BranchTrueFalseTotal
if $ConfigOption{"AUTO_DEFAULT"} 2 0 2
61  
62 2 ▶ $loader //= Getopt::EX::Loader->new(do {
Condition33%1/3
$loader //= "Getopt::EX::Loader"->new(do { map {exists $ConfigOption{$_} ? ($_, $ConfigOption{$_}) : ();} @Getopt::EX::Loader::OPTIONS })
l!l&&r!l&&!r
020
MC/DC0%0/2
$loader //= "Getopt::EX::Loader"->new(do { map {exists $ConfigOption{$_} ? ($_, $ConfigOption{$_}) : ();} @Getopt::EX::Loader::OPTIONS })
$loader "Getopt::EX::Loader"->new(do { map {exists $ConfigOption{$_} ? ($_, $ConfigOption{$_}) : ();} @Getopt::EX::Loader::OPTIONS })
Truth table33%1/3
A //= B
A= $loaderB= "Getopt::EX::Loader"->new(do { map {exists $ConfigOption{$_} ? ($_, $ConfigOption{$_}) : ();} @Getopt::EX::Loader::OPTIONS })
ABresult
1X1
011
000
63 map {
64 2 ▶ exists $ConfigOption{$_} ? ( $_ => $ConfigOption{$_} ) : ()
Branch: 1 branch
BranchTrueFalseTotal
exists $ConfigOption{$_} ? : 3 11 14
65 } @Getopt::EX::Loader::OPTIONS
66 });
67  
68 2 $loader->deal_with($argv);
69  
70 2 my @builtins = do {
71 2 ▶ if (ref $_[1] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] eq 'HASH') { } 1 1 2
72 1 $loader->hashed_builtins($_[1]);
73 } else {
74 1 $loader->builtins;
75 }
76 };
77 2 push @_, @builtins;
78  
79 # Suppress "Duplicate specification" warnings from Getopt::Long
80 0 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
81 0 ▶ warn @_ unless $_[0] =~ /^Duplicate specification/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^Duplicate specification/u 0 0 0
82 2 };
83 2 Getopt::Long::GetOptionsFromArray(@_);
84 }
85  
86 0 ▶sub GetOptionsFromString {
Subroutine GetOptionsFromString: not called CC=1 SCAR=6.9
Pod undocumented
87 0 die "GetOptionsFromString is not supported, yet.\n";
88 }
89  
90 0 ▶sub ExConfigure {
Subroutine ExConfigure: not called CC=5 SCAR=34.0
Pod documented
91 0 ▶ ExReset() if $loader_pid != $$;
Branch: 1 branch
BranchTrueFalseTotal
if $loader_pid != $$ 0 0 0
92 0 my %opt = @_;
93 0 for my $name (@ValidOptions) {
94 0 ▶ if (exists $opt{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{$name}) 0 0 0
95 0 $ConfigOption{$name} = delete $opt{$name};
96 }
97 }
98 0 ▶ warn "Unknown option: ", Dumper \%opt if %opt;
Branch: 1 branch
BranchTrueFalseTotal
if %opt 0 0 0
99 }
100  
101 2 ▶sub set_default {
Subroutine set_default: called CC=1 SCAR=0.2
Pod undocumented
102 4 ▶ use List::Util qw(pairmap);
Subroutine BEGIN: called CC=1 SCAR=0.0
103 2 ▶ pairmap { $ConfigOption{$a} //= $b } get_default();
Subroutine __ANON__: called CC=2 SCAR=8.1
Condition33%1/3
$ConfigOption{$a} //= $b
l!l&&r!l&&!r
030
MC/DC0%0/2
$ConfigOption{$a} //= $b
$ConfigOption{$a} $b
Truth table33%1/3
A //= B
A= $ConfigOption{$a}B= $b
ABresult
1X1
011
000
104 }
105  
106 3 ▶sub get_default {
Subroutine get_default: called CC=4 SCAR=14.0
Pod undocumented
107 3 my @list;
108  
109 3 ▶ my $prog = ($0 =~ /([^\/]+)$/) ? $1 : return ();
Branch: 1 branch
BranchTrueFalseTotal
$0 =~ m[([^/]+)$]u ? : 3 0 3
110  
111 3 ▶ if (defined (my $home = $ENV{HOME})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $home = $ENV{"HOME"})) 3 0 3
112 3 ▶ if (-f (my $rc = "$home/.${prog}rc")) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f (my $rc = "$home/.${prog}rc")) 1 2 3
113 1 push @list, RCFILE => $rc;
114 }
115 }
116  
117 3 push @list, BASECLASS => "App::$prog";
118  
119 3 @list;
120 }
121  
122 4 1;
123  
124 ############################################################
125  
126 package Getopt::EX::Long::Parser;
127  
128 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
129 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
130  
131 4 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
132 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
133  
134 4 ▶use Getopt::Long(); # Load first to make Getopt::Long::Parser available
Subroutine BEGIN: called CC=1 SCAR=0.0
135 4 ▶use parent -norequire, qw(Getopt::Long::Parser);
Subroutine BEGIN: called CC=1 SCAR=0.0
136  
137 4 ▶use Getopt::EX::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
138  
139 1 ▶sub new {
Subroutine new: called CC=4 SCAR=16.0
140 1 my $class = shift;
141  
142 1 my @exconfig;
143 1 ▶ while (defined (my $i = first { $_[$_] eq 'exconfig' } keys @_)) {
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $i = &first(sub { $_[$_] eq "exconfig"; } , keys @_))) 0 1 1
144 0 push @exconfig, @{ (splice @_, $i, 2)[1] };
145 }
146 1 ▶ if (@exconfig == 0 and $ConfigOption{AUTO_DEFAULT}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@exconfig == 0 and $ConfigOption{"AUTO_DEFAULT"}) 1 0 1
Condition33%1/3
@exconfig == 0 and $ConfigOption{"AUTO_DEFAULT"}
!ll&&!rl&&r
001
MC/DC0%0/2
@exconfig == 0 and $ConfigOption{"AUTO_DEFAULT"}
@exconfig == 0 $ConfigOption{"AUTO_DEFAULT"}
Truth table33%1/3
A and B
A= @exconfig == 0B= $ConfigOption{"AUTO_DEFAULT"}
ABresult
0X0
100
111
147 1 @exconfig = Getopt::EX::Long::get_default();
148 }
149  
150 1 my $obj = $class->SUPER::new(@_);
151  
152 1 my $loader = $obj->{exloader} = Getopt::EX::Loader->new(@exconfig);
153  
154 1 $obj;
155 }
156  
157 1 ▶sub getoptionsfromarray {
Subroutine getoptionsfromarray: called CC=2 SCAR=7.5
158 1 my $obj = shift;
159 1 my $argv = $_[0];
160 1 my $loader = $obj->{exloader};
161  
162 1 $loader->deal_with($argv);
163  
164 1 my @builtins = do {
165 1 ▶ if (ref $_[1] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] eq 'HASH') { } 1 0 1
166 1 $loader->hashed_builtins($_[1]);
167 } else {
168 0 $loader->builtins;
169 }
170 };
171 1 push @_, @builtins;
172  
173 # Suppress "Duplicate specification" warnings from Getopt::Long
174 0 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
175 0 ▶ warn @_ unless $_[0] =~ /^Duplicate specification/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^Duplicate specification/u 0 0 0
176 1 };
177 1 $obj->SUPER::getoptionsfromarray(@_);
178 }
179  
180 4 1;
181  
182 =head1 NAME
183  
184 Getopt::EX::Long - Getopt::Long compatible extended module
185  
186 =head1 SYNOPSIS
187  
188 use Getopt::EX::Long;
189 GetOptions("file=s" => \my $file);
190  
191 or using the object-oriented interface:
192  
193 use Getopt::EX::Long;
194 my $parser = Getopt::EX::Long::Parser->new(
195 config => [ qw(posix_default no_ignore_case) ],
196 exconfig => [ BASECLASS => 'App::example' ],
197 );
198 $parser->getoptions("file=s" => \my $file);
199  
200 =head1 DESCRIPTION
201  
202 L<Getopt::EX::Long> is almost fully compatible with L<Getopt::Long>.
203 You can replace the module declaration, and it should work the same as
204 before (see L</INCOMPATIBILITY>).
205  
206 In addition to standard L<Getopt::Long> functionality, users can
207 define their own option aliases and write dynamically loaded extension
208 modules. If the command name is I<example>, the file
209  
210 ~/.examplerc
211  
212 is loaded by default. In this rc file, users can define option
213 aliases with macro processing. This is useful when the command takes
214 complex arguments.
215  
216 Special options starting with B<-M> load the corresponding Perl
217 module. The module is assumed to be under a specific base class. For
218 example:
219  
220 % example -Mfoo
221  
222 loads the C<App::example::foo> module by default.
223  
224 Since extension modules are normal Perl modules, users can write any
225 code they need. If the module is specified with an initial function
226 call, that function is called when the module is loaded:
227  
228 % example -Mfoo::bar(buz=100)
229  
230 This loads module B<foo> and calls function I<bar> with the parameter
231 I<buz> set to 100.
232  
233 If the module includes a C<__DATA__> section, it is interpreted as an
234 rc file. Combined with the startup function call, this allows module
235 behavior to be controlled through user-defined options.
236  
237 For details about rc files and module specification, see
238 L<Getopt::EX::Module>.
239  
240 =head1 CONFIG OPTIONS
241  
242 Config options are set by B<ExConfigure> or the B<exconfig>
243 parameter for the B<Getopt::EX::Long::Parser::new> method.
244  
245 =over 4
246  
247 =item AUTO_DEFAULT
248  
249 Config options B<RCFILE> and B<BASECLASS> are automatically set based
250 on the name of the command executable. If you don't want this behavior,
251 set B<AUTO_DEFAULT> to 0.
252  
253 =back
254  
255 Other options including B<RCFILE> and B<BASECLASS> are passed to
256 B<Getopt::EX::Loader>. Read its documentation for details.
257  
258 =head1 FUNCTIONS
259  
260 =over 4
261  
262 =item B<ExConfigure>
263  
264 Set config options. See L</CONFIG OPTIONS>.
265  
266 =item B<ExReset>
267  
268 Reset the internal loader and configuration to their default state.
269 This is useful when you need to reinitialize the module state, for
270 example after forking a child process.
271  
272 Note that B<ExConfigure> and B<GetOptionsFromArray> automatically
273 detect forked processes and reset internally, so explicit calls to
274 B<ExReset> are usually unnecessary.
275  
276 =back
277  
278 =head1 INCOMPATIBILITY
279  
280 The subroutine B<GetOptionsFromString> is not supported.
281  
282 =head1 SEE ALSO
283  
284 L<Getopt::EX>,
285 L<Getopt::EX::Module>,
286 L<Getopt::EX::Loader>
« EX/Colormap.pm ↑ Summary EX/Util.pm »