lib/LEOCHARRE/CLI2.pm

filter: Statementstmt 28.2% 33 / 117 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 55.0% 11 / 20 Podpod n/a totaltotal 19.4% 44 / 226 CC 37 SCAR 39.7
CC
37
Cov
17%
opt_selected
132.0
slurp
90.0
import_resolve_opt_string
56.0
CRAP 53.2
↑ Summary
1 package LEOCHARRE::CLI2;
2 7 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS %OPT @OPT_KEYS $OPT_STRING %ARGV);
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Getopt::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 my @export_argv = qw/argv_files argv_files_count argv_dirs argv_dirs_count argv_cwd/;
11 @ISA = qw(Exporter);
12 @EXPORT_OK = ( qw/yn sq cwd abs_path slurp burp opt_selected user_exists/, @export_argv );
13 %EXPORT_TAGS = ( argv => \@export_argv, all => \@EXPORT_OK, );
14 $VERSION = sprintf "%d.%02d", q$Revision: 1.16 $ =~ /(\d+)/g;
15  
16 #use Smart::Comments '###';
17 7 ▶use String::ShellQuote;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 *sq = \&String::ShellQuote::shell_quote;
21 *abs_path = \&Cwd::abs_path;
22 *cwd = \&Cwd::cwd;
23  
24  
25 0 ▶sub user_exists {
Subroutine user_exists: not called CC=4 SCAR=30.0
26 0 my $uname = shift;
27 0 ▶ $uname=~/\w+/ or Carp::cluck("missing user argument") and return;
Branch: 1 branch
BranchTrueFalseTotal
$uname =~ /\w+/ or Carp::cluck("missing user argument") and return 0 0 0
Condition0%0/5
$uname =~ /\w+/ || Carp::cluck('missing user argument') && (return)
l!l&&r!l&&!r
000
Carp::cluck("missing user argument") and return
!ll
00
MC/DC0%0/2
$uname =~ /\w+/ || Carp::cluck('missing user argument') && (return)
$uname =~ /\w+/ Carp::cluck("missing user argument")
Truth table0%0/3
A || B
A= $uname =~ /\w+/B= Carp::cluck("missing user argument")
ABresult
1X1
011
000
28 0 ▶ ( system('id', $uname ) == 0) ? 1 : 0
Branch: 1 branch
BranchTrueFalseTotal
system('id', $uname) == 0 ? : 0 0 0
29 }
30  
31 0 ▶sub opt_selected {
Subroutine opt_selected: not called CC=11 SCAR=48.8
32  
33 0 ▶ if (@_){ # then we want to check that every one of thse and no more are selected
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
34 0 my %want;
35 0 @want{@_} =();
36 0 for ( keys %OPT ){
37 0 ▶ if( defined $OPT{$_} ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $LEOCHARRE::CLI2::OPT{$_}) 0 0 0
38 0 ▶ exists $want{$_} or return; # then one is set which we did not ask for
Branch: 1 branch
BranchTrueFalseTotal
exists $want{$_} or return 0 0 0
39 0 $want{$_}++;
40 }
41 }
42 0 for (keys %want){ # make sure they have all been seen as set
43 0 ▶ $want{$_} or return;
Branch: 1 branch
BranchTrueFalseTotal
$want{$_} or return 0 0 0
44 }
45 0 return 1;
46 }
47  
48 0 my @selected;
49 0 for(keys %OPT){
50 0 ▶ defined $OPT{$_} and push @selected, $_;
Branch: 1 branch
BranchTrueFalseTotal
defined $LEOCHARRE::CLI2::OPT{$_} and push @selected, $_ 0 0 0
51 }
52 0 ▶ @selected or return;
Branch: 1 branch
BranchTrueFalseTotal
@selected or return 0 0 0
53 0 ▶ wantarray ? (@selected) : [@selected];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
54 }
55  
56 0 ▶sub slurp {
Subroutine slurp: not called CC=9 SCAR=45.0
57 0 my $abs = shift;
58 0 ▶ -f $abs or Carp::cluck("Not on disk '$abs'") and return;
Branch: 1 branch
BranchTrueFalseTotal
-f $abs or Carp::cluck("Not on disk '${abs}'") and return 0 0 0
Condition0%0/5
-f $abs || Carp::cluck("Not on disk '${abs}'") && (return)
l!l&&r!l&&!r
000
Carp::cluck("Not on disk '${abs}'") and return
!ll
00
MC/DC0%0/2
-f $abs || Carp::cluck("Not on disk '${abs}'") && (return)
-f $abs Carp::cluck("Not on disk '${abs}'")
Truth table0%0/3
A || B
A= -f $absB= Carp::cluck("Not on disk '${abs}'")
ABresult
1X1
011
000
59  
60  
61 0 ▶ open( FILE, '<', $abs ) or warn("Could not open for reading '$abs', $!") and return;
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<", $abs or warn "Could not open for reading '${abs}', $!" and return 0 0 0
Condition0%0/5
open(FILE, '<', $abs) || warn("Could not open for reading '${abs}', $!") && (return)
l!l&&r!l&&!r
000
warn "Could not open for reading '${abs}', $!" and return
!ll
00
MC/DC0%0/2
open(FILE, '<', $abs) || warn("Could not open for reading '${abs}', $!") && (return)
open(FILE, '<', $abs) warn "Could not open for reading '${abs}', $!"
Truth table0%0/3
A || B
A= open(FILE, '<', $abs)B= warn "Could not open for reading '${abs}', $!"
ABresult
1X1
011
000
62  
63 0 ▶ if (wantarray){
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
64 0 my @lines = <FILE>;
65 0 close FILE;
66 0 ▶ @lines and scalar @lines or return _empty();
Branch: 1 branch
BranchTrueFalseTotal
@lines and scalar @lines or return _empty() 0 0 0
Condition0%0/2
@lines and scalar @lines
!ll
00
MC/DC0%0/1
@lines and scalar @lines
@lines
Truth table0%0/2
A
A= @lines
Aresult
00
11
67 0 return @lines;
68 }
69  
70 else {
71  
72 0 local $/;
73 0 my $txt = <FILE>;
74 0 close FILE;
75 0 ▶ (length $txt) or return _empty();
Branch: 1 branch
BranchTrueFalseTotal
length $txt or return _empty() 0 0 0
76 0 $txt;
77 }
78  
79 0 ▶ sub _empty { Carp::cluck("Nothing inside :'$abs' ?"); return; }
Subroutine _empty: not called CC=1 SCAR=6.9
80 }
81  
82 0 ▶sub burp {
Subroutine burp: not called CC=5 SCAR=34.0
83 0 my $abs = shift;
84 0 my $content = shift;
85 0 ▶ defined $content or Carp::cluck("No content arg provided") and return;
Branch: 1 branch
BranchTrueFalseTotal
defined $content or Carp::cluck("No content arg provided") and return 0 0 0
Condition0%0/5
defined $content || Carp::cluck('No content arg provided') && (return)
l!l&&r!l&&!r
000
Carp::cluck("No content arg provided") and return
!ll
00
MC/DC0%0/2
defined $content || Carp::cluck('No content arg provided') && (return)
defined $content Carp::cluck("No content arg provided")
Truth table0%0/3
A || B
A= defined $contentB= Carp::cluck("No content arg provided")
ABresult
1X1
011
000
86 0 ▶ open( FILE,'>', $abs) or warn("Could not open for writing '$abs', $!") and return;
Branch: 1 branch
BranchTrueFalseTotal
open FILE, ">", $abs or warn "Could not open for writing '${abs}', $!" and return 0 0 0
Condition0%0/5
open(FILE, '>', $abs) || warn("Could not open for writing '${abs}', $!") && (return)
l!l&&r!l&&!r
000
warn "Could not open for writing '${abs}', $!" and return
!ll
00
MC/DC0%0/2
open(FILE, '>', $abs) || warn("Could not open for writing '${abs}', $!") && (return)
open(FILE, '>', $abs) warn "Could not open for writing '${abs}', $!"
Truth table0%0/3
A || B
A= open(FILE, '>', $abs)B= warn "Could not open for writing '${abs}', $!"
ABresult
1X1
011
000
87 0 print FILE $content;
88 0 close FILE;
89 0 $abs;
90 }
91  
92 0 ▶sub import {
Subroutine import: not called CC=3 SCAR=20.6
93 0 my $class = shift;
94  
95 # find the opt string
96 0 import_resolve_opt_string(\@_);
97 0 import_make_opts();
98
99 0 _init_env_ext();
100  
101  
102 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
103 0 ▶ main->can('debug') or *{'main::debug'} = \&debug;
Branch: 1 branch
BranchTrueFalseTotal
"main"->can("debug") or *{"main::debug";} = \&LEOCHARRE::CLI2::debug 0 0 0
104 0 ▶ main->can('usage') or *{'main::usage'} = \&usage;
Branch: 1 branch
BranchTrueFalseTotal
"main"->can("usage") or *{"main::usage";} = \&LEOCHARRE::CLI2::usage 0 0 0
105 ### @_
106  
107 0 __PACKAGE__->export_to_level(1, ( $class, @_));
108 }
109  
110  
111 0 ▶sub import_resolve_opt_string {
Subroutine import_resolve_opt_string: not called CC=7 SCAR=40.3
112 ### finding opt string..
113 0 my $import_list = shift;
114  
115 0 my @changed_list;
116
117 0 for my $arg ( @$import_list ){
118 ### testing arg -----------------
119 ### $arg
120  
121 # if arg is between brackers, it is a definition for parent package
122 0 ▶ if ($arg=~/^\[(.+)\]$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^\[(.+)\]$/) 0 0 0
123 0 $ENV{SCRIPT_PARENT_PACKAGE} = $1;
124 0 next;
125 }
126  
127 # if arg is between parens, it is a definition for what man page to look up more in
128 0 ▶ if ($arg=~/^\((.+)\)$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^\((.+)\)$/) 0 0 0
129
130 0 $ENV{SCRIPT_MAN} = $1;
131 0 next;
132 }
133  
134 # if the arg has spaces, it is deemed as the SCRIPT_DESCRIPTION
135 0 ▶ if ($arg=~/ /){
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ / /) 0 0 0
136 0 $ENV{SCRIPT_DESCRIPTION} = $arg;
137 0 next;
138 }
139  
140  
141 0 my $tag = $arg;
142 0 $tag=~s/^\://;
143
144 0 ▶ if( __PACKAGE__->can($arg) or $EXPORT_TAGS{$tag} ){
Branch: 1 branch
BranchTrueFalseTotal
if (__PACKAGE__->can($arg) or $LEOCHARRE::CLI2::EXPORT_TAGS{$tag}) 0 0 0
Condition0%0/3
__PACKAGE__->can($arg) or $LEOCHARRE::CLI2::EXPORT_TAGS{$tag}
l!l&&r!l&&!r
000
MC/DC0%0/2
__PACKAGE__->can($arg) or $LEOCHARRE::CLI2::EXPORT_TAGS{$tag}
__PACKAGE__->can($arg) $LEOCHARRE::CLI2::EXPORT_TAGS{$tag}
Truth table0%0/3
A or B
A= __PACKAGE__->can($arg)B= $LEOCHARRE::CLI2::EXPORT_TAGS{$tag}
ABresult
1X1
011
000
145 ### arg is a sub or export tag:
146 ### $arg
147 0 push @changed_list, $arg;
148 0 next;
149 }
150 ### arg is not a sub or export tag
151  
152  
153
154 #$opt_string and die("bad args? cant have $arg as export arg?");
155 0 $OPT_STRING = $arg;
156 ### $OPT_STRING
157 }
158  
159 # replace the import list
160 0 @$import_list = @changed_list;
161  
162 # note that this does NOT replace the list:
163 # $import_list = \@changed_list
164 # it just changes the reference! ;-)
165  
166  
167 ### $import_list
168  
169
170
171 }
172  
173 0 ▶sub _init_env_ext {
Subroutine _init_env_ext: not called CC=1 SCAR=6.9
174  
175 0 $0=~/([^\/]+)$/;
176 0 $ENV{SCRIPT_FILENAME} = $1;
177  
178 }
179  
180  
181 0 ▶sub import_make_opts {
Subroutine import_make_opts: not called CC=4 SCAR=26.1
182
183 0 for my $l ( qw/h d/ ){ # took out v version, won't work
184 0 ▶ $OPT_STRING=~/$l/ or $OPT_STRING.=$l;
Branch: 1 branch
BranchTrueFalseTotal
$LEOCHARRE::CLI2::OPT_STRING =~ /$l/ or $LEOCHARRE::CLI2::OPT_STRING .= $l 0 0 0
185 }
186  
187  
188 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
189 0 *{'main::OPT'} = \%OPT;
190 0 *{'main::OPT_STRING'} = \$OPT_STRING;
191  
192  
193 0 require Getopt::Std;
194 0 Getopt::Std::getopts($OPT_STRING, \%OPT);
195
196 0 my $_opt_string = $OPT_STRING;
197 0 $_opt_string=~s/\W//g;
198 0 @OPT_KEYS = split(//, $_opt_string);
199 ## @OPT_KEYS
200
201 # make variables
202 0 for my $opt_key (@OPT_KEYS){
203 0 *{"main\::opt_$opt_key"} = \$OPT{$opt_key};
204 }
205  
206
207  
208 }
209  
210  
211  
212  
213 # ARGV ----- begin
214 sub _argv {
215 defined %ARGV or _init_argv();
216 if (my $key = shift){
217 return $ARGV{$key};
218 }
219 \%ARGV;
220 }
221
222 sub _init_argv {
223  
224 my @_argv;
225 my(@files,$files_count, @dirs, $dirs_count);
226  
227 ### -------------------------------- init argv paths
228 for my $arg ( @ARGV ){
229 defined $arg or next;
230 ### testing for disk arg
231 ### $arg
232
233 my ($isf, $isd) = ( -f $arg, -d $arg );
234  
235 unless( $isf or $isd ){
236  
237 ### arg -f/-d no
238 push @_argv, $arg; # leave alone
239 next;
240 }
241  
242
243 my $abs = Cwd::abs_path($arg);
244  
245 $isf and (push @files, $abs) and next;
246 push @dirs, $abs;
247 }
248  
249 if( $ARGV{DIRS_COUNT} = ( (scalar @dirs) || 0 ) ){
250 $ARGV{DIRS} = \@dirs;
251 $ARGV{CWD} = $dirs[0];
252 }
253 else {
254 $ARGV{CWD}= Cwd::abs_path('./');
255 }
256  
257 if( $ARGV{FILES_COUNT} = ( (scalar @files) || 0 ) ){
258 $ARGV{FILES} = \@files;
259 }
260  
261 ### %ARGV
262  
263
264 @ARGV = @_argv;
265 }
266  
267  
268 sub argv_files { _argv('FILES') or return; @{_argv('FILES')} }
269 sub argv_files_count { _argv('FILES_COUNT') }
270 sub argv_dirs { _argv('DIRS') or return; @{_argv('DIRS')} }
271 sub argv_dirs_count { _argv('DIRS_COUNT') }
272 sub argv_cwd { _argv('CWD') }
273
274  
275 # end argv------------
276  
277  
278  
279  
280 INIT {
281 ### LEOCHARRE CLI2 INIT
282 $main::opt_h
283 and print STDERR &main::usage
284 and exit;
285 }
286  
287  
288 sub debug { $main::opt_d and warn(" # $ENV{SCRIPT_FILENAME}, @_\n"); 1 }
289  
290  
291 sub yn {
292 my $question = shift;
293 $question ||='Your answer? ';
294 my $val = undef;
295 until (defined $val){
296 print "$question (y/n): ";
297 $val = <STDIN>;
298 chomp $val;
299 if ($val eq 'y'){ $val = 1; }
300 elsif ($val eq 'n'){ $val = 0;}
301 else { $val = undef; }
302 }
303 return $val;
304 }
305  
306  
307 # auto generated usage
308 sub usage {
309  
310 my $script_name = $ENV{SCRIPT_FILENAME};
311 my $script_description = $ENV{SCRIPT_DESCRIPTION};
312 my $script_man = $ENV{SCRIPT_MAN};
313 my $script_also = $ENV{SCRIPT_PARENT_PACKAGE};
314  
315 my $script_version = $main::VERSION;
316  
317 $script_version and ($script_version=" v $script_version");
318
319 $script_description and $script_description=~s/\n*$/\n/;
320
321 if( $script_man ){
322 unless( $script_man=~/man /){
323 $script_man = "\nTry 'man $script_man' for more info.\n";
324 }
325 }
326  
327 if( $script_also ){
328 $script_also = "\n$script_also - parent package\n";
329 }
330  
331  
332  
333 my $out = "$script_name [OPTION]...\n$script_description\n";
334  
335
336 for my $opt ( sort keys %OPT ){
337 my $desc =
338 $opt eq 'h' ? 'help' :
339 $opt eq 'd' ? 'debug' : undef;
340  
341 my $argtype;
342 if (!$desc){
343 # does it take an arg?
344 if ($main::OPT_STRING=~/$opt\:/){
345 $desc=undef;
346 $argtype='string';
347 }
348 }
349 no warnings;
350 $out.= sprintf "%6s %-10s %s\n",
351 "-$opt", $argtype, $desc;
352 }
353  
354 "$out\n$script_man$script_also";
355 }
356  
357  
358  
359  
360  
361 1;
362  
363  
364 __END__
↑ Summary