File Coverage

blib/lib/Getopt/EX/Long.pm
Criterion Covered Total %
statement 96 112 85.7
branch 12 28 42.8
condition 3 9 33.3
subroutine 25 31 80.6
pod 3 7 42.8
total 139 187 74.3


line stmt bran cond sub pod time code
1             package Getopt::EX::Long;
2              
3             our $VERSION = "3.03";
4              
5 4     4   313765 use v5.14;
  4         13  
6 4     4   16 use warnings;
  4         5  
  4         167  
7 4     4   16 use Carp;
  4         4  
  4         208  
8              
9             {
10 4     4   13 no warnings 'once';
  4         6  
  4         395  
11             *REQUIRE_ORDER = \$Getopt::Long::REQUIRE_ORDER;
12             *PERMUTE = \$Getopt::Long::PERMUTE;
13             *RETURN_IN_ORDER = \$Getopt::Long::RETURN_IN_ORDER;
14              
15             *Configure = \&Getopt::Long::Configure;
16             *HelpMessage = \&Getopt::Long::HelpMessage;
17             *VersionMessage = \&Getopt::Long::VersionMessage;
18             }
19              
20 4     4   20 use Exporter 'import';
  4         26  
  4         262  
21             our @EXPORT = qw(&GetOptions $REQUIRE_ORDER $PERMUTE $RETURN_IN_ORDER);
22             our @EXPORT_OK = ( '&GetOptionsFromArray',
23             # '&GetOptionsFromString',
24             '&Configure',
25             '&HelpMessage',
26             '&VersionMessage',
27             '&ExConfigure',
28             '&ExReset',
29             );
30 4     4   1052 use parent qw(Getopt::Long);
  4         774  
  4         19  
31              
32 4     4   48152 use Data::Dumper;
  4         6  
  4         233  
33 4     4   22 use Getopt::Long();
  4         7  
  4         63  
34 4     4   1253 use Getopt::EX::Loader;
  4         9  
  4         225  
35 4     4   24 use Getopt::EX::Func qw(parse_func);
  4         5  
  4         1527  
