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 42 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 42 ▶use Getopt::Long qw(:config pass_through);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 42 ▶use MooseX::ConfigCascade::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 42 ▶use TaskPipe::PodReader;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 42 ▶use Module::Runtime 'require_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 42 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 42 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3785 ▶has specs => (is => 'rw', isa => 'HashRef', default => sub{{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
12 3785 ▶has specs_by_module => (is => 'rw', isa => 'HashRef', default => sub{{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
13 42 has args => (is => 'rw', isa => 'HashRef');
14 42 has unused_args => (is => 'rw', isa => 'HashRef');
15  
16 has pod_reader => (is => 'rw', isa => 'TaskPipe::PodReader', default => sub{
17 3785 ▶ TaskPipe::PodReader->new;
Subroutine __ANON__: called CC=1 SCAR=0.0
18 42 });
19  
20  
21 sub load{
22 1764 ▶ my $self = shift;
Subroutine load: called CC=4 SCAR=13.9
Pod undocumented
23  
24 1764 ▶ confess "args is not defined (call get_args first?)" unless defined $self->args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->args 0 1764 1764
25  
26 1764 foreach my $name (keys %{$self->specs}){
27  
28 27188 my $val = $self->args->{$name};
29 27188 delete $self->unused_args->{$name};
30 27188 my $type = $self->specs->{$name}->{type};
31 27188 ▶ MooseX::ConfigCascade::Util->conf->{ $self->specs->{$name}->{module} }->{$name} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 2916 24272 27188
32  
33 }
34  
35 }
36  
37  
38 sub check_unused_args{
39 567 ▶ my ($self) = @_;
Subroutine check_unused_args: called CC=2 SCAR=9.2
Pod undocumented
40  
41 567 ▶ if ( %{$self->unused_args} ){
Branch: 1 branch
BranchTrueFalseTotal
if (%{$self->unused_args;}) 0 567 567
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 882 ▶ my ($self) = @_;
Subroutine get_args: called CC=8 SCAR=20.8
Pod undocumented
52  
53 882 my $arg_sets = [];
54 882 my $arg_set = [];
55 882 foreach my $arg (@ARGV){
56  
57 4680 ▶ if ( $arg =~ /^--/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^--/) { } 3222 1458 4680
58 3222 ▶ push @$arg_sets,$arg_set if @$arg_set;
Branch: 1 branch
BranchTrueFalseTotal
if @$arg_set 2340 882 3222
59 3222 $arg_set = [$arg];
60 } else {
61 1458 ▶ push @$arg_set,$arg if @$arg_set;
Branch: 1 branch
BranchTrueFalseTotal
if @$arg_set 1458 0 1458
62 }
63 }
64 882 ▶ push @$arg_sets,$arg_set if @$arg_set;
Branch: 1 branch
BranchTrueFalseTotal
if @$arg_set 882 0 882
65  
66 882 my $arg_strings = [];
67 882 foreach my $arg_set (@$arg_sets){
68 3222 my $arg_string = join(' ',@$arg_set);
69 3222 push @$arg_strings,$arg_string;
70 }
71  
72 882 my $args = {};
73 882 foreach my $arg_string (@$arg_strings ){
74 3222 my ($key,$val) = split /[=\s]+/, $arg_string, 2;
75 3222 $key =~ s/^\s*--//;
76 3222 $key =~ s/\s*$//;
77 3222 $val =~ s/^\s*//;
78 3222 $val =~ s/\s*$//;
79 3222 $args->{$key} = $val;
80 }
81  
82 882 $self->args( $args );
83 882 $self->unused_args( $args );
84 882 return $args;
85 }
86  
87  
88  
89  
90 sub add_specs{
91 2646 ▶ my ($self,$specs) = @_;
Subroutine add_specs: called CC=18 SCAR=30.6
Pod undocumented
92  
93 2646 foreach my $spec (@$specs){
94  
95 4473 my %opt;
96 4473 ▶ if ( ref $spec eq ref {} ){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $spec eq ref {}) { } 4473 0 4473
elsif (ref $spec eq ref '') { } 0 0 0
97 4473 %opt = %$spec;
98 } elsif ( ref $spec eq ref '') {
99 0 %opt = ( module => $spec );
100 }
101  
102 4473 ▶ confess "A module name must be specified" unless $opt{module};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"module"} 0 4473 4473
103 4473 my $is_config = 0;
104 4473 ▶ $is_config = 1 if $opt{is_config};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"is_config"} 3591 882 4473
105  
106 4473 my $options_section;
107  
108 4473 ▶ if ( $is_config ){
Branch: 1 branch
BranchTrueFalseTotal
if ($is_config) { } 3591 882 4473
109 3591 ▶ $options_section = $opt{section} || 'METHODS';
Condition50%1/2
$opt{'section'} || 'METHODS'
l!l
03591
MC/DC0%0/1
$opt{'section'} || 'METHODS'
$opt{'section'}
Truth table50%1/2
A
A= $opt{'section'}
Aresult
11
00
110 } else {
111 882 ▶ $options_section = $opt{section} || 'OPTIONS';
Condition50%1/2
$opt{'section'} || 'OPTIONS'
l!l
0882
MC/DC0%0/1
$opt{'section'} || 'OPTIONS'
$opt{'section'}
Truth table50%1/2
A
A= $opt{'section'}
Aresult
11
00
112 }
113  
114
115 4473 my $pod = $self->pod_reader->read_pod( $opt{module} );
116  
117 4473 my ($head1) = grep { $_->title eq $options_section } $pod->head1;
118  
119 4473 ▶ confess "No $options_section section found in $opt{module}" unless $head1;
Branch: 1 branch
BranchTrueFalseTotal
unless $head1 0 4473 4473
120 4473 ▶ confess "No 'over' section in $opt{module}" unless $head1->over->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $head1->over->[0] 0 4473 4473
121  
122 4473 my @opt_items = $head1->over->[0]->item;
123  
124  
125 4473 my $loaded = $opt{module}->new;
126 4473 my $defaults = $self->get_defaults( $opt{module} );
127  
128 4473 my $module_conf = {};
129 4473 foreach my $opt_item (@opt_items){
130  
131 31157 ▶ 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"};}) 15120 16037 31157
Condition100%3/3
$opt{"items"} and not grep({$_ eq $opt_item->title;} @{$opt{"items"};})
!ll&&!rl&&r
41301190715120
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 16037 ▶ 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 16037 16037
Condition33%1/3
$opt{"exclude"} and grep {$_ eq $opt_item->title;} @{$opt{"exclude"};}
!ll&&!rl&&r
1603700
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 16037 my $opt_method = $opt_item->title;
135 16037 ▶ 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 16037 16037
136  
137 16037 my ($attr) = grep { $_->name eq $opt_method } $opt{module}->meta->get_all_attributes;
138  
139 16037 my $constraint = $attr->type_constraint;
140  
141
142 16037 $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 16037 ▶ is_config => $opt{is_config} || 0
Condition100%2/2
$opt{'is_config'} || 0
l!l
143011736
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 4473 $self->specs_by_module->{ $opt{module} } = $module_conf;
155 }
156 }
157
158  
159 sub get_defaults{
160 4473 ▶ my ($self,$module) = @_;
Subroutine get_defaults: called CC=1 SCAR=0.0
Pod undocumented
161  
162 4473 my $current_conf = MooseX::ConfigCascade::Util->conf;
163 4473 MooseX::ConfigCascade::Util->conf({});
164 4473 require_module( $module );
165 4473 my $defaults = $module->new;
166 4473 MooseX::ConfigCascade::Util->conf( $current_conf );
167  
168 4473 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 42 1;
« TaskPipe/UserAgentManager/UserAgentHandler_PhantomJS.pm ↑ Summary TaskPipe/InterpParam.pm »