oCLI/Request.pm

filter: Statementstmt 73.3% 55 / 75 Branchbran 72.2% 26 / 36 Conditioncond 86.6% 13 / 15 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 55.5% 5 / 9 Podpod 0.0% 0 / 1 totaltotal 73.1% 106 / 145 CC 24 SCAR 28.3
CC
24
Cov
75%
new_from_command_line
23.1
__ANON__
6.0
_build_command_class
6.0
CRAP 16.9
« oCLI/Context.pm ↑ Summary oCLI/Plugin.pm »
1 package oCLI::Request;
2 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Scalar::Util qw( looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 has [qw( overrides command stdin )] => (
6 is => 'ro',
7 );
8  
9 has args => (
10 is => 'ro',
11 11 ▶ default => sub { return [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
12 2 );
13  
14 has settings => (
15 is => 'ro',
16 6 ▶ default => sub { return +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
17 2 );
18  
19 has command_class => (
20 is => 'ro',
21 lazy => 1,
22 0 ▶ builder => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Subroutine _build_command_class: not called CC=2 SCAR=17.9
23 0 my ( $self ) = @_;
24  
25 0 ▶ return "" unless index($self->command, ':') >= 0;
Branch: 2 branches
BranchTrueFalseTotal
unless index($self->command, ":") != -1 0 0 0
unless index($self->command, ":") != -1 0 0 0
26  
27 0 my @parts = split /:/, $self->command;
28 0 delete $parts[-1];
29  
30 0 return join "::", map { ucfirst(lc($_)) } @parts;
31 },
32 2 );
33  
34 has command_name => (
35 is => 'ro',
36 lazy => 1,
37 0 ▶ builder => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine _build_command_name: not called CC=1 SCAR=6.9
38 0 my ( $self ) = @_;
39  
40 0 return lc( (split( /:/, $self->command ))[-1] );
41 }
42 2 );
43  
44 16 ▶sub new_from_command_line {
Subroutine new_from_command_line: called CC=22 SCAR=31.4
Pod undocumented
45 16 my ( $class, @command_line ) = @_;
46  
47 16 my $data;
48  
49 16 my ( $overrides, $command, @arguments, $switches );
50  
51 # Process over rides
52 16 ▶ while ( @command_line && substr($command_line[0],0,1) eq '/' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@command_line and substr($command_line[0], 0, 1) eq "/") 38 16 54
Condition100%3/3
@command_line and substr($command_line[0], 0, 1) eq "/"
!ll&&!rl&&r
61038
MC/DC100%2/2
@command_line and substr($command_line[0], 0, 1) eq "/"
@command_line substr($command_line[0], 0, 1) eq "/"
Truth table100%3/3
A and B
A= @command_lineB= substr($command_line[0], 0, 1) eq "/"
ABresult
0X0
100
111
53 38 my $command = shift @command_line;
54  
55 38 ▶ if ( $command =~ /^\/([^= ]+)=(.+)$/ ) { # /key=value
Branch: 2 branches
BranchTrueFalseTotal
if ($command =~ m[^/([^= ]+)=(.+)$]) { } 28 10 38
elsif ($command =~ m[^/([^ ]+)$]) { } 10 0 10
56 28 $data->{overrides}->{$1} = $2;
57 } elsif ( $command =~ /^\/([^ ]+)$/ ) {
58 10 $data->{overrides}->{$1} = 1;
59 } else {
60 0 die "Could not parse command line argument: $command\n";
61 }
62 }
63  
64 # Process command if one exists.
65 16 ▶ $data->{command} = shift @command_line if @command_line and $command_line[0] !~ /^--/;
Branch: 1 branch
BranchTrueFalseTotal
if @command_line and not $command_line[0] =~ /^--/ 10 6 16
Condition66%2/3
@command_line and not $command_line[0] =~ /^--/
!ll&&!rl&&r
6010
MC/DC50%1/2
@command_line and not $command_line[0] =~ /^--/
@command_line not $command_line[0] =~ /^--/
Truth table66%2/3
A and not(B)
A= @command_lineB= not $command_line[0] =~ /^--/
ABresult
0X0
100
111
66  
67 16 ▶ while ( @command_line && substr($command_line[0],0,2) ne '--' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@command_line and substr($command_line[0], 0, 2) ne "--") 9 16 25
Condition100%3/3
@command_line and substr($command_line[0], 0, 2) ne "--"
!ll&&!rl&&r
6109
MC/DC100%2/2
@command_line and substr($command_line[0], 0, 2) ne "--"
@command_line substr($command_line[0], 0, 2) ne "--"
Truth table100%3/3
A and B
A= @command_lineB= substr($command_line[0], 0, 2) ne "--"
ABresult
0X0
100
111
68 9 my $command = shift @command_line;
69  
70 # Expand filenames into their content in arguments prefixed with @.
71 9 ▶ if ( substr($command,0,1) eq '@' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($command, 0, 1) eq "\@") 1 8 9
72 1 ▶ open my $lf, "<", substr($command,1)
Branch: 1 branch
BranchTrueFalseTotal
open my $lf, "<", substr($command, 1) or die "Failed to read ${command}: $!" 1 0 1
73 or die "Failed to read $command: $!";
74 1 $command = do { local $/; <$lf> };
75 1 close $lf;
76 }
77 9 push @{$data->{args}}, $command;
78 }
79  
80 # Process arguments
81 #
82 # --foo { foo => 1 }
83 # --no-foo { foo => 0 }
84 # --foo bar { foo => 'bar' }
85 # --foo bar --foo blee { foo => [ 'bar', 'blee' ] }
86 # --foo=bar { foo => 'bar' }
87 # --foo=bar --foo=blee { foo => [ 'bar', 'blee' ] }
88 # --foo @path { foo => contents_of_file(<path>) }
89 16 ▶ while ( defined( my $command = shift @command_line )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $command = shift @command_line)) 32 16 48
90  
91  
92 32 ▶ if ( $command =~ /^--no-([^ ]+)$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($command =~ /^--no-([^ ]+)$/) { } 5 27 32
elsif (!@command_line || $command_line[0] =~ /^--/ and $command =~ /^--([^ =]+)$/) { } 10 17 27
93 5 $data->{settings}->{$1} = 0;
94 ▶ } elsif ( ( ! @command_line or $command_line[0] =~ /^--/ ) && $command =~ /^--([^ =]+)$/ ) {
Condition83%5/6
!@command_line || $command_line[0] =~ /^--/ and $command =~ /^--([^ =]+)$/
!ll&&!rl&&r
17010
not @command_line or $command_line[0] =~ /^--/
l!l&&r!l&&!r
3717
MC/DC66%2/3
!@command_line || $command_line[0] =~ /^--/ and $command =~ /^--([^ =]+)$/
not @command_line $command_line[0] =~ /^--/ $command =~ /^--([^ =]+)$/
Truth table60%3/5
not(A) or B and C
A= not @command_lineB= $command_line[0] =~ /^--/C= $command =~ /^--([^ =]+)$/
ABCresult
00X0
1X00
0100
1X11
0111
95 10 $data->{settings}->{$1} = 1;
96 } else {
97 17 $command =~ s/^--//;
98  
99 17 my $argument;
100 17 ▶ if ( $command =~ /^([^=]+)=(.+?)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command =~ /^([^=]+)=(.+?)$/) { } 0 17 17
101 0 ( $command, $argument ) = ( $1, $2 );
102 } else {
103 17 $argument = shift @command_line;
104 }
105  
106 17 ▶ if ( substr($argument,0,1) eq '@' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($argument, 0, 1) eq '@') { } 2 15 17
107 2 ▶ open my $lf, "<", substr($argument,1)
Branch: 1 branch
BranchTrueFalseTotal
open my $lf, "<", substr($argument, 1) or die "Failed to read ${argument}: $!" 2 0 2
108 or die "Failed to read $argument: $!";
109 2 $data->{settings}->{$command} = do { local $/; <$lf> };
110 2 close $lf;
111 } else {
112 # If we have nothing, this becomes a string...
113 15 ▶ if ( ! $data->{settings}->{$command} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $data->{'settings'}{$command}) { } 9 6 15
elsif (ref $data->{'settings'}{$command} eq 'ARRAY') { } 0 6 6
114 9 $data->{settings}->{$command} = $argument;
115 # If we have an array ref there already...
116 } elsif ( ref($data->{settings}->{$command}) eq 'ARRAY' ) {
117 0 push @{$data->{settings}->{$command}}, $argument;
118 # Otherwise we promote to an array ref
119 } else {
120 6 $data->{settings}->{$command} = [ $data->{settings}->{$command}, $argument ];
121 }
122 }
123 }
124 }
125  
126 # Process STDIN
127 16 ▶ if ( ! -t STDIN ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-t STDIN) 16 0 16
128 16 $data->{stdin} = do { local $/; <STDIN> };
129 }
130  
131 16 return $class->new($data);
132 }
133  
134 2 1;
« oCLI/Context.pm ↑ Summary oCLI/Plugin.pm »