| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 58 | 3012 | ▶ | push @$arg_sets,$arg_set if @$arg_set; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 3012 | $arg_set = [$arg]; | |||||||||||||||||||||||||||
| 60 | } else { | ||||||||||||||||||||||||||||
| 61 | 1360 | ▶ | push @$arg_set,$arg if @$arg_set; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 826 | ▶ | push @$arg_sets,$arg_set if @$arg_set; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 103 | 4207 | my $is_config = 0; | |||||||||||||||||||||||||||
| 104 | 4207 | ▶ | $is_config = 1 if $opt{is_config}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 4207 | my $options_section; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 4207 | ▶ | if ( $is_config ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 3381 | ▶ | $options_section = $opt{section} || 'METHODS'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opt{'section'} || 'METHODS'
MC/DC0%0/1
$opt{'section'} || 'METHODS'
$opt{'section'}
Truth table50%1/2
A
A= $opt{'section'}
| |||||||||||||||||||||||||||||
| 110 | } else { | ||||||||||||||||||||||||||||
| 111 | 826 | ▶ | $options_section = $opt{section} || 'OPTIONS'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opt{'section'} || 'OPTIONS'
MC/DC0%0/1
$opt{'section'} || 'OPTIONS'
$opt{'section'}
Truth table50%1/2
A
A= $opt{'section'}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 120 | 4207 | ▶ | confess "No 'over' section in $opt{module}" unless $head1->over->[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$opt{"items"} and not grep({$_ eq $opt_item->title;} @{$opt{"items"};})
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"};})
| |||||||||||||||||||||||||||||
| 132 | 15087 | ▶ | next if $opt{exclude} && grep { $_ eq $opt_item->title } @{$opt{exclude}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$opt{"exclude"} and grep {$_ eq $opt_item->title;} @{$opt{"exclude"};}
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"};}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$opt{'is_config'} || 0
$opt{'is_config'}
Truth table100%2/2
A
A= $opt{'is_config'}
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||