EX/Long.pm

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