Crane/Options.pm

filter: Statementstmt 85.9% 55 / 64 Branchbran 67.8% 19 / 28 Conditioncond 68.1% 15 / 22 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 76.6% 102 / 133 CC 24 SCAR 32.9
CC
24
Cov
78%
load_options
33.6
options
2.0
BEGIN
1.0
CRAP 26.9
« Crane.pm ↑ Summary Crane/Config.pm »
1 # -*- coding: utf-8 -*-
2  
3  
4 package Crane::Options;
5  
6  
7 3 ▶use Crane::Base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use File::Basename qw( basename );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Getopt::Long qw( GetOptionsFromArray :config posix_default );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 3 our @EXPORT = qw(
14 &options
15 &args
16 );
17  
18 3 our @EXPORT_OPTS = qw(
19 $OPT_SEPARATOR
20
21 $OPT_VERSION
22 $OPT_HELP
23 );
24  
25 3 our @EXPORT_OK = (
26 qw(
27 &load_options
28 ),
29
30 @EXPORT_OPTS,
31 );
32  
33 3 our %EXPORT_TAGS = (
34 'opts' => \@EXPORT_OPTS,
35 );
36  
37  
38 3 Readonly::Scalar(our $OPT_SEPARATOR => []);
39 3 Readonly::Scalar(our $OPT_VERSION => [ 'version!', 'Shows version information and exits.' ]);
40 3 Readonly::Scalar(our $OPT_HELP => [ 'help!', 'Shows this help and exits.' ]);
41  
42  
43 17 ▶sub options {
Subroutine options: called CC=2 SCAR=6.9
44
45 17 return state $options = do {
46 2 ▶ load_options(scalar @_ ? @_ : ( $OPT_VERSION, $OPT_HELP ));
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 1 1 2
47 };
48
49 }
50  
51  
52 1 ▶sub args {
Subroutine args: called CC=1 SCAR=0.0
53
54 1 return state $args = [ @ARGV ];
55
56 }
57  
58  
59 5 ▶sub load_options {
Subroutine load_options: called CC=23 SCAR=35.1
Pod documented
60
61 5 my @options = grep { ref $_ eq 'ARRAY' } @_;
62 5 my $options = {};
63
64 {
65 5 local $WARNING = 0;
66
67 # Parse command line
68 5 GetOptionsFromArray(\@ARGV, $options, grep { defined } map { $_->[0] } @options);
69 }
70
71 # Application file name
72 5 my $app = basename($PROGRAM_NAME);
73
74 # Show version information and exit
75 5 ▶ if ( $options->{'version'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"version"}) 0 5 5
76 0 ▶ my $version = $main::VERSION // 'not specified';
Condition0%0/2
$main::VERSION // "not specified"
l!l
00
MC/DC0%0/1
$main::VERSION // "not specified"
$main::VERSION
Truth table0%0/2
A
A= $main::VERSION
Aresult
11
00
77
78 0 ▶ print { *STDOUT } "$app version is $version\n" or confess($OS_ERROR);
Branch: 1 branch
BranchTrueFalseTotal
print {*STDOUT;} "$app version is $version\n" or confess($Crane::Options::OS_ERROR) 0 0 0
79
80 0 exit 0;
81 }
82
83 # Create help ...
84 5 my $help = "$app <options> <args>\n\n";
85
86 5 ▶ if ( scalar @options ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @options) 5 0 5
87 5 $help .= "OPTIONS:\n";
88 }
89
90 # ... and check options
91 5 foreach my $opt ( @options ) {
92 28 ▶ if ( ref $opt ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt ne "ARRAY") 0 28 28
93 0 next;
94 }
95
96 28 my $spec = $opt->[0];
97 28 my $desc = $opt->[1];
98 28 my $params = $opt->[2];
99
100 # Separator
101 28 ▶ if ( not defined $spec and not defined $desc and not defined $params ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $spec || defined $desc || defined $params) { } 5 23 28
elsif (defined $spec and $spec =~ /^([^!+=:]+)/isu) { } 23 0 23
Condition66%4/6
defined $spec || defined $desc || defined $params
l!l&&r!l&&!r
2305
defined $spec or defined $desc
l!l&&r!l&&!r
2305
MC/DC33%1/3
defined $spec || defined $desc || defined $params
defined $spec defined $desc defined $params
Truth table50%2/4
A or B || C
A= defined $specB= defined $descC= defined $params
ABCresult
1XX1
01X1
0011
0000
102 5 $help .= "\n";
103 # Option
104 ▶ } elsif ( defined $spec and $spec =~ m{^([^!+=:]+)}si ) {
Condition33%1/3
defined $spec and $spec =~ /^([^!+=:]+)/isu
!ll&&!rl&&r
0023
MC/DC0%0/2
defined $spec and $spec =~ /^([^!+=:]+)/isu
defined $spec $spec =~ /^([^!+=:]+)/isu
Truth table33%1/3
A and B
A= defined $specB= $spec =~ /^([^!+=:]+)/isu
ABresult
0X0
100
111
105 23 my @names = split m{[|]}si, $1;
106 23 my $name = $names[0];
107 23 my $short = ( grep { length == 1 } @names )[0];
108 23 my $long = ( grep { length >= 2 } @names )[0];
109
110 # Check params
111 23 ▶ if ( ref $params eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params eq "HASH") 6 17 23
112 # Default value
113 6 ▶ if ( exists $params->{'default'} and not exists $options->{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"default"} and not exists $options->{$name}) 3 3 6
Condition100%3/3
exists $params->{"default"} and not exists $options->{$name}
!ll&&!rl&&r
213
MC/DC100%2/2
exists $params->{"default"} and not exists $options->{$name}
exists $params->{"default"} not exists $options->{$name}
Truth table100%3/3
A and not(B)
A= exists $params->{"default"}B= not exists $options->{$name}
ABresult
0X0
100
111
114 3 $options->{ $name } = $params->{'default'};
115 }
116
117 # Is required
118 6 ▶ if ( $params->{'required'} and not $options->{'help'} and not exists $options->{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"required"} and not $options->{"help"} and not exists $options->{$name}) 1 5 6
Condition83%5/6
$params->{"required"} and not $options->{"help"} and not exists $options->{$name}
!ll&&!rl&&r
411
$params->{"required"} and not $options->{"help"}
!ll&&!rl&&r
402
MC/DC66%2/3
$params->{"required"} and not $options->{"help"} and not exists $options->{$name}
$params->{"required"} not $options->{"help"} not exists $options->{$name}
Truth table75%3/4
A and not(B) and not(C)
A= $params->{"required"}B= not $options->{"help"}C= not exists $options->{$name}
ABCresult
0XX0
10X0
1100
1111
119 1 die "Option required: $name\n";
120 }
121 }
122
123 # Add to help
124 22 ▶ $help .= sprintf q{ %-2s %-20s %s},
Branch: 2 branches
BranchTrueFalseTotal
defined $short ? : 10 12 22
defined $long ? : 20 2 22
Condition100%2/2
$desc // ""
l!l
202
MC/DC100%1/1
$desc // ""
$desc
Truth table100%2/2
A
A= $desc
Aresult
11
00
125 defined $short ? "-$short" : '',
126 defined $long ? "--$long" : '',
127
128 $desc // '';
129
130 22 $help .= "\n";
131 } else {
132 0 confess("Invalid option specification: $spec");
133 }
134 }
135
136 # Show help and exit
137 4 ▶ if ( $options->{'help'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"help"}) 0 4 4
138 0 ▶ print { *STDOUT } $help or confess($OS_ERROR);
Branch: 1 branch
BranchTrueFalseTotal
print {*STDOUT;} $help or confess($Crane::Options::OS_ERROR) 0 0 0
139
140 0 exit 0;
141 }
142
143 4 return $options;
144
145 }
146  
147  
148 3 1;
149  
150  
151 =head1 NAME
152  
153 Crane::Options - Command line options and arguments parser
154  
155  
156 =head1 SYNOPSIS
157  
158 use Crane::Options;
159
160 my $option = options->{'version'};
161 my $arg2 = args->[1];
162  
163  
164 =head1 DESCRIPTION
165  
166 Parses command line options and arguments. Options are available as hash
167 reference returned by L<options|/"options (@options)"> function and arguments are available as array
168 reference returned by L<args|/"args ()"> function.
169  
170 You can configure options by passing list of array references when first call
171 L<options|/"options (@options)"> function (see description below).
172  
173 By default two options are available: B<version> and B<help>.
174  
175  
176 =head1 EXPORTED CONSTANTS
177  
178 =head2 Tag :opts - predefined options
179  
180 =over
181  
182 =item B<$OPT_SEPARATOR>
183  
184 Not an option exaclty, just a separator in help output.
185  
186 Equals to:
187  
188 []
189  
190 =item B<$OPT_VERSION>
191  
192 Version information output.
193  
194 Equals to:
195  
196 [ 'version!', 'Shows version information and exits.' ]
197  
198 =item B<$OPT_HELP>
199  
200 Help output.
201  
202 Equals to:
203  
204 [ 'help!', Shows this help and exits.' ]
205  
206 =back
207  
208  
209 =head1 EXPORTED FUNCTIONS
210  
211 =over
212  
213 =item B<options> (I<@options>)
214  
215 Returns hash reference to the command line options.
216  
217 Can be configured when first called with the list of I<@options>. To create an
218 option you should pass a list of array references with one required and two
219 mandatory items:
220  
221 =over
222  
223 =item B<Specification>
224  
225 Scalar, required. Specification from L<Getopt::Long|Getopt::Long> module.
226  
227 =item B<Description>
228  
229 Scalar. Text description (what is this option does?).
230  
231 =item B<Parameters>
232  
233 Hash reference. Additional parameters:
234  
235 =over
236  
237 =item B<default>
238  
239 Default value for option if option does not exist.
240  
241 =item B<required>
242  
243 Flag that option should be exists.
244  
245 =back
246  
247 =back
248  
249 Separator is empty array reference.
250  
251 =item B<args> ()
252  
253 Returns array reference to command line arguments.
254  
255 =back
256  
257  
258 =head1 FUNCTIONS
259  
260 =over
261  
262 =item B<load_options> (I<@options>)
263  
264 Parses command line arguments list I<@ARGV> and return reference to hash.
265  
266 See L<@options parameter description|/"options (@options)">.
267  
268 =back
269  
270  
271 =head1 ERRORS
272  
273 =over
274  
275 =item Invalid option specification: I<%s>
276  
277 Where I<%s> is specification string.
278  
279 Fires when required parameter of specification is not defined or incorrect.
280  
281 =back
282  
283  
284 =head1 DIAGNOSTICS
285  
286 =over
287  
288 =item Option required: I<%s>
289  
290 Where I<%s> is an option name.
291  
292 Option does not exist but required.
293  
294 =back
295  
296  
297 =head1 EXAMPLES
298  
299  
300 =head2 Simple option in compare with defaults
301  
302 Configuration:
303  
304 options(
305 [ 'config|C=s', 'Path to configuration file.' ],
306 $OPT_SEPARATOR,
307 $OPT_VERSION,
308 $OPT_HELP,
309 );
310  
311 Help output:
312  
313 example.pl <options> <args>
314 -C --config Path to configuration file.
315
316 --version Shows version information and exists.
317 -? --help Shows this help and exits.
318  
319  
320 =head2 Two required arguments, one with default value and default options
321  
322 Configuration:
323  
324 options(
325 [ 'daemon|M!', 'Run as daemon.', { 'default' => 1 } ],
326 $OPT_SEPARATOR,
327 [ 'from=s', 'Start of the interval.', { 'required' => 1 } ],
328 [ 'to=s', 'End of the interval.', { 'required' => 1 } ],
329 $OPT_SEPARATOR,
330 $OPT_VERSION,
331 $OPT_HELP,
332 );
333  
334 Help output:
335  
336 example.pl <options> <args>
337 -M --daemon Run as daemon.
338
339 --from Start of the interval.
340 --to End of the interval.
341
342 --version Shows version information and exists.
343 -? --help Shows this help and exits.
344  
345  
346 =head1 BUGS
347  
348 Please report any bugs or feature requests to
349 L<https://rt.cpan.org/Public/Bug/Report.html?Queue=Crane> or to
350 L<https://github.com/temoon/crane/issues>.
351  
352  
353 =head1 AUTHOR
354  
355 Tema Novikov, <novikov.tema@gmail.com>
356  
357  
358 =head1 COPYRIGHT AND LICENSE
359  
360 Copyright (C) 2013-2014 Tema Novikov.
361  
362 This library is free software; you can redistribute it and/or modify it under
363 the terms of the Artistic License 2.0. For details, see the full text of the
364 license in the file LICENSE.
365  
366  
367 =head1 SEE ALSO
368  
369 =over
370  
371 =item * B<RT Cpan>
372  
373 L<https://rt.cpan.org/Public/Dist/Display.html?Name=Crane>
374  
375 =item * B<Github>
376  
377 L<https://github.com/temoon/crane>
378  
379 =back
« Crane.pm ↑ Summary Crane/Config.pm »