blib/lib/Rex/Args.pm

filter: Statementstmt 84.1% 90 / 107 Branchbran 65.3% 17 / 26 Conditioncond 57.1% 12 / 21 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 5 totaltotal 74.7% 142 / 190 CC 24 SCAR 25.2
CC
24
Cov
77%
parse_rex_opts
24.4
_read_old_way
6.5
is_opt
2.3
CRAP 12.4
« lib/Rex/Service/Base.pm ↑ Summary lib/Rex/Box/KVM.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Args;
6  
7 109 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 109 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 109 our $VERSION = '1.16.1'; # VERSION
11  
12 109 ▶use vars qw(%rex_opts);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 109 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 109 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 109 our $CLEANUP = 1;
17  
18 1 ▶sub args_spec {
Subroutine args_spec: called CC=1 SCAR=0.0
Pod undocumented
19 return (
20 1 a => {},
21 C => {},
22 c => {},
23 q => {},
24 Q => {},
25 F => {},
26 T => {},
27 h => {},
28 v => {},
29 d => {},
30 s => {},
31 m => {},
32 y => {},
33 w => {},
34 S => { type => "string" },
35 E => { type => "string" },
36 o => { type => "string" },
37 f => { type => "string" },
38 M => { type => "string" },
39 b => { type => "string" },
40 e => { type => "string" },
41 H => { type => "string" },
42 u => { type => "string" },
43 p => { type => "string" },
44 P => { type => "string" },
45 K => { type => "string" },
46 G => { type => "string" },
47 g => { type => "string" },
48 z => { type => "string" },
49 O => { type => "string" },
50 t => { type => "string" },
51 );
52 }
53  
54 1 ▶sub parse_rex_opts {
Subroutine parse_rex_opts: called CC=20 SCAR=32.0
Pod undocumented
55 1 my ($class) = @_;
56  
57 1 my %args = $class->args_spec;
58  
59 #### clean up @ARGV
60 1 my $runner = 0;
61 1 for (@ARGV) {
62 17 ▶ if ( /^\-[A-Za-z]+/ && length($_) > 2 && $CLEANUP ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\-[A-Za-z]+/u and length $_ > 2 and $Rex::Args::CLEANUP) 1 16 17
Condition83%5/6
/^\-[A-Za-z]+/u and length $_ > 2 and $Rex::Args::CLEANUP
!ll&&!rl&&r
1601
/^\-[A-Za-z]+/u and length $_ > 2
!ll&&!rl&&r
881
MC/DC66%2/3
/^\-[A-Za-z]+/u and length $_ > 2 and $Rex::Args::CLEANUP
/^\-[A-Za-z]+/u length $_ > 2 $Rex::Args::CLEANUP
Truth table75%3/4
A and B and C
A= /^\-[A-Za-z]+/uB= length $_ > 2C= $Rex::Args::CLEANUP
ABCresult
0XX0
10X0
1100
1111
63 1 my @args = map { "-$_" } split( //, substr( $_, 1 ) );
64 1 splice( @ARGV, $runner, 1, @args );
65 }
66  
67 17 $runner++;
68 }
69  
70 #### parse rex options
71 1 my @params = @ARGV;
72 1 for my $p (@params) {
73  
74 # shift off @ARGV
75 10 my $shift = shift @ARGV;
76  
77 10 ▶ if ( length($p) >= 2 && substr( $p, 0, 1 ) eq "-" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $p >= 2 and substr($p, 0, 1) eq '-') { } 9 1 10
Condition66%2/3
length $p >= 2 and substr($p, 0, 1) eq "-"
!ll&&!rl&&r
019
MC/DC50%1/2
length $p >= 2 and substr($p, 0, 1) eq "-"
length $p >= 2 substr($p, 0, 1) eq "-"
Truth table66%2/3
A and B
A= length $p >= 2B= substr($p, 0, 1) eq "-"
ABresult
0X0
100
111
78 9 my $name_param = substr( $p, 1, 2 );
79  
80 # found a parameter
81  
82 9 ▶ if ( exists $args{$name_param} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{$name_param}) { } 9 0 9
83 9 Rex::Logger::debug("Option found: $name_param ($p)");
84 9 my $type = "Single";
85  
86 9 ▶ if ( exists $args{$name_param}->{type} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{$name_param}{'type'}) { } 5 4 9
elsif (exists $args{$name_param}{'func'}) { } 0 4 4
87 5 $type = $args{$name_param}->{type};
88  
89 5 Rex::Logger::debug(" is a $type");
90 5 shift @params; # remove the next parameter, because it must be an option
91  
92 5 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $ARGV[0] or length $ARGV[0] == 2 and exists $args{substr $ARGV[0], 1, 2} and substr($ARGV[0], 0, 1) eq "-") 0 5 5
Condition33%3/9
not exists $ARGV[0] or length $ARGV[0] == 2 and exists $args{substr $ARGV[0], 1, 2} and substr($ARGV[0], 0, 1) eq "-"
l!l&&r!l&&!r
005
length $ARGV[0] == 2 and exists $args{substr $ARGV[0], 1, 2} and substr($ARGV[0], 0, 1) eq "-"
!ll&&!rl&&r
500
length $ARGV[0] == 2 and exists $args{substr $ARGV[0], 1, 2}
!ll&&!rl&&r
500
MC/DC0%0/4
not exists $ARGV[0] or length $ARGV[0] == 2 and exists $args{substr $ARGV[0], 1, 2} and substr($ARGV[0], 0, 1) eq "-"
not exists $ARGV[0] length $ARGV[0] == 2 exists $args{substr $ARGV[0], 1, 2} substr($ARGV[0], 0, 1) eq "-"
Truth table20%1/5
not(A) or B and C and D
A= not exists $ARGV[0]B= length $ARGV[0] == 2C= exists $args{substr $ARGV[0], 1, 2}D= substr($ARGV[0], 0, 1) eq "-"
ABCDresult
1XXX1
01111
00XX0
010X0
01100
93 !exists $ARGV[0]
94 || ( length( $ARGV[0] ) == 2
95 && exists $args{ substr( $ARGV[0], 1, 2 ) }
96 && substr( $ARGV[0], 0, 1 ) eq "-" )
97 )
98 {
99 # this is a typed parameter without an option!
100 0 Rex::Logger::debug(" but there is no parameter");
101 0 Rex::Logger::debug( Dumper( \@params ) );
102 0 print("No parameter for $name_param\n");
103 0 CORE::exit 1;
104 }
105 }
106 elsif ( exists $args{$name_param}->{func} ) {
107 0 Rex::Logger::debug(" is a function - executing now");
108 0 $args{$name_param}->{func}->();
109 }
110  
111 9 my $c = "Rex::Args::\u$type";
112 9 ▶ eval "use $c";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
113 9 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 9 9
114 0 die("No Argumentclass $type found!");
115 }
116  
117 9 ▶ if ( exists $rex_opts{$name_param} && $type eq "Single" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Rex::Args::rex_opts{$name_param} and $type eq 'Single') { } 0 9 9
Condition66%2/3
exists $Rex::Args::rex_opts{$name_param} and $type eq "Single"
!ll&&!rl&&r
810
MC/DC0%0/2
exists $Rex::Args::rex_opts{$name_param} and $type eq "Single"
exists $Rex::Args::rex_opts{$name_param} $type eq "Single"
Truth table66%2/3
A and B
A= exists $Rex::Args::rex_opts{$name_param}B= $type eq "Single"
ABresult
0X0
100
111
118 0 $rex_opts{$name_param}++;
119 }
120 else {
121 # multiple params defined, create an array
122 9 ▶ if ( exists $rex_opts{$name_param} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Rex::Args::rex_opts{$name_param}) { } 1 8 9
123 1 ▶ if ( !ref $rex_opts{$name_param} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $Rex::Args::rex_opts{$name_param}) 1 0 1
124 1 $rex_opts{$name_param} = [ $rex_opts{$name_param} ];
125 }
126 1 push @{ $rex_opts{$name_param} }, $c->get;
127 }
128 else {
129 8 $rex_opts{$name_param} = $c->get;
130 }
131 }
132 }
133 else {
134 0 Rex::Logger::debug("Option not known: $name_param ($p)");
135 0 next;
136 }
137 }
138 else {
139 # unshift the last parameter
140 1 unshift @ARGV, $shift;
141 1 last;
142 }
143 }
144 }
145  
146 224 ▶sub getopts { return %rex_opts; }
Subroutine getopts: called CC=1 SCAR=0.0
Pod undocumented
147  
148 65 ▶sub is_opt {
Subroutine is_opt: called CC=2 SCAR=8.1
Pod undocumented
149 65 my ( $class, $opt ) = @_;
150 65 ▶ if ( exists $rex_opts{$opt} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Rex::Args::rex_opts{$opt}) 0 65 65
151 0 return $rex_opts{$opt};
152 }
153 }
154  
155 16 ▶sub get {
Subroutine get: called CC=2 SCAR=6.9
Pod undocumented
156 16 my ($class) = @_;
157  
158 16 my $task = Rex::TaskList->create->current_task;
159 16 ▶ if ($task) {
Branch: 1 branch
BranchTrueFalseTotal
if ($task) { } 13 3 16
160 13 return $task->get_opts();
161 }
162 else {
163 3 return _read_old_way();
164 }
165 }
166  
167 3 ▶sub _read_old_way {
Subroutine _read_old_way: called CC=3 SCAR=18.7
168 #### parse task options
169 3 my %task_opts;
170  
171 3 for my $p (@ARGV) {
172 0 my ( $key, $val ) = split( /=/, $p, 2 );
173  
174 0 $key =~ s/^--//;
175  
176 0 ▶ if ( defined $val ) { $task_opts{$key} = $val; next; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 0 0
177 0 $task_opts{$key} = 1;
178 }
179  
180 3 return %task_opts;
181 }
182  
183 109 1;
« lib/Rex/Service/Base.pm ↑ Summary lib/Rex/Box/KVM.pm »