blib/lib/Template/Preprocessor/TTML/CmdLineProc.pm

filter: Statementstmt 96.7% 119 / 123 Branchbran 88.2% 30 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 25 / 25 Podpod 100% 2 / 2 totaltotal 95.6% 176 / 184 SCAR 28.9
CC
18
Cov
95%
get_result
6.0
_handle_long_option
5.0
_handle_short_option
4.1
CRAP 18.0
↑ Summary Build »
1 package Template::Preprocessor::TTML::CmdLineProc;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 6 ▶use base 'Template::Preprocessor::TTML::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 package Template::Preprocessor::TTML::CmdLineProc::Results;
10  
11 6 ▶use base 'Template::Preprocessor::TTML::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 __PACKAGE__->mk_accessors(qw(
14 defines
15 include_files
16 include_path
17 input_filename
18 output_to_stdout
19 output_filename
20 run_mode
21 ));
22  
23 sub initialize
24 {
25 37 ▶ my $self = shift;
Subroutine initialize: called CC=1 SCAR=0.0
26 37 $self->output_to_stdout(1);
27 37 $self->include_path([]);
28 37 $self->defines(+{});
29 37 $self->include_files([]);
30 37 $self->run_mode("regular");
31 37 return 0;
32 }
33  
34 sub add_to_inc
35 {
36 14 ▶ my $self = shift;
Subroutine add_to_inc: called CC=1 SCAR=0.0
37 14 my $path = shift;
38 14 push @{$self->include_path()}, $path;
39 }
40  
41 sub add_to_defs
42 {
43 19 ▶ my ($self, $k, $v) = @_;
Subroutine add_to_defs: called CC=1 SCAR=0.0
44 19 $self->defines()->{$k} = $v;
45 }
46  
47 sub add_include_file
48 {
49 4 ▶ my $self = shift;
Subroutine add_include_file: called CC=1 SCAR=0.0
50 4 my $path = shift;
51 4 push @{$self->include_files()}, $path;
52 }
53  
54 package Template::Preprocessor::TTML::CmdLineProc;
55  
56 6 __PACKAGE__->mk_accessors(qw(
57 argv
58 result
59 ));
60  
61  
62 sub initialize
63 {
64 37 ▶ my $self = shift;
Subroutine initialize: called CC=1 SCAR=0.0
Pod documented
65 37 my (%args) = @_;
66 37 $self->argv($args{argv});
67  
68 37 $self->result(Template::Preprocessor::TTML::CmdLineProc::Results->new());
69 37 return 0;
70 }
71  
72 sub _get_next_arg
73 {
74 122 ▶ my $self = shift;
Subroutine _get_next_arg: called CC=1 SCAR=0.0
75 122 return shift(@{$self->argv()});
76 }
77  
78 sub _no_args_left
79 {
80 85 ▶ my $self = shift;
Subroutine _no_args_left: called CC=1 SCAR=0.0
81 85 return (@{$self->argv()} == 0);
82 }
83  
84 sub _get_run_mode_opts_map
85 {
86 return
87 {
88 65 ▶ "--version" => "version",
Subroutine _get_run_mode_opts_map: called CC=1 SCAR=0.0
89 "-V" => "version",
90 "--help" => "help",
91 "-h" => "help",
92 };
93 }
94  
95 sub _get_arged_longs_opts_map
96 {
97 return
98 {
99 18 ▶ "include" => "_process_include_path_opt",
Subroutine _get_arged_longs_opts_map: called CC=1 SCAR=0.0
100 "includefile" => "_process_add_includefile_opt",
101 "define" => "_process_define_opt",
102 };
103 }
104  
105 sub _handle_middle_run_mode_opt
106 {
107 49 ▶ my ($self, $arg) = @_;
Subroutine _handle_middle_run_mode_opt: called CC=2 SCAR=6.9
108 49 ▶ if (exists($self->_get_run_mode_opts_map()->{$arg}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->_get_run_mode_opts_map->{$arg}) 4 45 49
109 {
110 4 die "Option \"$arg\" was specified in the middle of the command line. It should be a standalone option.";
111 }
112 }
113  
114 sub _handle_long_option
115 {
116 21 ▶ my $self = shift;
Subroutine _handle_long_option: called CC=5 SCAR=16.1
117 21 my $arg_orig = shift;
118 21 ▶ if ($arg_orig eq "--")
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_orig eq "--") 1 20 21
119 {
120 1 return $self->_handle_no_more_options();
121 }
122 20 $self->_handle_middle_run_mode_opt($arg_orig);
123 18 my $arg = $arg_orig;
124 18 $arg =~ s!^--!!;
125 18 my $map = $self->_get_arged_longs_opts_map();
126 18 $arg =~ m{^([^=]*)};
127 18 my $option = $1;
128 18 ▶ if (exists($map->{$option}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $map->{$option}) 17 1 18
129 {
130 17 my $sub = $self->can($map->{$option});
131 17 ▶ if (length($arg) eq length($option))
Branch: 1 branch
BranchTrueFalseTotal
if (length $arg eq length $option) { } 10 7 17
132 {
133 10 ▶ if ($self->_no_args_left())
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_no_args_left) 0 10 10
134 {
135 0 die "An argument should be specified after \"$arg_orig\"";
136 }
137 10 return $sub->(
138 $self, $self->_get_next_arg()
139 );
140 }
141 else
142 {
143 7 return $sub->(
144 $self, substr($arg, length($option)+1)
145 );
146 }
147 }
148 1 die "Unknown option!";
149 }
150  
151 sub _handle_no_more_options
152 {
153 1 ▶ my $self = shift;
Subroutine _handle_no_more_options: called CC=1 SCAR=0.0
154 1 $self->_assign_filename($self->_get_next_arg());
155 }
156  
157 sub _get_arged_short_opts_map
158 {
159 return
160 {
161 27 ▶ "o" => "_process_output_short_opt",
Subroutine _get_arged_short_opts_map: called CC=1 SCAR=0.0
162 "I" => "_process_include_path_opt",
163 "D" => "_process_define_opt",
164 };
165 }
166  
167 sub _handle_short_option
168 {
169 29 ▶ my $self = shift;
Subroutine _handle_short_option: called CC=4 SCAR=14.1
170 29 my $arg_orig = shift;
171  
172 29 $self->_handle_middle_run_mode_opt($arg_orig);
173  
174 27 my $arg = $arg_orig;
175 27 $arg =~ s!^-!!;
176 27 my $map = $self->_get_arged_short_opts_map();
177 27 my $first_char = substr($arg, 0, 1);
178 27 ▶ if (exists($map->{$first_char}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $map->{$first_char}) 27 0 27
179 {
180 27 my $sub = $self->can($map->{$first_char});
181 27 ▶ if (length($arg) > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (length $arg > 1) { } 14 13 27
182 {
183 14 return $sub->(
184 $self, substr($arg, 1)
185 );
186 }
187 else
188 {
189 13 ▶ if ($self->_no_args_left())
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_no_args_left) 0 13 13
190 {
191 0 die "An argument should be specified after \"$arg_orig\"";
192 }
193 13 return $sub->(
194 $self, $self->_get_next_arg()
195 );
196 }
197 }
198 0 die "Unknown option \"$arg_orig\"!";
199 }
200  
201 sub _process_output_short_opt
202 {
203 7 ▶ my $self = shift;
Subroutine _process_output_short_opt: called CC=1 SCAR=0.0
204 7 my $filename = shift;
205 7 $self->result()->output_to_stdout(0);
206 7 $self->result()->output_filename($filename);
207 }
208  
209 sub _process_include_path_opt
210 {
211 14 ▶ my $self = shift;
Subroutine _process_include_path_opt: called CC=1 SCAR=0.0
212 14 my $path = shift;
213 14 $self->result()->add_to_inc($path);
214 }
215  
216 sub _process_add_includefile_opt
217 {
218 4 ▶ my $self = shift;
Subroutine _process_add_includefile_opt: called CC=1 SCAR=0.0
219 4 my $file = shift;
220 4 $self->result()->add_include_file($file);
221 }
222  
223 sub _process_define_opt
224 {
225 19 ▶ my $self = shift;
Subroutine _process_define_opt: called CC=2 SCAR=7.2
226 19 my $def = shift;
227 19 ▶ if ($def !~ m{^([^=]+)=(.*)$})
Branch: 1 branch
BranchTrueFalseTotal
unless ($def =~ /^([^=]+)=(.*)$/) 0 19 19
228 {
229 0 die "Variable definition should contain a \"=\", but instead it is \"$def\"!";
230 }
231 19 my ($var, $value) = ($1, $2);
232 19 $self->result()->add_to_defs($var, $value);
233 }
234  
235 sub _assign_filename
236 {
237 25 ▶ my ($self, $arg) = @_;
Subroutine _assign_filename: called CC=2 SCAR=6.9
238 25 ▶ if (! $self->_no_args_left())
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_no_args_left) { } 1 24 25
239 {
240 1 die "Junk after filename";
241 }
242 else
243 {
244 24 $self->result()->input_filename($arg);
245 }
246 }
247  
248 sub _handle_exclusive_run_mode_opt
249 {
250 36 ▶ my $self = shift;
Subroutine _handle_exclusive_run_mode_opt: called CC=3 SCAR=11.0
251 36 ▶ if ((@{$self->argv()} == 1))
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->argv;} == 1) 10 26 36
252 {
253 10 my $opt = $self->argv()->[0];
254 10 ▶ if (exists($self->_get_run_mode_opts_map()->{$opt}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->_get_run_mode_opts_map->{$opt}) 6 4 10
255 {
256 $self->result()->run_mode(
257 6 $self->_get_run_mode_opts_map()->{$opt}
258 );
259 6 return 1;
260 }
261 }
262 30 return 0;
263 }
264  
265  
266 sub get_result
267 {
268 37 ▶ my $self = shift;
Subroutine get_result: called CC=6 SCAR=17.9
Pod documented
269  
270 37 ▶ if ($self->_no_args_left())
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_no_args_left) 1 36 37
271 {
272 1 die "Incorrect usage: you need to specify a filename";
273 }
274  
275 36 ▶ if (! $self->_handle_exclusive_run_mode_opt())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_handle_exclusive_run_mode_opt) 30 6 36
276 {
277 30 ▶ while (defined(my $arg = $self->_get_next_arg()))
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $arg = $self->_get_next_arg)) 74 24 98
278 {
279 74 ▶ if ($arg =~ m{^-})
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^-/) { } 50 24 74
280 {
281 50 ▶ if ($arg =~ m{^--})
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^--/) { } 21 29 50
282 {
283 21 $self->_handle_long_option($arg);
284 }
285 else
286 {
287 29 $self->_handle_short_option($arg);
288 }
289 }
290 else
291 {
292 24 $self->_assign_filename($arg);
293 }
294 }
295 }
296 30 return $self->result();
297 }
298  
299 6 1;
300  
301 __END__
↑ Summary Build »