36              
37             my %DefaultConfigOption = ( AUTO_DEFAULT => 1 );
38             my %ConfigOption = %DefaultConfigOption;
39             my @ValidOptions = ('AUTO_DEFAULT' , @Getopt::EX::Loader::OPTIONS);
40              
41             my $loader;
42             my $loader_pid = $$; # Set at module load time for fork detection
43              
44             sub ExReset {
45 0     0 1 0 undef $loader;
46 0         0 %ConfigOption = %DefaultConfigOption;
47 0         0 $loader_pid = $$;
48             }
49              
50             sub GetOptions {
51 2     2 1 396359 unshift @_, \@ARGV;
52 2         9 goto &GetOptionsFromArray;
53             }
54              
55             sub GetOptionsFromArray {
56 2     2 0 4 my $argv = $_[0];
57              
58 2 50       17 ExReset() if $loader_pid != $$;
59              
60 2 50       15 set_default() if $ConfigOption{AUTO_DEFAULT};
61              
62 2   33     13 $loader //= Getopt::EX::Loader->new(do {
63             map {
64 2 100       6 exists $ConfigOption{$_} ? ( $_ => $ConfigOption{$_} ) : ()
  14         45  
65             } @Getopt::EX::Loader::OPTIONS
66             });
67              
68 2         10 $loader->deal_with($argv);
69              
70 2         3 my @builtins = do {
71 2 100       7 if (ref $_[1] eq 'HASH') {
72 1         11 $loader->hashed_builtins($_[1]);
73             } else {
74 1         3 $loader->builtins;
75             }
76             };
77 2         6 push @_, @builtins;
78              
79             # Suppress "Duplicate specification" warnings from Getopt::Long
80             local $SIG{__WARN__} = sub {
81 0 0   0   0 warn @_ unless $_[0] =~ /^Duplicate specification/;
82 2         12 };
83 2         12 Getopt::Long::GetOptionsFromArray(@_);
84             }
85              
86             sub GetOptionsFromString {
87 0     0 0 0 die "GetOptionsFromString is not supported, yet.\n";
88             }
89              
90             sub ExConfigure {
91 0 0   0 1 0 ExReset() if $loader_pid != $$;
92 0         0 my %opt = @_;
93 0         0 for my $name (@ValidOptions) {
94 0 0       0 if (exists $opt{$name}) {
95 0         0 $ConfigOption{$name} = delete $opt{$name};
96             }
97             }
98 0 0       0 warn "Unknown option: ", Dumper \%opt if %opt;
99             }
100              
101             sub set_default {
102 4     4   24 use List::Util qw(pairmap);
  4         5  
  4         831  
103 3   33 3 0 21 pairmap { $ConfigOption{$a} //= $b } get_default();
  2     2   16  
104             }
105              
106             sub get_default {
107 3     3 0 5 my @list;
108              
109 3 50       28 my $prog = ($0 =~ /([^\/]+)$/) ? $1 : return ();
110              
111 3 50       31 if (defined (my $home = $ENV{HOME})) {
112 3 100       103 if (-f (my $rc = "$home/.${prog}rc")) {
113 1         3 push @list, RCFILE => $rc;
114             }
115             }
116              
117 3         10 push @list, BASECLASS => "App::$prog";
118              
119 3         20 @list;
120             }
121              
122             1;
123              
124             ############################################################
125              
126             package Getopt::EX::Long::Parser;
127              
128 4     4   22 use strict;
  4         5  
  4         117  
129 4     4   15 use warnings;
  4         8  
  4         190  
130              
131 4     4   32 use List::Util qw(first);
  4         4  
  4         232  
132 4     4   20 use Data::Dumper;
  4         5  
  4         194  
133              
134 4     4   19 use Getopt::Long(); # Load first to make Getopt::Long::Parser available
  4         6  
  4         103  
135 4     4   14 use parent -norequire, qw(Getopt::Long::Parser);
  4         4  
  4         61  
136              
137 4     4   197 use Getopt::EX::Loader;
  4         8  
  4         1605  
138              
139             sub new {
140 1     1   198728 my $class = shift;
141              
142 1         2 my @exconfig;
143 1     0   11 while (defined (my $i = first { $_[$_] eq 'exconfig' } keys @_)) {
  0         0  
144 0         0 push @exconfig, @{ (splice @_, $i, 2)[1] };
  0         0  
145             }
146 1 50 33     11 if (@exconfig == 0 and $ConfigOption{AUTO_DEFAULT}) {
147 1         3 @exconfig = Getopt::EX::Long::get_default();
148             }
149              
150 1         12 my $obj = $class->SUPER::new(@_);
151              
152 1         1056 my $loader = $obj->{exloader} = Getopt::EX::Loader->new(@exconfig);
153              
154 1         4 $obj;
155             }
156              
157             sub getoptionsfromarray {
158 1     1   37 my $obj = shift;
159 1         2 my $argv = $_[0];
160 1         2 my $loader = $obj->{exloader};
161              
162 1         4 $loader->deal_with($argv);
163              
164 1         2 my @builtins = do {
165 1 50       3 if (ref $_[1] eq 'HASH') {
166 1         3 $loader->hashed_builtins($_[1]);
167             } else {
168 0         0 $loader->builtins;
169             }
170             };
171 1         3 push @_, @builtins;
172              
173             # Suppress "Duplicate specification" warnings from Getopt::Long
174             local $SIG{__WARN__} = sub {
175 0 0   0   0 warn @_ unless $_[0] =~ /^Duplicate specification/;
176 1         6 };
177 1         9 $obj->SUPER::getoptionsfromarray(@_);
178             }
179              
180             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 is almost fully compatible with L.
203             You can replace the module declaration, and it should work the same as
204             before (see L).
205              
206             In addition to standard L functionality, users can
207             define their own option aliases and write dynamically loaded extension
208             modules. If the command name is I, 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 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 and calls function I with the parameter
231             I 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.
239              
240             =head1 CONFIG OPTIONS
241              
242             Config options are set by B or the B
243             parameter for the B method.
244              
245             =over 4
246              
247             =item AUTO_DEFAULT
248              
249             Config options B and B are automatically set based
250             on the name of the command executable. If you don't want this behavior,
251             set B to 0.
252              
253             =back
254              
255             Other options including B and B are passed to
256             B. Read its documentation for details.
257              
258             =head1 FUNCTIONS
259              
260             =over 4
261              
262             =item B
263              
264             Set config options. See L.
265              
266             =item B
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 and B automatically
273             detect forked processes and reset internally, so explicit calls to
274             B are usually unnecessary.
275              
276             =back
277              
278             =head1 INCOMPATIBILITY
279              
280             The subroutine B is not supported.
281              
282             =head1 SEE ALSO
283              
284             L,
285             L,
286             L