File Coverage

lib/LEOCHARRE/CLI2.pm
Criterion Covered Total %
statement 138 188 73.4
branch 44 90 48.8
condition 10 30 33.3
subroutine 27 32 84.3
pod 10 14 71.4
total 229 354 64.6


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