| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
@command_line and substr($command_line[0], 0, 1) eq "/"
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 "/"
| |||||||||||||||||||||||||||||||||||||||
| 53 | 38 | my $command = shift @command_line; | |||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||
| 55 | 38 | ▶ | if ( $command =~ /^\/([^= ]+)=(.+)$/ ) { # /key=value | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
@command_line and not $command_line[0] =~ /^--/
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] =~ /^--/
| |||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||
| 67 | 16 | ▶ | while ( @command_line && substr($command_line[0],0,2) ne '--' ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@command_line and substr($command_line[0], 0, 2) ne "--"
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 "--"
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 72 | 1 | ▶ | open my $lf, "<", substr($command,1) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||
| 92 | 32 | ▶ | if ( $command =~ /^--no-([^ ]+)$/ ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 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 =~ /^--([^ =]+)$/
not @command_line or $command_line[0] =~ /^--/
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 =~ /^--([^ =]+)$/
| |||||||||||||||||||||||||||||||||||||||
| 95 | 10 | $data->{settings}->{$1} = 1; | |||||||||||||||||||||||||||||||||||||
| 96 | } else { | ||||||||||||||||||||||||||||||||||||||
| 97 | 17 | $command =~ s/^--//; | |||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||
| 99 | 17 | my $argument; | |||||||||||||||||||||||||||||||||||||
| 100 | 17 | ▶ | if ( $command =~ /^([^=]+)=(.+?)$/ ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 107 | 2 | ▶ | open my $lf, "<", substr($argument,1) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 128 | 16 | $data->{stdin} = do { local $/; <STDIN> }; | |||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||
| 131 | 16 | return $class->new($data); | |||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||
| 134 | 2 | 1; | |||||||||||||||||||||||||||||||||||||