lib/Wrapper/GetoptLong.pm

filter: Statementstmt 93.9% 109 / 116 Branchbran 65.7% 25 / 38 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 91.6% 11 / 12 Podpod 100% 7 / 7 totaltotal 86.3% 152 / 176 CC 26 SCAR 17.1
CC
26
Cov
86%
print_usage_and_die
12.0
mk_get_opt_array
7.0
init_getopts
7.0
CRAP 5.5
↑ Summary
1 package Wrapper::GetoptLong;
2  
3 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # @(#) NMG/2WM - $Id: GetoptLong.pm,v 1.1.1.1 2023/02/08 02:16:13 user Exp $
11  
12 =head1 NAME
13 Wrapper::GetoptLong - A wrapper for the Getopt::Long module
14  
15 =head1 VERSION
16 Version 0.01
17  
18 =head1 ABSTRACT
19 A wrapper to the Getopt::Long module
20  
21 =cut
22  
23 4 our $VERSION = '0.01';
24  
25 # Add your own module that defines the supporting functions, pass an instance of it to the constructor (new) as $obj.
26  
27 4 our @EXPORT = qw($obj print_usage_and_die);
28  
29 4 my %opts=();
30 4 my @usage_arr=();
31 4 my $usage='';
32 4 our $obj;
33 4 my $config_href=();
34 4 my @opts_arr=();
35 4 my $retval='';
36  
37  
38 # opt_arg_eg : is option argument example, to be used in the usage message.
39 # the -help option is a freebie, added by this module to the %OPT_CONFIG hash.
40 # with the print_usage_and_die exported function.
41  
42 4 my $dflt_help_opt= {
43 'desc' => 'Will print this usage message and exit',
44 'func' => 'print_usage_and_die()',
45 'opt_arg_eg' => '',
46 'opt_arg_type' => '',
47 };
48  
49  
50 =head1 SYNOPSIS
51  
52 A wrapper for the Getopts::Long module.
53  
54 use MyMainModule; # has the definition for function the_date. In real practice, not a demo,
55 # MyMainModule defines all the support functions for the options.
56
57 use Wrapper::GetoptLong;
58 my $mmm=MyMainModule->new();
59 my %OPTS_CONFIG=(
60 'the_date' => {
61 'desc' => q^Print today's date. Takes date_format as argument.^,
62 'func' => q^MyModule::the_date($obj->opts{'the_date'})^,
63 'opt_arg_eg' => '%m/%d/%Y',
64 'opt_arg_type' => 's',
65 },
66 );
67  
68 @ARGV=('--the_date', '%m/%d/%Y');
69 my $golw_obj=new Wrapper::GetoptLong(\%OPTS_CONFIG, $mmm);
70 $golw->run_getopt();
71 my $rc=$golw->execute_opt();
72 if(ref($rc))
73 {
74 use Data::Dumper;
75 print Dumper $rc;
76 }
77 else
78 {
79 print $rc, "\n";
80 }
81 =cut
82  
83 =head2 DEMO
84 A full demo is available on GitHub.
85 Requires a database , mySQL or MariaDB - the repository includes a file to create and populate the database.
86 https://github.com/ngabriel8/WGOLDemo
87  
88 =head1 EXPORT
89  
90 obj
91 print_usage_and_die
92  
93 =head1 METHODS
94  
95 =over 4
96  
97 =cut
98  
99  
100 =item I<new>
101  
102 my $golw = new Wrapper::GetoptLong($config_href, $MyObj);
103  
104 The constructor takes two arguments: a reference to an OPTS_CONFIG hash and an object ref (optional).
105  
106 =cut
107  
108 sub new
109 3 ▶{
Subroutine new: called CC=3 SCAR=11.0
Pod documented
110 3 my $name = basename(__FILE__, '.pm');
111 3 my $golw_usage=sprintf('Usage: my $gow=new %s(\%%OPTS_CONFIG, $object);', $name);
112 3 $!=0;
113 3 ▶(scalar(@_) == 1) && die $golw_usage;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 1 and die $golw_usage 0 3 3
114 3 my $class=shift;
115 3 ($config_href, $obj)=@_;
116 3 my $self=();
117 3 $self->{'name'}=$name;
118 3 bless($self, $class);
119 3 my @opts=sort keys %{$config_href};
120 3 my @fnd=grep /^help$/i, @opts;
121 3 ▶($#fnd == -1) && ($config_href->{'help'}=$dflt_help_opt);
Branch: 1 branch
BranchTrueFalseTotal
$#fnd == -1 and $config_href->{"help"} = $dflt_help_opt 3 0 3
122  
123 3 push(@usage_arr, "Usage $0: ");
124 3 $self->init_getopts();
125 3 return $self;
126 } # new
127  
128 =back
129  
130 =head2 print_usage_and_die
131 Prints Usage message for the calling script and exists.
132 =cut
133  
134 sub print_usage_and_die
135 0 ▶{
Subroutine print_usage_and_die: not called CC=3 SCAR=24.8
Pod documented
136 0 my($self, $extra_msg)=@_;
137 0 ▶$extra_msg ||=undef;
Condition0%0/2
$extra_msg ||= undef
l!l
00
MC/DC0%0/1
$extra_msg ||= undef
$extra_msg
Truth table0%0/2
A
A= $extra_msg
Aresult
11
00
138  
139 0 ▶(defined $extra_msg) && print STDERR $extra_msg;
Branch: 1 branch
BranchTrueFalseTotal
defined $extra_msg and print STDERR $extra_msg 0 0 0
140 0 print STDERR $usage;
141 0 exit 0;
142 } # print_usage_and_die
143  
144 =head2 init_getopts
145 Called by the constructor. Initializes usage, opts_array ...etc, for the GetOption call.
146 =cut
147  
148 sub init_getopts
149 3 ▶{
Subroutine init_getopts: called CC=7 SCAR=19.5
Pod documented
150 3 my ($self)=@_;
151 3 my @opts_no_arg=();
152 3 my @opts_with_arg=();
153 3 $self->mk_get_opt_array(\@opts_no_arg, \@opts_with_arg);
154 # cmd_str looks like this : GetOptions(\%opts, @opts_arr) || print_usage_and_die($usage, 'Invalid command line option');^;
155 3 my $no_arg_str='';
156 3 my $count=scalar(@opts_no_arg);
157 3 ▶if($count > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0) 3 0 3
158 {
159 3 ▶ $no_arg_str = ($count == 1) ? sprintf('-%s', $opts_no_arg[0]) : sprintf('-[%s]', join('|', @opts_no_arg));
Branch: 1 branch
BranchTrueFalseTotal
$count == 1 ? : 2 1 3
160 # push(@usage_arr, $no_arg_str);
161 3 $usage_arr[0] .= " $no_arg_str";
162 }
163 3 my $with_arg_str='';
164 3 $count=scalar(@opts_with_arg);
165 3 ▶if($count > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0) 1 2 3
166 {
167 1 $with_arg_str = join("\n", @opts_with_arg);
168 1 push(@usage_arr, $with_arg_str);
169 }
170 3 $self->add_desc();
171 3 my @desc_arr=();
172 3 foreach (@usage_arr)
173 {
174 12 ▶ (/^$/) && next;
Branch: 1 branch
BranchTrueFalseTotal
/^$/ and next 3 9 12
175 9 push(@desc_arr, $_);
176 }
177 3 $usage=join("\n", @desc_arr) . "\n";
178 3 ▶($#ARGV == -1) && print_usage_and_die();
Branch: 1 branch
BranchTrueFalseTotal
$#ARGV == -1 and print_usage_and_die() 0 3 3
179 } # init_getopts
180  
181 =head2 mk_get_opt_array
182 Makes the array of valid options to pass to GetOption.
183 =cut
184  
185 sub mk_get_opt_array
186 3 ▶{
Subroutine mk_get_opt_array: called CC=7 SCAR=19.5
Pod documented
187 3 my ($self, $no_arg_aref, $with_arg_aref)=@_;
188 3 @opts_arr=();
189 3 my @no_args=();
190 3 foreach my $opt (sort keys %{$config_href})
191 {
192 5 my $opt_str=$opt;
193 5 ▶ if($config_href->{$opt}->{'opt_arg_type'} eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($config_href->{$opt}{'opt_arg_type'} eq '') { } 4 1 5
194 {
195 4 push(@no_args, $opt);
196 }
197 else
198 {
199 1 $opt_str .= sprintf(':%s', $config_href->{$opt}->{'opt_arg_type'});
200 1 my $eg=$config_href->{$opt}->{'opt_arg_eg'};
201 1 push(@{$with_arg_aref}, "--$opt $eg");
202 }
203 5 push(@opts_arr, $opt_str);
204 } # foreach $opt
205 3 ▶if(scalar(@no_args))
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @no_args) 3 0 3
206 {
207 3 my @tmp=();
208 3 my $help=undef;
209 3 foreach (@no_args)
210 {
211 4 ▶ (/^help$/i) ? ($help=$_) : push(@tmp, $_);
Branch: 1 branch
BranchTrueFalseTotal
/^help$/i ? : 3 1 4
212 } # foreach @no_args
213 3 ▶ (defined $help) && unshift(@tmp, $help);
Branch: 1 branch
BranchTrueFalseTotal
defined $help and unshift @tmp, $help 3 0 3
214 3 push(@{$no_arg_aref}, @tmp);
215 } # if scalar(@no_args)
216 } # mk_get_opt_array
217  
218  
219 =head2 add_desc
220 Makes the description part of the usage message.
221  
222 =cut
223  
224 sub add_desc
225 3 ▶{
Subroutine add_desc: called CC=5 SCAR=16.1
Pod documented
226 3 my ($self)=@_;
227 3 my @desc=();
228 3 my $help='';
229 3 foreach my $opt (sort keys %{$config_href})
230 {
231 5 my $str='';
232 5 ▶ if($opt =~ /^help$/i)
Branch: 2 branches
BranchTrueFalseTotal
if ($opt =~ /^help$/i) { } 3 2 5
elsif ($config_href->{$opt}{'opt_arg_type'} eq '') { } 1 1 2
233 {
234 3 $help="-$opt $config_href->{$opt}->{'desc'}";
235 }
236 elsif($config_href->{$opt}->{'opt_arg_type'} eq '')
237 {
238 1 $str="-$opt $config_href->{$opt}->{'desc'}";
239 }
240 else
241 {
242 1 $str="--$opt $config_href->{$opt}->{'desc'}";
243 }
244 5 push(@desc, $str);
245 } # foreach $opt
246  
247 3 ▶($help) && unshift(@desc, $help);
Branch: 1 branch
BranchTrueFalseTotal
$help and unshift @desc, $help 3 0 3
248 3 push(@usage_arr, @desc);
249 } # add_desc
250  
251 =head2 run_getopt
252 Calls the GetOptions function to populate the %opts hash.
253  
254 =cut
255  
256 sub run_getopt
257 2 ▶{
Subroutine run_getopt: called CC=2 SCAR=7.2
Pod documented
258 2 my ($self)=@_;
259 2 ▶GetOptions(\%opts, @opts_arr) || print_usage_and_die('Invalid command line option');
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%opts, @opts_arr) or print_usage_and_die("Invalid command line option") 2 0 2
260 } # run_getopt
261  
262 =head2 execute_opt
263 If %opts is not empty, executes the function associated with that option (passed from the command line).
264  
265 =cut
266  
267 sub execute_opt
268 2 ▶{
Subroutine execute_opt: called CC=5 SCAR=17.0
Pod documented
269 2 my ($self)=@_;
270 2 my $rc=0;
271  
272 2 foreach my $opt (sort keys %opts)
273 {
274 2 ▶ if(defined $config_href->{$opt})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config_href->{$opt}) 2 0 2
275 {
276 2 my $cmd=sprintf('$retval=%s;', $config_href->{$opt}->{'func'});
277 # print "Working on $opt, evaluating $cmd\n";
278 2 eval($cmd);
279 2 ▶ if(!$@)
Branch: 1 branch
BranchTrueFalseTotal
if (not $@) { } 2 0 2
280 {
281 2 ▶ ($cmd =~ /print_usage_and_die/) && exit(0);
Branch: 1 branch
BranchTrueFalseTotal
$cmd =~ /print_usage_and_die/ and exit 0 0 2 2
282 2 $rc=$retval;
283 }
284 else
285 {
286 0 print STDERR $@;
287 0 $rc=1;
288 }
289 2 last; # There should be only one opt active. But, be safe...
290 }
291 } # foreach $opt
292 2 return $rc;
293 } # run_getopt
294  
295 =head1 AUTHOR
296  
297 Nazar Gabriel, C<< <ngabriel@cpan.org> >>
298  
299 =head1 BUGS
300  
301 Please report any bugs or feature requests to C<bug-wrapper-getoptlong at rt.cpan.org>, or through
302 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Wrapper-GetoptLong>. I will be notified, and then you'll
303 automatically be notified of progress on your bug as I make changes.
304  
305 =head1 SUPPORT
306  
307 You can find documentation for this module with the perldoc command.
308  
309 perldoc Wrapper::GetoptLong
310  
311 You can also look for information at:
312  
313 =over 4
314  
315 =item * RT: CPAN's request tracker (report bugs here)
316  
317 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Wrapper-GetoptLong>
318  
319 =item * CPAN Ratings
320  
321 L<https://cpanratings.perl.org/d/Wrapper-GetoptLong>
322  
323 =item * Search CPAN
324  
325 L<https://metacpan.org/release/Wrapper-GetoptLong>
326  
327 =back
328  
329  
330 =head1 ACKNOWLEDGEMENTS
331  
332 =head1 LICENSE AND COPYRIGHT
333  
334 This software is Copyright (c) 2023 by Nazar Gabriel.
335  
336 This is free software, licensed under:
337  
338 The Artistic License 2.0 (GPL Compatible)
339  
340 =cut
341  
342 4 1; # End of Wrapper::GetoptLong
↑ Summary