TaskPipe/Tool/Options.pm

filter: Statementstmt 94.6% 106 / 112 Branchbran 64.7% 22 / 34 Conditioncond 66.6% 8 / 12 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 5 totaltotal 83.2% 154 / 185 CC 29 SCAR 24.1
CC
29
Cov
86%
add_specs
21.3
get_args
8.0
load
4.0
CRAP 11.2
« TaskPipe/UserAgentManager/UserAgentHandler_PhantomJS.pm ↑ Summary TaskPipe/InterpParam.pm »
1 package TaskPipe::Tool::Options;
2  
3 40 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use Getopt::Long qw(:config pass_through);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use MooseX::ConfigCascade::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use TaskPipe::PodReader;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 40 ▶use Module::Runtime 'require_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3533 ▶has specs => (is => 'rw', isa => 'HashRef', default => sub{{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
12 3533 ▶has specs_by_module => (is => 'rw', isa => 'HashRef', default => sub{{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
13 40 has args => (is => 'rw', isa => 'HashRef');
14 40 has unused_args => (is => 'rw', isa => 'HashRef');
15  
16 has pod_reader => (is => 'rw', isa => 'TaskPipe::PodReader', default => sub{
17 3533 ▶ TaskPipe::PodReader->new;
Subroutine __ANON__: called CC=1 SCAR=0.0
18 40 });
19  
20  
21 sub load{
22 1652 ▶ my $self = shift;
Subroutine load: called CC=4 SCAR=13.9
Pod undocumented
23  
24 1652 ▶ confess "args is not defined (call get_args first?)" unless defined $self->args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->args 0 1652 1652
25  
26 1652 foreach my $name (keys %{$self->specs}){
27  
28 25524 my $val = $self->args->{$name};
29 25524 delete $self->unused_args->{$name};
30 25524 my $type = $self->specs->{$name}->{type};
31 25524 ▶ MooseX::ConfigCascade::Util->conf->{ $self->specs->{$name}->{module} }->{$name} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 2720 22804 25524
32  
33 }
34  
35 }
36  
37  
38 sub check_unused_args{
39 525 ▶ my ($self) = @_;
Subroutine check_unused_args: called CC=2 SCAR=9.2
Pod undocumented
40  
41 525 ▶ if ( %{$self->unused_args} ){
Branch: 1 branch
BranchTrueFalseTotal
if (%{$self->unused_args;}) 0 525 525
42 0 my @arg_keys = keys( %{$self->unused_args} );
43 0 confess '1 or more command line arguments was not recognised ("'.join('","',@arg_keys).'")';
44 }
45 }
46  
47  
48  
49  
50 sub get_args{
51 826 ▶ my ($self) = @_;
Subroutine get_args: called CC=8 SCAR=20.8
Pod undocumented
52  
53 826 my $arg_sets = [];
54 826 my $arg_set = [];
55 826 foreach my $arg (@ARGV){
56  
57 4372 ▶ if ( $arg =~ /^--/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^--/) { } 3012 1360 4372
58 3012 ▶ push @$arg_sets,$arg_set if @$arg_set;
Branch: 1 branch
BranchTrueFalseTotal
if @$arg_set 2186 826 3012
59 3012 $arg_set = [$arg];
60 } else {
61 1360 ▶ push @$arg_set,$arg if @$arg_set;
Branch: 1 branch
BranchTrueFalseTotal
if @$arg_set 1360 0 1360
62 }
63 }
64 826 ▶ push @$arg_sets,$arg_set if @$arg_set;
Branch: 1 branch
BranchTrueFalseTotal
if @$arg_set 826 0 826
65  
66 826 my $arg_strings = [];
67 826 foreach my $arg_set (@$arg_sets){
68 3012 my $arg_string = join(' ',@$arg_set);
69 3012 push @$arg_strings,$arg_string;
70 }
71  
72 826 my $args = {};
73 826 foreach my $arg_string (@$arg_strings ){
74 3012 my ($key,$val) = split /[=\s]+/, $arg_string, 2;
75 3012 $key =~ s/^\s*--//;
76 3012 $key =~ s/\s*$//;
77 3012 $val =~ s/^\s*//;
78 3012 $val =~ s/\s*$//;
79 3012 $args->{$key} = $val;
80 }
81  
82 826 $self->args( $args );
83 826 $self->unused_args( $args );
84 826 return $args;
85 }
86  
87  
88  
89  
90 sub add_specs{
91 2478 ▶ my ($self,$specs) = @_;
Subroutine add_specs: called CC=18 SCAR=30.6
Pod undocumented
92  
93 2478 foreach my $spec (@$specs){
94  
95 4207 my %opt;
96 4207 ▶ if ( ref $spec eq ref {} ){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $spec eq ref {}) { } 4207 0 4207
elsif (ref $spec eq ref '') { } 0 0 0
97 4207 %opt = %$spec;
98 } elsif ( ref $spec eq ref '') {
99 0 %opt = ( module => $spec );
100 }
101  
102 4207 ▶ confess "A module name must be specified" unless $opt{module};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"module"} 0 4207 4207
103 4207 my $is_config = 0;
104 4207 ▶ $is_config = 1 if $opt{is_config};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"is_config"} 3381 826 4207
105  
106 4207 my $options_section;
107  
108 4207 ▶ if ( $is_config ){
Branch: 1 branch
BranchTrueFalseTotal
if ($is_config) { } 3381 826 4207
109 3381 ▶ $options_section = $opt{section} || 'METHODS';
Condition50%1/2
$opt{'section'} || 'METHODS'
l!l
03381
MC/DC0%0/1
$opt{'section'} || 'METHODS'
$opt{'section'}
Truth table50%1/2
A
A= $opt{'section'}
Aresult
11
00
110 } else {
111 826 ▶ $options_section = $opt{section} || 'OPTIONS';
Condition50%1/2
$opt{'section'} || 'OPTIONS'
l!l
0826
MC/DC0%0/1
$opt{'section'} || 'OPTIONS'
$opt{'section'}
Truth table50%1/2
A
A= $opt{'section'}
Aresult
11
00
112 }
113  
114
115 4207 my $pod = $self->pod_reader->read_pod( $opt{module} );
116  
117 4207 my ($head1) = grep { $_->title eq $options_section } $pod->head1;
118  
119 4207 ▶ confess "No $options_section section found in $opt{module}" unless $head1;
Branch: 1 branch
BranchTrueFalseTotal
unless $head1 0 4207 4207
120 4207 ▶ confess "No 'over' section in $opt{module}" unless $head1->over->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $head1->over->[0] 0 4207 4207
121  
122 4207 my @opt_items = $head1->over->[0]->item;
123  
124  
125 4207 my $loaded = $opt{module}->new;
126 4207 my $defaults = $self->get_defaults( $opt{module} );
127  
128 4207 my $module_conf = {};
129 4207 foreach my $opt_item (@opt_items){
130  
131 29367 ▶ next if $opt{items} && ! grep { $_ eq $opt_item->title } @{$opt{items}};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"items"} and not grep({$_ eq $opt_item->title;} @{$opt{"items"};}) 14280 15087 29367
Condition100%3/3
$opt{"items"} and not grep({$_ eq $opt_item->title;} @{$opt{"items"};})
!ll&&!rl&&r
38941119314280
MC/DC100%2/2
$opt{"items"} and not grep({$_ eq $opt_item->title;} @{$opt{"items"};})
$opt{"items"} not grep({$_ eq $opt_item->title;} @{$opt{"items"};})
Truth table100%3/3
A and not(B)
A= $opt{"items"}B= not grep({$_ eq $opt_item->title;} @{$opt{"items"};})
ABresult
0X0
100
111
132 15087 ▶ next if $opt{exclude} && grep { $_ eq $opt_item->title } @{$opt{exclude}};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"exclude"} and grep {$_ eq $opt_item->title;} @{$opt{"exclude"};} 0 15087 15087
Condition33%1/3
$opt{"exclude"} and grep {$_ eq $opt_item->title;} @{$opt{"exclude"};}
!ll&&!rl&&r
1508700
MC/DC0%0/2
$opt{"exclude"} and grep {$_ eq $opt_item->title;} @{$opt{"exclude"};}
$opt{"exclude"} grep {$_ eq $opt_item->title;} @{$opt{"exclude"};}
Truth table33%1/3
A and B
A= $opt{"exclude"}B= grep {$_ eq $opt_item->title;} @{$opt{"exclude"};}
ABresult
0X0
100
111
133  
134 15087 my $opt_method = $opt_item->title;
135 15087 ▶ confess "method $opt_method specfied in pod, but no attribute exists in $opt{module}" unless $opt{module}->can($opt_method);
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"module"}->can($opt_method) 0 15087 15087
136  
137 15087 my ($attr) = grep { $_->name eq $opt_method } $opt{module}->meta->get_all_attributes;
138  
139 15087 my $constraint = $attr->type_constraint;
140  
141
142 15087 $module_conf->{$opt_method} = $loaded->$opt_method;
143 $self->specs->{$opt_method} = {
144  
145 pod => $opt_item,
146 default => $defaults->$opt_method,
147 module => $opt{module},
148 15087 ▶ is_config => $opt{is_config} || 0
Condition100%2/2
$opt{'is_config'} || 0
l!l
134471640
MC/DC100%1/1
$opt{'is_config'} || 0
$opt{'is_config'}
Truth table100%2/2
A
A= $opt{'is_config'}
Aresult
11
00
149  
150 };
151
152 }
153  
154 4207 $self->specs_by_module->{ $opt{module} } = $module_conf;
155 }
156 }
157
158  
159 sub get_defaults{
160 4207 ▶ my ($self,$module) = @_;
Subroutine get_defaults: called CC=1 SCAR=0.0
Pod undocumented
161  
162 4207 my $current_conf = MooseX::ConfigCascade::Util->conf;
163 4207 MooseX::ConfigCascade::Util->conf({});
164 4207 require_module( $module );
165 4207 my $defaults = $module->new;
166 4207 MooseX::ConfigCascade::Util->conf( $current_conf );
167  
168 4207 return $defaults;
169 }
170  
171  
172 =head1 NAME
173  
174 TaskPipe::Tool::Options - handles command line options for TaskPipe Tool
175  
176 =head1 DESCRIPTION
177  
178 This is the module responsible for loading and managing parameters specified at the command line when running the C<taskpipe> script
179  
180 =head1 AUTHOR
181  
182 Tom Gracey <tomgracey@gmail.com>
183  
184 =head1 COPYRIGHT AND LICENSE
185  
186 Copyright (c) Tom Gracey 2018
187  
188 TaskPipe is free software, licensed under
189  
190 The GNU Public License Version 3
191  
192 =cut
193  
194 40 1;
« TaskPipe/UserAgentManager/UserAgentHandler_PhantomJS.pm ↑ Summary TaskPipe/InterpParam.pm »