CLI.pm

filter: Statementstmt 70.7% 145 / 205 Branchbran 43.5% 34 / 78 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 90.9% 30 / 33 Podpod 0.0% 0 / 10 totaltotal 62.7% 214 / 341 CC 46 SCAR 26.2
CC
46
Cov
63%
typeStr
72.0
parse_string
14.5
parse
12.0
CRAP 13.7
« CLI/MixedArray.pm ↑ Summary CLI/Var.pm »
1 package CLI;
2  
3  
4 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶ use Exporter();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶ use vars qw(@ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 @ISA = qw(Exporter);
9 1 @EXPORT = qw(VAR HASH COMMAND ARRAY MIXED ARRAY INTEGER FLOAT STRING
10 SSTRING TIME DEGREE BOOLEAN
11 hashmatch parse_string typeStr string_value);
12 1 ▶ use Astro::Time qw(turn2str str2turn);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 }
15  
16 1 ▶use constant VAR => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use constant HASH => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use constant COMMAND => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use constant ARRAY => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use constant MIXEDARRAY => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use constant INTEGER => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use constant FLOAT => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use constant STRING => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use constant SSTRING => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use constant TIME => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use constant DEGREE => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use constant BOOLEAN => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 sub parse_string ($$);
31 sub typeStr ($);
32 sub hashmatch ($$\@);
33 sub string_value($$);
34  
35 1 ▶use CLI::Var;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use CLI::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use CLI::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use CLI::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use CLI::MixedArray;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
44 1 my $proto = shift;
45 1 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
46  
47 1 my $self = {
48 ELEMENTS => {},
49 DEFAULT => undef
50 };
51  
52 1 bless ($self, $class);
53  
54 1 return $self;
55 }
56  
57 10 ▶sub add {
Subroutine add: called CC=6 SCAR=22.8
Pod undocumented
58 10 my $self = shift;
59 10 my $type = shift;
60 10 my $name = shift;
61 10 my $elements = $self->{ELEMENTS};
62  
63 10 my $new_element;
64 10 ▶ if ($type==VAR) {
Branch: 5 branches
BranchTrueFalseTotal
if ($type == 1) { } 6 4 10
elsif ($type == 4) { } 0 4 4
elsif ($type == 5) { } 0 4 4
elsif ($type == 2) { } 0 4 4
elsif ($type == 3) { } 4 0 4
65 6 my $var = shift;
66 6 tie $$var, 'CLI::Var', $name, @_;
67 6 $new_element = tied $$var;
68 } elsif ($type==ARRAY) {
69 0 my $var = shift;
70 0 tie @{$var}, 'CLI::Array', $name, @_;
71 0 $new_element = tied @{$var};
72 } elsif ($type==MIXEDARRAY) {
73 0 my $var = shift;
74 0 tie @{$var}, 'CLI::MixedArray', $name, @_;
75 0 $new_element = tied @{$var};
76 } elsif ($type==HASH) {
77 0 $new_element = new CLI::Hash($name, @_);
78 } elsif ($type==COMMAND) {
79 4 $new_element = new CLI::Command($name, @_);
80 } else {
81 0 carp 'CLI::add Unknown element type';
82 }
83  
84 10 $elements->{$name} = $new_element;
85  
86 10 return $new_element;
87 }
88  
89 # Command to run if command is not known
90 0 ▶sub default {
Subroutine default: not called CC=2 SCAR=17.9
Pod undocumented
91 0 my $self = shift;
92 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
93 0 $self->{DEFAULT} = shift; #TODO Should check type
94 }
95 0 return $self->{DEFAULT};
96 }
97  
98 13 ▶sub parse {
Subroutine parse: called CC=6 SCAR=24.9
Pod undocumented
99 13 my $self = shift;
100 13 my $line = shift;
101  
102 13 my $elements = $self->{ELEMENTS};
103  
104 13 my ($key, $value);
105 13 ▶ if ($line =~ /^\s*(\S+) # Key
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*(\S+) # Key (?:\s+ # Optionally followed by a space (?:(.*\S))?\s*)? # and then some value $/x) { } 13 0 13
106 (?:\s+ # Optionally followed by a space
107 (?:(.*\S))?\s*)? # and then some value
108 $/x) {
109 13 $key = $1;
110 13 $value = $2;
111 } else {
112 0 return; # Ignore blank lines
113 }
114 13 my @matches = ();
115 13 my $command = hashmatch($key, $elements, @matches);
116  
117 13 ▶ if (defined $command) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $command) { } 13 0 13
118 13 $command->parse($value);
119 } else {
120 0 ▶ if (@matches) {
Branch: 1 branch
BranchTrueFalseTotal
if (@matches) { } 0 0 0
121 0 print "\"$key\" matches:\n\n";
122 0 foreach my $match (@matches) {
123 0 print " $match\n";
124 }
125 0 print "\n";
126 } else {
127 0 ▶ if (defined $self->default) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->default) { } 0 0 0
128 0 &{$self->default()}($line, $key, $value);
129 } else {
130 0 print "\n Unknown command $key\n\n";
131 }
132 }
133 }
134 }
135  
136 # Save a config file based on the current state of variables
137 # Currently does not save anything but variable types
138 1 ▶sub save_config{
Subroutine save_config: called CC=5 SCAR=17.4
Pod undocumented
139 1 my $self = shift;
140  
141 1 my $fconfig = shift;
142  
143 1 ▶ if (!defined $fconfig) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fconfig) 0 1 1
144 0 carp "CLI->save_config must supply config filename";
145 0 return;
146 }
147  
148 1 ▶ if (! open(CONFIG, '>', $fconfig)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open CONFIG, ">", $fconfig) 0 1 1
149 0 carp "Could not open $fconfig: $!";
150 0 return;
151 }
152  
153 1 my $elements = $self->{ELEMENTS};
154  
155 1 foreach (keys(%$elements)) {
156 10 my $type = ref($elements->{$_});
157 10 ▶ if ($type eq 'CLI::Var') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "CLI::Var") 6 4 10
158 6 printf(CONFIG "%s %s\n", $_, $elements->{$_}->value);
159 }
160 }
161  
162 1 close(CONFIG);
163 }
164  
165 # Read back a previously saved config file.
166 # Currently config file can contain anything that could be
167 # type on the command line
168 1 ▶sub restore_config{
Subroutine restore_config: called CC=4 SCAR=15.2
Pod undocumented
169 1 my $self = shift;
170  
171 1 my $fconfig = shift;
172  
173 1 ▶ if (!defined $fconfig) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fconfig) 0 1 1
174 0 carp "CLI->restore_config: must supply config filename";
175 0 return;
176 }
177  
178 1 ▶ if (! open(CONFIG, $fconfig)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open CONFIG, $fconfig) 0 1 1
179 0 carp "Could not open $fconfig for reading: $!";
180 0 return;
181 }
182  
183 1 my $elements = $self->{ELEMENTS};
184  
185 1 ▶ while (<CONFIG>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline CONFIG)) 6 1 7
186 6 $self->parse($_);
187 }
188  
189 1 close(CONFIG);
190 }
191  
192  
193 # Some generally useful routines
194  
195 13 ▶sub hashmatch ($$\@) {
Subroutine hashmatch: called CC=8 SCAR=23.0
Pod undocumented
196 #+
197 # Match a keyword in a hash, using case insensitive minimal matching, and
198 # returns the hash value
199 # (ie 'Fr' matches 'Fred' and 'Frank')
200 # Usage:
201 # my %hash = (
202 # Fred => 'Some value',
203 # William => 20,
204 # Mary => [0, 1, 2]
205 # );
206 # my $key = 'Fr';
207 # my @matches = ();
208 # my $match = hashmatch($key, \%hash, @matches);
209 #
210 # Returns undef if no matches found, or multiple matches found
211 # The third parameter is set to a list reference containing all the matches
212 # Wild cards are allowed: * match any number of characters
213 # ? Match one character
214 #-
215 13 my($key, $hash, $matches) = @_;
216  
217 13 @$matches = ();
218  
219 # Cannot do exact matches with wild cards
220 13 my $exactmatch = 1;
221 13 ▶ $exactmatch = 0 if (($key =~ /\?/) || ($key =~ /\*/));
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /\?/ or $key =~ /\*/ 0 13 13
Condition33%1/3
$key =~ /\?/ or $key =~ /\*/
l!l&&r!l&&!r
0013
MC/DC0%0/2
$key =~ /\?/ or $key =~ /\*/
$key =~ /\?/ $key =~ /\*/
Truth table33%1/3
A or B
A= $key =~ /\?/B= $key =~ /\*/
ABresult
1X1
011
000
222  
223 13 my @matches = ();
224 # Clean up the key
225 13 $key =~ s/\./\\\./g; # Pass '.'s (. -> \.)
226 13 $key =~ s/\+/\\\+/g; # Pass '+'s (+ -> \+)
227 13 $key =~ s/\[/\\\[/g; # Pass '['s (+ -> \[)
228 13 $key =~ s/\]/\\\]/g; # Pass ']'s (+ -> \])
229 13 $key =~ s/\(/\\\(/g; # Pass '('s (+ -> \()
230 13 $key =~ s/\)/\\\)/g; # Pass ')'s (+ -> \))
231 13 $key =~ s/\*/\.\*/g; # Allow simple wild cards ( * -> .* )
232 13 $key =~ s/\?/\./g; # ? matches single character (? -> .)
233  
234 13 foreach (keys(%$hash)) {
235 #print "Trying $_ ";
236 63 ▶ if ($exactmatch && /^$key$/i) { # Return immediately for an exact match
Branch: 2 branches
BranchTrueFalseTotal
if ($exactmatch and /^$key$/i) { } 13 50 63
elsif (/^$key/i) { } 0 50 50
Condition66%2/3
$exactmatch and /^$key$/i
!ll&&!rl&&r
05013
MC/DC50%1/2
$exactmatch and /^$key$/i
$exactmatch /^$key$/i
Truth table66%2/3
A and B
A= $exactmatchB= /^$key$/i
ABresult
0X0
100
111
237 #print " exact match to $key!\n";
238 13 @$matches = ($_);
239 13 return $$hash{$_};
240 } elsif (/^$key/i) {
241 0 push @$matches, $_;
242 #print " matches $key\n";
243 } else {
244 #print " no match\n";
245 }
246 }
247  
248 0 ▶ if (@$matches==1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$matches == 1) { } 0 0 0
249 0 return $hash->{$matches->[0]};
250 } else {
251 0 return undef;
252 }
253 };
254  
255 9 ▶sub parse_string ($$) {
Subroutine parse_string: called CC=11 SCAR=26.8
Pod undocumented
256 9 my ($type, $string) = @_;
257  
258 9 ▶ if (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) { } 9 0 9
259  
260 9 my $pattern = '';
261 9 ▶ if ($type == INTEGER) {
Branch: 6 branches
BranchTrueFalseTotal
if ($type == 1) { } 2 7 9
elsif ($type == 2) { } 4 3 7
elsif ($type == 4) { } 0 3 3
elsif ($type == 3) { } 1 2 3
elsif ($type == 5) { } 0 2 2
elsif ($type == 6) { } 2 0 2
262 2 $pattern = '[+-]?\\d+';
263 } elsif ($type == FLOAT) {
264 #$pattern = '\\d+\.\\d+'; # Way to simplistic
265 4 $pattern = '\\S+'; # Way to simplistic
266 } elsif ($type == SSTRING) {
267 0 $pattern = '\\S+';
268 } elsif ($type == STRING) {
269 1 $pattern = '\\S.*';
270 } elsif ($type == TIME) {
271 0 $pattern = '\\S+';
272 } elsif ($type == DEGREE) {
273 2 $pattern = '\\S+';
274 } else {
275 0 $pattern = '.+';
276 }
277  
278 9 ▶ if ($string =~ /^\s*($pattern)(?:\s+(.*))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^\s*($pattern)(?:\s+(.*))?$/) { } 9 0 9
279 9 $_[1] = $2; # Pass back the unused portion of the string
280  
281 # Return the matched portion, with possibly with some additional processing
282 9 ▶ if ($type == DEGREE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type == 6) { } 2 7 9
elsif ($type == 5) { } 0 7 7
283 2 return str2turn($1,'D');
284 } elsif ($type == TIME) {
285 0 return str2turn($1,'H');
286 } else {
287 7 return $1;
288 }
289 } else {
290 0 return undef;
291 }
292 } else {
293 0 return undef;
294 }
295 }
296  
297 0 ▶sub typeStr ($) {
Subroutine typeStr: not called CC=8 SCAR=42.8
Pod undocumented
298 0 my $type = shift;
299 0 ▶ if ($type == INTEGER) {
Branch: 7 branches
BranchTrueFalseTotal
if ($type == 1) { } 0 0 0
elsif ($type == 2) { } 0 0 0
elsif ($type == 3) { } 0 0 0
elsif ($type == 4) { } 0 0 0
elsif ($type == 5) { } 0 0 0
elsif ($type == 6) { } 0 0 0
elsif ($type == 7) { } 0 0 0
300 0 return 'Integer';
301 } elsif ($type == FLOAT) {
302 0 return 'Float';
303 } elsif ($type == STRING) {
304 0 return 'String';
305 } elsif ($type == SSTRING) {
306 0 return 'SString';
307 } elsif ($type == TIME) {
308 0 return 'Time';
309 } elsif ($type == DEGREE) {
310 0 return 'Degree';
311 } elsif ($type == BOOLEAN) {
312 0 return 'Boolean';
313 } else {
314 0 return 'Unknown';
315 }
316 }
317  
318 0 ▶sub string_value ($$) {
Subroutine string_value: not called CC=3 SCAR=24.8
Pod undocumented
319 0 my $value = shift;
320 0 my $type = shift;
321 0 ▶ if ($type == TIME) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type == 5) { } 0 0 0
elsif ($type == 6) { } 0 0 0
322 0 return turn2str($value, 'H', 2);
323 } elsif ($type == DEGREE) {
324 0 return turn2str($value, 'D', 2);
325 } else {
326 0 return $value;
327 }
328 }
329  
330  
331 1 1;
« CLI/MixedArray.pm ↑ Summary CLI/Var.pm »