Usul/TraitFor/Usage.pm

filter: Statementstmt 74.2% 141 / 190 Branchbran 55.5% 30 / 54 Conditioncond 20.0% 2 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 74.2% 26 / 35 Podpod 100% 8 / 8 totaltotal 68.3% 207 / 303 CC 37 SCAR 19.3
CC
37
Cov
68%
__ANON__
30.0
__ANON__
20.0
__ANON__
12.0
CRAP 6.9
« Usul/TraitFor/UntaintedGetopts.pm ↑ Summary Usul/L10N.pm »
1 package Class::Usul::TraitFor::Usage;
2  
3 17 ▶use attributes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 17 ▶use Class::Inspector;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Class::Usul::Constants qw( FAILED FALSE NUL OK SPC TRUE );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use Class::Usul::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use Class::Usul::Functions qw( dash2under emit emit_to ensure_class_loaded
Subroutine BEGIN: called CC=1 SCAR=0.0
10 find_source is_member list_attr_of pad throw
11 17 untaint_cmdline untaint_identifier );
12 17 ▶use Class::Usul::IPC;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 17 ▶use Class::Usul::Types qw( Bool DataEncoding DataLumper ProcCommer );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 17 ▶use Class::Usul::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 17 requires qw( config dumper next_argv options_usage output quiet );
20  
21 # Public attributes
22 option 'encoding' => is => 'lazy', isa => DataEncoding,
23 documentation => 'Decode/encode input/output using this encoding',
24 21 ▶ default => sub { $_[ 0 ]->config->encoding }, format => 's';
Subroutine __ANON__: called CC=1 SCAR=0.0
25  
26 17 option 'help_manual' => is => 'ro', isa => Bool, default => FALSE,
27 documentation => 'Displays the documentation for the program',
28 short => 'H';
29  
30 17 option 'help_options' => is => 'ro', isa => Bool, default => FALSE,
31 documentation => 'Describes program options and methods',
32 short => 'h';
33  
34 17 option 'help_usage' => is => 'ro', isa => Bool, default => FALSE,
35 documentation => 'Displays this command line usage',
36 short => '?';
37  
38 17 option 'show_version' => is => 'ro', isa => Bool, default => FALSE,
39 documentation => 'Displays the version number of the program class';
40  
41 has 'file' => is => 'lazy', isa => DataLumper,
42 0 ▶ builder => sub { Class::Usul::File->new( builder => $_[ 0 ] ) };
Subroutine __ANON__: called CC=1 SCAR=0.4
Subroutine _build_file: not called CC=1 SCAR=0.4
43  
44 has 'ipc' => is => 'lazy', isa => ProcCommer,
45 8 ▶ builder => sub { Class::Usul::IPC->new( builder => $_[ 0 ] ) },
Subroutine _build_ipc: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
46 17 handles => [ 'run_cmd' ];
47  
48 # Class attributes
49 17 my $_can_call_cache = {}; my $_method_cache = {};
50  
51 # Private functions
52 5 ▶my $_list_methods_of = sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
53 5 ▶ my $class = blessed $_[ 0 ] || $_[ 0 ];
Condition33%1/3
builtin::blessed($_[0]) || $_[0]
l!l&&r!l&&!r
500
MC/DC0%0/2
builtin::blessed($_[0]) || $_[0]
builtin::blessed($_[0]) $_[0]
Truth table33%1/3
A || B
A= builtin::blessed($_[0])B= $_[0]
ABresult
1X1
011
000
54  
55 exists $_method_cache->{ $class } or $_method_cache->{ $class }
56 9 = [ map { s{ \A .+ :: }{}msx; $_ }
57 141 grep { my $subr = $_;
58 141 grep { $_ eq 'method' } attributes::get( \&{ $subr } ) }
59 5 ▶ @{ Class::Inspector->methods( $class, 'full', 'public' ) } ];
Branch: 1 branch
BranchTrueFalseTotal
exists $_method_cache->{$class} or $_method_cache->{$class} = [map({s/ \A .+ :: //msx; $_;} grep({my $subr = $_; grep {$_ eq "method";} &attributes::get(\&{$subr;});} @{"Class::Inspector"->methods($class, "full", "public");}))] 3 2 5
60  
61 5 return $_method_cache->{ $class };
62 17 };
63  
64 8 ▶my $_get_pod_header_for_method = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
65 8 my ($class, $method) = @_;
66  
67 8 ▶ my $src = find_source $class
Branch: 1 branch
BranchTrueFalseTotal
my $src = &find_source($class) or &throw("Class [_1] cannot find source", [$class]) 8 0 8
68 or throw 'Class [_1] cannot find source', [ $class ];
69 312 my $ev = [ grep { $_->{content} =~ m{ (?: ^|[< ]) $method (?: [ >]|$ ) }msx}
70 1040 grep { $_->{type} eq 'command' }
71 8 @{ Pod::Eventual::Simple->read_file( $src ) } ]->[ 0 ];
72 8 ▶ my $pod = $ev ? $ev->{content} : undef; $pod and chomp $pod;
Branch: 2 branches
BranchTrueFalseTotal
$ev ? : 4 4 8
$pod and chomp $pod 4 4 8
73  
74 8 return $pod;
75 17 };
76  
77 # Private methods
78 21 ▶my $_apply_stdio_encoding = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
79 21 my $self = shift; my $enc = untaint_cmdline $self->encoding;
80  
81 21 for (*STDIN, *STDOUT, *STDERR) {
82 63 ▶ $_->opened or next; binmode $_, ":encoding(${enc})";
Branch: 1 branch
BranchTrueFalseTotal
$_->opened or next 63 0 63
83 }
84  
85 21 autoflush STDOUT TRUE; autoflush STDERR TRUE;
86 21 return;
87 17 };
88  
89 1 ▶my $_get_classes_and_roles = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
90 1 my $self = shift; my %uniq = (); ensure_class_loaded 'mro';
91  
92 1 my @classes = @{ mro::get_linear_isa( blessed $self ) };
93  
94 1 ▶ while (my $class = shift @classes) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = shift @classes) 19 1 20
95 19 $class = (split m{ __WITH__ }mx, $class)[ 0 ];
96 19 ▶ $class =~ m{ ::_BASE \z }mx and next;
Branch: 1 branch
BranchTrueFalseTotal
$class =~ / ::_BASE \z /mx and next 0 19 19
97 19 $class =~ s{ \A Role::Tiny::_COMPOSABLE:: }{}mx;
98 19 ▶ $uniq{ $class } and next; $uniq{ $class }++;
Branch: 1 branch
BranchTrueFalseTotal
$uniq{$class} and next 10 9 19
99  
100 exists $Role::Tiny::APPLIED_TO{ $class }
101 9 ▶ and push @classes, keys %{ $Role::Tiny::APPLIED_TO{ $class } };
Branch: 1 branch
BranchTrueFalseTotal
exists $Role::Tiny::APPLIED_TO{$class} and push @classes, keys %{$Role::Tiny::APPLIED_TO{$class};} 9 0 9
102 }
103  
104 1 return [ sort keys %uniq ];
105 17 };
106  
107 0 ▶my $_man_page_from = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
108 0 my ($self, $src) = @_; ensure_class_loaded 'Pod::Man';
109  
110 0 my $conf = $self->config;
111 0 ▶ my $parser = Pod::Man->new( center => $conf->doc_title || NUL,
Condition0%0/2
$conf->doc_title || ''
l!l
00
MC/DC0%0/1
$conf->doc_title || ''
$conf->doc_title
Truth table0%0/2
A
A= $conf->doc_title
Aresult
11
00
112 name => $conf->script,
113 release => 'Version '.$self->app_version,
114 section => '3m' );
115 0 ▶ my $cmd = $conf->man_page_cmd || [];
Condition0%0/2
$conf->man_page_cmd || []
l!l
00
MC/DC0%0/1
$conf->man_page_cmd || []
$conf->man_page_cmd
Truth table0%0/2
A
A= $conf->man_page_cmd
Aresult
11
00
116 0 my $tempfile = $self->file->tempfile;
117  
118 0 $parser->parse_from_file( $src->pathname.NUL, $tempfile->pathname );
119 0 emit $self->run_cmd( [ @{ $cmd }, $tempfile->pathname ] )->out;
120 0 return OK;
121 17 };
122  
123 0 ▶my $_usage_for = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
124 0 my ($self, $method) = @_; ensure_class_loaded 'Pod::Select';
125  
126 0 for my $class (@{ $self->$_get_classes_and_roles }) {
127 0 ▶ is_member( $method, Class::Inspector->methods( $class, 'public' ) )
Branch: 1 branch
BranchTrueFalseTotal
&is_member($method, "Class::Inspector"->methods($class, "public")) or next 0 0 0
128 or next;
129  
130 0 my $selector = Pod::Select->new(); my $tfile = $self->file->tempfile;
131  
132 0 $selector->select( "/(?:[A-Z][\<])?${method}.*" );
133 0 $selector->parse_from_file( find_source $class, $tfile->pathname );
134 0 ▶ $tfile->stat->{size} > 0 and return $self->$_man_page_from( $tfile );
Branch: 1 branch
BranchTrueFalseTotal
$tfile->stat->{"size"} > 0 and return $self->$_man_page_from($tfile) 0 0 0
135 }
136  
137 0 emit_to \*STDERR, "Method ${method} no documentation found\n";
138 0 return FAILED;
139 17 };
140  
141 0 ▶my $_output_usage = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
142 0 my ($self, $verbose) = @_; my $method = $self->next_argv;
143  
144 0 ▶ defined $method and $method = untaint_identifier dash2under $method;
Branch: 1 branch
BranchTrueFalseTotal
defined $method and $method = &untaint_identifier(scalar &dash2under($method)) 0 0 0
145  
146 0 ▶ $self->can_call( $method ) and return $self->$_usage_for( $method );
Branch: 1 branch
BranchTrueFalseTotal
$self->can_call($method) and return $self->$_usage_for($method) 0 0 0
147  
148 0 ▶ $verbose > 1 and return $self->$_man_page_from( $self->config );
Branch: 1 branch
BranchTrueFalseTotal
$verbose > 1 and return $self->$_man_page_from($self->config) 0 0 0
149  
150 0 ▶ ensure_class_loaded 'Pod::Usage'; $verbose > 0 and Pod::Usage::pod2usage
Branch: 1 branch
BranchTrueFalseTotal
$verbose > 0 and Pod::Usage::pod2usage({"-exitval", 0, "-input", $self->config->pathname . '', "-message", " ", "-verbose", $verbose}) 0 0 0
151 ( { -exitval => OK,
152 -input => $self->config->pathname.NUL,
153 -message => SPC,
154 -verbose => $verbose } ); # Never returns
155  
156 0 emit_to \*STDERR, $self->options_usage;
157 0 return FAILED;
158 17 };
159  
160 # Construction
161 21 ▶before 'BUILD' => sub {
Subroutine __ANON__: called CC=5 SCAR=17.0
162 21 my $self = shift; $self->$_apply_stdio_encoding;
163  
164 21 ▶ $self->help_usage and $self->exit_usage( 0 );
Branch: 1 branch
BranchTrueFalseTotal
$self->help_usage and $self->exit_usage(0) 0 21 21
165 21 ▶ $self->help_options and $self->exit_usage( 1 );
Branch: 1 branch
BranchTrueFalseTotal
$self->help_options and $self->exit_usage(1) 0 21 21
166 21 ▶ $self->help_manual and $self->exit_usage( 2 );
Branch: 1 branch
BranchTrueFalseTotal
$self->help_manual and $self->exit_usage(2) 0 21 21
167 21 ▶ $self->show_version and $self->exit_version;
Branch: 1 branch
BranchTrueFalseTotal
$self->show_version and $self->exit_version 0 21 21
168 21 return;
169 17 };
170  
171 # Public methods
172 4 ▶sub app_version {
Subroutine app_version: called CC=1 SCAR=0.0
Pod documented
173 4 my $self = shift; my $class = $self->config->appclass;
174  
175 0 ▶ my $ver = try { ensure_class_loaded $class; $class->VERSION } catch { '?' };
Subroutine __ANON__: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
176  
177 4 return $ver;
178 }
179  
180 10 ▶sub can_call {
Subroutine can_call: called CC=4 SCAR=13.9
Pod documented
181 10 ▶ my ($self, $wanted) = @_; $wanted or return FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$wanted or return 0 10 0 10
182  
183 10 ▶ exists $_can_call_cache->{ $wanted } or $_can_call_cache->{ $wanted }
Branch: 2 branches
BranchTrueFalseTotal
exists $_can_call_cache->{$wanted} or $_can_call_cache->{$wanted} = &is_member($wanted, &$_list_methods_of($self)) ? 1 : 0 6 4 10
&is_member($wanted, &$_list_methods_of($self)) ? : 3 1 4
184 = (is_member $wanted, $_list_methods_of->( $self )) ? TRUE : FALSE;
185  
186 10 return $_can_call_cache->{ $wanted };
187 }
188  
189 0 ▶sub dump_config_attr : method {
Subroutine dump_config_attr: not called CC=1 SCAR=6.9
Pod documented
190 0 my $self = shift; my @except =
191 qw( BUILDARGS BUILD inflate_path inflate_paths inflate_symbol new secret);
192  
193 0 $self->dumper( [ list_attr_of $self->config, @except ] );
194  
195 0 return OK;
196 }
197  
198 1 ▶sub dump_self : method {
Subroutine dump_self: called CC=1 SCAR=0.0
Pod documented
199 1 my $self = shift;
200  
201 1 $self->dumper( $self ); $self->dumper( $self->config );
202  
203 1 return OK;
204 }
205  
206 0 ▶sub exit_usage {
Subroutine exit_usage: not called CC=2 SCAR=17.9
Pod documented
207 0 my ($self, $level) = @_; $self->quiet( TRUE );
208  
209 0 my $rv = $self->$_output_usage( $level );
210  
211 0 ▶ if ($level == 0) { emit "\nMethods:\n"; $self->list_methods }
Branch: 1 branch
BranchTrueFalseTotal
if ($level == 0) 0 0 0
212  
213 0 exit $rv;
214 }
215  
216 0 ▶sub exit_version {
Subroutine exit_version: not called CC=1 SCAR=6.9
Pod documented
217 0 $_[ 0 ]->output( 'Version '.$_[ 0 ]->app_version ); exit OK;
218 }
219  
220 0 ▶sub help : method {
Subroutine help: not called CC=1 SCAR=6.9
Pod documented
221 0 my $self = shift; $self->$_output_usage( 1 ); return OK;
222 }
223  
224 1 ▶sub list_methods : method {
Subroutine list_methods: called CC=9 SCAR=22.1
Pod documented
225 1 my $self = shift; ensure_class_loaded 'Pod::Eventual::Simple';
226  
227 1 my $abstract = {}; my $max = 0; my $classes = $self->$_get_classes_and_roles;
228  
229 1 for my $method (@{ $_list_methods_of->( $self ) }) {
230 4 ▶ my $mlen = length $method; $mlen > $max and $max = $mlen;
Branch: 1 branch
BranchTrueFalseTotal
$mlen > $max and $max = $mlen 1 3 4
231  
232 4 for my $class (@{ $classes }) {
233 36 ▶ is_member( $method, Class::Inspector->methods( $class, 'public' ) )
Branch: 1 branch
BranchTrueFalseTotal
&is_member($method, "Class::Inspector"->methods($class, "public")) or next 8 28 36
234 or next;
235  
236 8 ▶ my $pod = $_get_pod_header_for_method->( $class, $method ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $pod = &$_get_pod_header_for_method($class, $method) or next 4 4 8
237  
238 (not exists $abstract->{ $method }
239 or length $pod > length $abstract->{ $method })
240 4 ▶ and $abstract->{ $method } = $pod;
Branch: 1 branch
BranchTrueFalseTotal
!exists $abstract->{$method} || length $pod > length $abstract->{$method} and $abstract->{$method} = $pod 4 0 4
Condition33%1/3
not exists $abstract->{$method} or length $pod > length $abstract->{$method}
l!l&&r!l&&!r
400
MC/DC0%0/2
not exists $abstract->{$method} or length $pod > length $abstract->{$method}
not exists $abstract->{$method} length $pod > length $abstract->{$method}
Truth table33%1/3
not(A) or B
A= not exists $abstract->{$method}B= length $pod > length $abstract->{$method}
ABresult
1X1
011
000
241 }
242 }
243  
244 1 for my $key (sort keys %{ $abstract }) {
245 4 my ($method, @rest) = split SPC, $abstract->{ $key };
246  
247 4 $key =~ s{ [_] }{-}gmx; emit( (pad $key, $max).SPC.(join SPC, @rest) );
248 }
249  
250 1 return OK;
251 }
252  
253 17 1;
254  
255 __END__
« Usul/TraitFor/UntaintedGetopts.pm ↑ Summary Usul/L10N.pm »