blib/lib/MooX/Role/CliOptions.pm

filter: Statementstmt 94.4% 51 / 54 Branchbran 68.1% 15 / 22 Conditioncond 64.2% 9 / 14 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 90.0% 9 / 10 Podpod 100% 1 / 1 totaltotal 80.9% 89 / 110 CC 17 SCAR 26.7
CC
17
Cov
83%
init
21.5
_pod2usage
2.0
BEGIN
1.0
CRAP 14.5
↑ Summary examples/moodulino.pl »
1 package MooX::Role::CliOptions;
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 Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use Getopt::Long v2.36 qw(GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # this could have been replaced with hand-rolled "isa" clauses, but if
13 # one is using Moo then the odds are good that one already has this
14 4 ▶use Types::Standard qw( ArrayRef Bool );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 our $VERSION = '0.05';
19  
20 4 my @options = ( 'help', 'man' );
21 do {
22 3 has debug => (
23 is => 'ro',
24 isa => Bool,
25 default => 1,
26 );
27  
28 3 has verbose => (
29 is => 'ro',
30 isa => Bool,
31 default => 1,
32 );
33  
34 3 push( @options, qw( debug! verbose! ) );
35 4 ▶} unless $ENV{MRC_NO_STDOPTS};
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"MRC_NO_STDOPTS"}) 3 1 4
36  
37 4 has argv => (
38 is => 'ro',
39 isa => ArrayRef,
40 );
41  
42 25 ▶sub init {
Subroutine init: called CC=17 SCAR=30.7
Pod documented
43 25 my $class = shift;
44 25 my %args = @_;
45  
46 25 ▶ my $argv = delete( $args{argv} ) or croak q{'argv' argument is required};
Branch: 1 branch
BranchTrueFalseTotal
my $argv = delete $args{"argv"} or croak("'argv' argument is required") 25 0 25
47 25 ▶ croak q{'argv' must be an array reference'}
Branch: 1 branch
BranchTrueFalseTotal
unless ref $argv and ref $argv eq "ARRAY" 0 25 25
Condition33%1/3
ref $argv and ref $argv eq "ARRAY"
!ll&&!rl&&r
0025
MC/DC0%0/2
ref $argv and ref $argv eq "ARRAY"
ref $argv ref $argv eq "ARRAY"
Truth table33%1/3
A and B
A= ref $argvB= ref $argv eq "ARRAY"
ABresult
0X0
100
111
48 unless ref($argv) && ref($argv) eq 'ARRAY';
49  
50 25 ▶ my $add_opts = delete( $args{add_opts} ) || [];
Condition100%2/2
delete $args{'add_opts'} || []
l!l
1411
MC/DC100%1/1
delete $args{'add_opts'} || []
delete $args{'add_opts'}
Truth table100%2/2
A
A= delete $args{'add_opts'}
Aresult
11
00
51 25 ▶ croak q{'add_opts' must be an array reference'}
Branch: 1 branch
BranchTrueFalseTotal
unless ref $add_opts and ref $add_opts eq "ARRAY" 0 25 25
Condition33%1/3
ref $add_opts and ref $add_opts eq "ARRAY"
!ll&&!rl&&r
0025
MC/DC0%0/2
ref $add_opts and ref $add_opts eq "ARRAY"
ref $add_opts ref $add_opts eq "ARRAY"
Truth table33%1/3
A and B
A= ref $add_optsB= ref $add_opts eq "ARRAY"
ABresult
0X0
100
111
52 unless ref($add_opts) && ref($add_opts) eq 'ARRAY';
53  
54 25 ▶ croak q{unknown argument supplied for 'init'} if keys(%args);
Branch: 1 branch
BranchTrueFalseTotal
if keys %args 0 25 25
55  
56 25 push( @options, $_ ) for ( @{$add_opts} );
57  
58 25 my %values;
59 25 ▶ GetOptionsFromArray( $argv, \%values, @options ) or _pod2usage(2);
Branch: 1 branch
BranchTrueFalseTotal
&GetOptionsFromArray($argv, \%values, @options) or _pod2usage(2) 21 4 25
60 25 ▶ _pod2usage(1) if $values{help};
Branch: 1 branch
BranchTrueFalseTotal
if $values{"help"} 0 25 25
61 25 ▶ _pod2usage( -exitstatus => 0, -verbose => 2 ) if $values{man};
Branch: 1 branch
BranchTrueFalseTotal
if $values{"man"} 0 25 25
62  
63 25 $values{argv} = $argv;
64  
65 do {
66 # have (no)debug imply verbose if that is not not specificed.
67 # This is a # personal preference based on my experience of how
68 # these options are most commonly used.
69 $values{verbose} ||= $values{debug}
70 16 ▶ if defined( $values{debug} ) && !defined( $values{verbose} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $values{"debug"} and not defined $values{"verbose"} 4 12 16
Condition83%5/6
defined $values{"debug"} and not defined $values{"verbose"}
!ll&&!rl&&r
1024
$values{"verbose"} ||= $values{"debug"}
l!l&&r!l&&!r
022
MC/DC100%2/2
defined $values{"debug"} and not defined $values{"verbose"}
defined $values{"debug"} not defined $values{"verbose"}
MC/DC50%1/2
$values{"verbose"} ||= $values{"debug"}
$values{"verbose"} $values{"debug"}
Truth table100%3/3
A and not(B)
A= defined $values{"debug"}B= not defined $values{"verbose"}
ABresult
0X0
100
111
Truth table66%2/3
A ||= B
A= $values{"verbose"}B= $values{"debug"}
ABresult
1X1
011
000
71 25 ▶ } unless $ENV{MRC_NO_STDOPTS};
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"MRC_NO_STDOPTS"}) 16 9 25
72  
73 25 my $app = eval { $class->new(%values); };
74 25 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 25 25
75 0 print $@ . "\n";
76 0 _pod2usage(2);
77 } if $@;
78  
79 25 return $app;
80 }
81  
82 # this is needed so test scripts can intercept the call to pod2usage and
83 # prevent the exit from happening
84 0 ▶sub _pod2usage {
Subroutine _pod2usage: not called CC=1 SCAR=6.9
85 0 pod2usage(@_);
86 }
87  
88 4 1; # End of MooX::Role::CliOptions
89 __END__
↑ Summary examples/moodulino.pl »