lib/Getopt/Valid.pm

filter: Statementstmt 92.0% 161 / 175 Branchbran 78.1% 86 / 110 Conditioncond 54.5% 36 / 66 MC/DCmcdc 29.4% 10 / 34 Subroutinesub 93.7% 15 / 16 Podpod 100% 8 / 8 totaltotal 77.2% 316 / 409 CC 85 SCAR 31.1
CC
85
Cov
81%
_parse_struct
35.1
validate
31.2
errors
13.8
CRAP 22.4
↑ Summary
1 package Getopt::Valid;
2  
3 =head1 NAME
4  
5 Getopt::Valid - Extended processing and validation of command line options
6  
7 =head1 DESCRIPTION
8  
9 Implements an extended getopt mechanism relying on L<Getopt::Long> but provides extended validation and filtering capabilities.
10  
11 Useful for shell scripts.
12  
13 I wrote this, because i need input validation / processing in most of my scripts. This keeps it formal and readable while
14 not making me re-implement the wheel over and over again.
15  
16 The dependency footprint is rather small (only L<Getopt::Long>).
17  
18 =head1 SYNOPSIS
19  
20 #!/usr/bin/perl
21
22 use strict;
23 use warnings;
24 use Getopt::Valid;
25  
26 #
27 # VALIDATION DEFINITION
28 #
29  
30 my $validation_ref = {
31
32 # name of the program
33 name => 'MyScript', # fallback to $0, if not set
34
35 # version info
36 version => '1.0.1', # fallback to $main::VERSION or "unknown", if not set
37
38 # the struct of the params
39 struct => [
40
41 # extended example
42 'somestring|s=s' => {
43 description => 'The description of somestring',
44 constraint => sub { my ( $val ) = @_; return index( $val, '123' ) > -1 },
45 required => 1,
46 },
47
48 # Example using only validator and fallback to default description.
49 # This value is optional (mind: no "!")
50 'otherstring|o=s' => qr/^([a-z]+)$/, # all lowercase words
51
52 # Example of using integer key with customized description.
53 # This value is optional (mind the "!")
54 'someint|i=i!' => 'The description of someint',
55
56 # Bool value using the default description
57 'somebool|b' => undefm
58
59 # the following is implicit, prints out the usag and exists.. can be overwritten
60 #'help|h' => 'Show this help'
61 ]
62 };
63  
64 #
65 # FUNCTIONAL USAGE
66 #
67  
68 my $validated_args_ref = GetOptionsValid( $validation_ref )
69 || die "Failed to validate input\n".
70 join( "\nERRORS:\n", @Getopt::Valid::ERRORS, "\n\nUSAGE:\n", $Getopt::Valid::USAGE );
71
72 # acces the user input
73 print "Got $validated_args_ref->{ somestring }\n";
74
75
76 #
77 # OBJECT USAGE
78 #
79  
80 my $opt = Getopt::Valid->new( $validation_ref );
81  
82 # collect data from @ARGV.. you could manipulate @ARGV and run again..
83 $opt->collect_argv;
84  
85 # whether validates
86 if ( $opt->validate ) {
87
88 # acces valid input data
89 print "Got ". $opt->valid_args->{ somestring }. "\n";
90 }
91  
92 # oops, not valid
93 else {
94
95 # print errors
96 print "Oops ". $opt->errors( " ** " ). "\n";
97
98 # print usage
99 print $opt->usage();
100 }
101  
102 =head1 VALIDATOR SYNTAX
103  
104 =over
105  
106 =item * name STRING
107  
108 Name of the program. Use in help output.
109  
110 Defaults to $0
111  
112 =item * version VERSION STRING
113  
114 Version of the program. Used in help output. Tries $main::VERSION, if not set.
115  
116 Defaults to "unknown"
117  
118 =item * underscore BOOL
119  
120 Whether "-" characters in arguments shall be rewritten to "_" in the result.
121  
122 Default: 0 (disabled)
123  
124 =item * struct ARRAYREF
125  
126 Structure of the arguments. The order will be respected in the help generation.
127  
128 Can be written in 4 styles
129  
130 =over
131  
132 =item * SCALAR
133  
134 $struct_ref = [
135 'argument-name|a=s' => 'Description text for help',
136 'other-arg' => 'Description text for help'
137 ];
138  
139 =item * REGEXP
140  
141 $struct_ref = [
142 'argument-name|a=s' => qr/(bla|blub),
143 ];
144  
145 =item * CODE
146  
147 $struct_ref = [
148 'argument-name|a=s' => sub {
149 my ( $value, $name, $validator ) = @_;
150 warn "Checking '$name' = '$value'\n";
151 return $value eq 'ok';
152 }
153 ];
154  
155 =item * HASH
156  
157 $struct_ref = [
158 'argument-name|a=s' => {
159
160 # the description for the help
161 description => 'Description text for help',
162
163 # default value, if not given
164 default => 'Some Default',
165
166 # whether required (redundant for bool), default: 0
167 required => 1|0,
168
169 # constraint can be regexp or code-ref, see above
170 constraint => sub { .. },
171
172 # modify value before handing to constraint (if any)
173 prefilter => sub {
174 my ( $value, $name, $validator ) = @_;
175 return "THE NEW $value\n";
176 },
177
178 # modify value after constraint check (if any). runs only if valid (or no constraint).
179 postfilter => sub {
180 my ( $value, $name, $validator ) = @_;
181 return "THE NEW $value\n";
182 },
183
184 # trigger is called in any case, even if arg not given
185 anytrigger => sub {
186 my ( $value, $name, $validator ) = @_;
187 return ; # ignored
188 },
189
190 # trigger is called if value is defined / given (independent if validated)
191 settrigger => sub {
192 my ( $value, $name, $validator ) = @_;
193 return ; # ignored
194 },
195
196 # trigger is called if value is validated (or no validator is given)
197 oktrigger => sub {
198 my ( $value, $name, $validator ) = @_;
199 return ; # ignored
200 },
201
202 # trigger is called if value is invalid (or no validator is given)
203 oktrigger => sub {
204 my ( $value, $name, $validator ) = @_;
205 return ; # ignored
206 },
207 }
208 ];
209  
210 =back
211  
212 =back
213  
214 =cut
215  
216 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
217 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
218  
219 5 ▶use version 0.74; our $VERSION = qv( "v0.1.4" );
Subroutine BEGIN: called CC=1 SCAR=0.0
220  
221 5 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
222  
223 5 ▶use base qw/ Exporter /;
Subroutine BEGIN: called CC=1 SCAR=0.0
224 5 our @EXPORT = qw/ GetOptionsValid /;
225  
226 5 our $REQUIRED_STR = '[REQ]';
227 5 our @ERRORS;
228 5 our $ERROR;
229 5 our $USAGE = '';
230  
231 =head1 EXPORTED METHODS
232  
233 In functional context, you can access the errors via C<@Getopt::Valid::ERRORS> and the usage via C<$Getopt::Valid::USAGE>
234  
235 =head2 GetOptionsValid $validator_ref
236  
237 See L</VALIDATOR SYNTAX>
238  
239 =cut
240  
241 2 ▶sub GetOptionsValid {
Subroutine GetOptionsValid: called CC=2 SCAR=6.9
Pod documented
242 2 my ( $args_ref ) = @_;
243 2 my $self = Getopt::Valid->new( $args_ref );
244 2 $self->collect_argv;
245 2 $USAGE = $self->usage();
246 2 ▶ if ( $self->validate() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->validate) 1 1 2
247 1 return $self->valid_args;
248 }
249 1 return 0;
250 }
251  
252 =head1 CLASS METHODS
253  
254 =head2 new $validator_ref
255  
256 Constructor. See L</VALIDATOR SYNTAX>
257  
258 =cut
259  
260 17 ▶sub new {
Subroutine new: called CC=6 SCAR=22.8
Pod documented
261 17 my ( $class, $args_ref ) = @_;
262 17 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 17 17
263 die "Usage: Getopt::Valid->new( { name => .., version => .., struct => [ .. ] } )"
264 17 ▶ unless $args_ref && ref( $args_ref ) eq 'HASH' && $args_ref->{ struct } && ref( $args_ref->{ struct } ) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless $args_ref and ref $args_ref eq "HASH" and $args_ref->{"struct"} and ref $args_ref->{"struct"} eq "ARRAY" 0 17 17
Condition33%3/9
$args_ref and ref $args_ref eq "HASH" and $args_ref->{"struct"} and ref $args_ref->{"struct"} eq "ARRAY"
!ll&&!rl&&r
0017
$args_ref and ref $args_ref eq "HASH" and $args_ref->{"struct"}
!ll&&!rl&&r
0017
$args_ref and ref $args_ref eq "HASH"
!ll&&!rl&&r
0017
MC/DC0%0/4
$args_ref and ref $args_ref eq "HASH" and $args_ref->{"struct"} and ref $args_ref->{"struct"} eq "ARRAY"
$args_ref ref $args_ref eq "HASH" $args_ref->{"struct"} ref $args_ref->{"struct"} eq "ARRAY"
Truth table20%1/5
A and B and C and D
A= $args_refB= ref $args_ref eq "HASH"C= $args_ref->{"struct"}D= ref $args_ref->{"struct"} eq "ARRAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
265 17 ( bless {
266 %$args_ref,
267 collected_args => {},
268 updated_struct => {},
269 simple_struct => {},
270 order_struct => []
271 }, $class )->_parse_struct;
272 }
273  
274 17 ▶sub _parse_struct {
Subroutine _parse_struct: called CC=31 SCAR=35.6
275 17 my ( $self ) = @_;
276
277 17 my ( %seen_short, %simple_struct, %updated_struct, @order_pre, @order_struct );
278 17 my @struct = @{ $self->{ struct } };
279 17 ▶ for ( my $i = 0; $i < @struct; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @struct) 42 17 59
280 42 push @order_pre, $struct[ $i ];
281 }
282
283 17 my %struct = @struct;
284 17 foreach my $key( @order_pre ) {
285 42 my $kstruct = $struct{ $key };
286 42 my $required = $key =~ s/!$//;
287 42 my $error = 0;
288
289 42 my ( $name, $short, $mode_op, $mode, $constraint, $prefilter, $postfilter,
290 $description, $anytrigger, $settrigger, $oktrigger, $failtrigger, $default );
291 42 ▶ if ( $key =~ /
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ / \A (.+?) # name (?:\|(.+?))? # short (?: (?: ([=:]) # op ([isfo]|[0-9]+) # type | (\+) # increment type ) )?$ \z /x) { } 42 0 42
292 \A
293 (.+?) # name
294 (?:\|(.+?))? # short
295 (?:
296 (?:
297 ([=:]) # op
298 ([isfo]|[0-9]+) # type
299 |
300 (\+) # increment type
301 )
302 )?$
303 \z
304 /x ) {
305 42 $name = $1;
306 42 ▶ $short = $2 || '';
Condition100%2/2
$2 || ''
l!l
2319
MC/DC100%1/1
$2 || ''
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
307 42 ▶ $mode_op = $3 || '';
Condition100%2/2
$3 || ''
l!l
3012
MC/DC100%1/1
$3 || ''
$3
Truth table100%2/2
A
A= $3
Aresult
11
00
308 42 ▶ $mode = $4 || $5 || 'b';
Condition80%4/5
$4 || $5 || 'b'
l!l
3012
$4 or $5
l!l&&r!l&&!r
30012
MC/DC50%1/2
$4 || $5 || 'b'
$4 $5
Truth table66%2/3
A or B
A= $4B= $5
ABresult
1X1
011
000
309 }
310 else {
311 0 die "Could not use key '$key' for validation! RTFM"
312 }
313
314 42 ▶ my $rstruct = ref( $kstruct ) || '';
Condition100%2/2
ref $kstruct || ''
l!l
2418
MC/DC100%1/1
ref $kstruct || ''
ref $kstruct
Truth table100%2/2
A
A= ref $kstruct
Aresult
11
00
315 42 ▶ if ( $rstruct eq 'HASH' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($rstruct eq 'HASH') { } 24 18 42
elsif ($rstruct eq 'Regexp' or $rstruct eq 'CODE') { } 0 18 18
elsif ($rstruct) { } 0 18 18
elsif (defined $kstruct and length $kstruct > 1) { } 4 28 32
316 $required = $kstruct->{ required } || 0
317 24 ▶ if defined $kstruct->{ required };
Branch: 1 branch
BranchTrueFalseTotal
if defined $kstruct->{"required"} 5 19 24
Condition50%1/2
$kstruct->{'required'} || 0
l!l
50
MC/DC0%0/1
$kstruct->{'required'} || 0
$kstruct->{'required'}
Truth table50%1/2
A
A= $kstruct->{'required'}
Aresult
11
00
318 $constraint = $kstruct->{ constraint }
319 24 ▶ if defined $kstruct->{ constraint };
Branch: 1 branch
BranchTrueFalseTotal
if defined $kstruct->{"constraint"} 15 9 24
320 $prefilter = $kstruct->{ prefilter }
321 24 ▶ if defined $kstruct->{ prefilter };
Branch: 1 branch
BranchTrueFalseTotal
if defined $kstruct->{"prefilter"} 0 24 24
322 $postfilter = $kstruct->{ postfilter }
323 24 ▶ if defined $kstruct->{ postfilter };
Branch: 1 branch
BranchTrueFalseTotal
if defined $kstruct->{"postfilter"} 3 21 24
324 $anytrigger = $kstruct->{ anytrigger }
325 24 ▶ if defined $kstruct->{ anytrigger };
Branch: 1 branch
BranchTrueFalseTotal
if defined $kstruct->{"anytrigger"} 2 22 24
326 $settrigger = $kstruct->{ settrigger }
327 24 ▶ if defined $kstruct->{ settrigger };
Branch: 1 branch
BranchTrueFalseTotal
if defined $kstruct->{"settrigger"} 2 22 24
328 $oktrigger = $kstruct->{ oktrigger }
329 24 ▶ if defined $kstruct->{ oktrigger };
Branch: 1 branch
BranchTrueFalseTotal
if defined $kstruct->{"oktrigger"} 2 22 24
330 $failtrigger = $kstruct->{ failtrigger }
331 24 ▶ if defined $kstruct->{ failtrigger };
Branch: 1 branch
BranchTrueFalseTotal
if defined $kstruct->{"failtrigger"} 2 22 24
332 $description = $kstruct->{ description }
333 24 ▶ if defined $kstruct->{ description };
Branch: 1 branch
BranchTrueFalseTotal
if defined $kstruct->{"description"} 5 19 24
334 $default = $kstruct->{ default }
335 24 ▶ if defined $kstruct->{ default };
Branch: 1 branch
BranchTrueFalseTotal
if defined $kstruct->{"default"} 0 24 24
336 }
337 ▶ elsif ( $rstruct eq 'Regexp' || $rstruct eq 'CODE' ) {
Condition33%1/3
$rstruct eq "Regexp" or $rstruct eq "CODE"
l!l&&r!l&&!r
0018
MC/DC0%0/2
$rstruct eq "Regexp" or $rstruct eq "CODE"
$rstruct eq "Regexp" $rstruct eq "CODE"
Truth table33%1/3
A or B
A= $rstruct eq "Regexp"B= $rstruct eq "CODE"
ABresult
1X1
011
000
338 0 $constraint = $kstruct;
339 }
340 elsif ( $rstruct ) {
341 0 die "Invalid constraint for key $name. Neither regexp-ref nor code-ref nor scalar"
342 }
343 ▶ elsif ( defined $kstruct && length( $kstruct ) > 1 ) {
Condition66%2/3
defined $kstruct and length $kstruct > 1
!ll&&!rl&&r
1404
MC/DC50%1/2
defined $kstruct and length $kstruct > 1
defined $kstruct length $kstruct > 1
Truth table66%2/3
A and B
A= defined $kstructB= length $kstruct > 1
ABresult
0X0
100
111
344 4 $description = $kstruct;
345 }
346
347 42 ▶ $default = $mode eq 's' ? '' : 0
Branch: 2 branches
BranchTrueFalseTotal
unless defined $default 42 0 42
$mode eq 's' ? : 26 16 42
348 unless defined $default;
349
350 42 ▶ $description = "$name value"
Branch: 1 branch
BranchTrueFalseTotal
unless $description 33 9 42
351 unless $description;
352
353 42 $updated_struct{ $name } = {
354 required => $required,
355 short => $short,
356 mode => $mode,
357 mode_op => $mode_op,
358 constraint => $constraint,
359 postfilter => $postfilter,
360 prefilter => $prefilter,
361 description => $description,
362 anytrigger => $anytrigger,
363 settrigger => $settrigger,
364 oktrigger => $oktrigger,
365 failtrigger => $failtrigger,
366 default => $default
367 };
368 42 $simple_struct{ $key } = $name;
369 42 push @order_struct, $name;
370 42 $seen_short{ $short } ++;
371 }
372
373 17 ▶ unless ( defined $updated_struct{ help } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $updated_struct{"help"}) 17 0 17
374 17 ▶ my ( $short, $key ) = $seen_short{ h } ? ( undef, 'help' ) : ( 'h', 'help|h' );
Branch: 1 branch
BranchTrueFalseTotal
$seen_short{'h'} ? : 0 17 17
375 $updated_struct{ help } = {
376 required => 0,
377 short => $short,
378 mode => 'b',
379 mode_op => '',
380 constraint => undef,
381 postfilter => undef,
382 prefilter => undef,
383 description => 'Show this help',
384 anytrigger => undef,
385 0 ▶ settrigger => sub { print $_[-1]->usage; exit; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
386 17 oktrigger => undef,
387 failtrigger => undef,
388 default => 0,
389 };
390 17 $simple_struct{ $key } = 'help';
391 17 push @order_struct, 'help';
392 }
393
394 17 $self->{ updated_struct } = \%updated_struct;
395 17 $self->{ simple_struct } = \%simple_struct;
396 17 $self->{ order_struct } = \@order_struct;
397
398 17 $self;
399 }
400  
401 =head2 collect_argv
402  
403 Collect args found in C<@ARGV> using L<Getopt::Long#GetOptions>
404  
405 =cut
406  
407 17 ▶sub collect_argv {
Subroutine collect_argv: called CC=3 SCAR=11.0
Pod documented
408 17 my ( $self ) = @_;
409 17 my ( %struct, %args );
410 17 ▶ while( my( $key, $name ) = each %{ $self->{ simple_struct } ||= {} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->{"simple_struct"} ||= {};}) 58 17 75
Condition50%1/2
$self->{"simple_struct"} ||= {}
l!l
750
MC/DC0%0/1
$self->{"simple_struct"} ||= {}
$self->{"simple_struct"}
Truth table50%1/2
A
A= $self->{"simple_struct"}
Aresult
11
00
411 58 $key =~ s/!$//;
412 58 my $default = $self->{ updated_struct }->{ $name }->{ default };
413 58 $struct{ $key } = \( $args{ $name } = $default );
414 }
415
416 # DIRY HACK
417 # don't want to have the output of Getopt::Long around here
418 17 my $stderr = *STDERR;
419 17 open my $null, '>', File::Spec->devnull;
420 17 *STDERR = $null;
421 17 GetOptions( %struct );
422 17 *STDERR = $stderr;
423 17 close $null;
424
425 17 $self->args( \%args );
426 }
427  
428 =head2 args
429  
430 Set/get args.
431  
432 =cut
433  
434 34 ▶sub args {
Subroutine args: called CC=3 SCAR=11.1
Pod documented
435 34 my ( $self, $args_ref ) = @_;
436 34 ▶ $self->{ collected_args } = $args_ref
Branch: 1 branch
BranchTrueFalseTotal
if $args_ref 17 17 34
437 if $args_ref;
438 34 ▶ return $self->{ collected_args } ||= {};
Condition50%1/2
$self->{"collected_args"} ||= {}
l!l
340
MC/DC0%0/1
$self->{"collected_args"} ||= {}
$self->{"collected_args"}
Truth table50%1/2
A
A= $self->{"collected_args"}
Aresult
11
00
439 }
440  
441 =head2 validate
442  
443 Performs validation of the input. Returns differently in array- or scalar-context.
444  
445 =over
446  
447 =item * Array context
448  
449 Returns ( has_errors, hashref_of_valid_input )
450  
451 my ( $valid, $input_ref ) = $obj->validate();
452 if ( $valid ) {
453 print "All good, got arg 'some_arg': $input_ref->{ some_arg }\n";
454 }
455  
456 =item * Scalar context
457  
458 Returns whether validation was successfull (or any error ocurred)
459  
460 if ( scalar $obj->validate() ) {
461 print "All good, got arg 'some_arg': ". $opt->valid_args->{ some_arg }. "\n";
462 }
463  
464 =back
465  
466 =cut
467  
468 17 ▶sub validate {
Subroutine validate: called CC=27 SCAR=34.4
Pod documented
469 17 my ( $self ) = @_;
470 17 my $args_ref = $self->args;
471 17 my ( @errors, %valid_args );
472 17 ▶ while( my( $name, $ref ) = each %{ $self->{ updated_struct } ||= {} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->{"updated_struct"} ||= {};}) 58 17 75
Condition50%1/2
$self->{"updated_struct"} ||= {}
l!l
750
MC/DC0%0/1
$self->{"updated_struct"} ||= {}
$self->{"updated_struct"}
Truth table50%1/2
A
A= $self->{"updated_struct"}
Aresult
11
00
473 58 my $error = 0;
474 my ( $required, $constraint, $description, $prefilter, $postfilter )
475 58 = map { $ref->{ $_ } } qw/ required constraint description prefilter postfilter /;
476
477 # get value
478 my $value = defined $args_ref->{ $name }
479 58 ▶ ? $args_ref->{ $name }
Branch: 1 branch
BranchTrueFalseTotal
defined $args_ref->{$name} ? : 58 0 58
480 : undef;
481
482 # run pre filter
483 58 ▶ $value = $prefilter->( $value, $name, $self )
Branch: 1 branch
BranchTrueFalseTotal
if $prefilter 0 58 58
484 if $prefilter;
485
486 # check: required
487 58 ▶ if ( $required && ! $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($required and not $value) 4 54 58
Condition100%3/3
$required and not $value
!ll&&!rl&&r
4684
MC/DC100%2/2
$required and not $value
$required not $value
Truth table100%3/3
A and not(B)
A= $requiredB= not $value
ABresult
0X0
100
111
488 4 push @errors, sprintf( 'Required key "%s" not given', $name );
489 4 $error++;
490 }
491
492 # check: constraint
493 58 ▶ if ( $constraint && defined $value && ( ref( $value ) || length( $value ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constraint and defined $value and ref $value || length $value) 14 44 58
Condition55%5/9
$constraint and defined $value and ref $value || length $value
!ll&&!rl&&r
44014
$constraint and defined $value
!ll&&!rl&&r
44014
ref $value or length $value
l!l&&r!l&&!r
0140
MC/DC25%1/4
$constraint and defined $value and ref $value || length $value
$constraint defined $value ref $value length $value
Truth table40%2/5
A and B and C or D
A= $constraintB= defined $valueC= ref $valueD= length $value
ABCDresult
0XXX0
10XX0
11000
111X1
11011
494 14 ▶ if ( ref( $constraint ) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $constraint eq 'CODE') { } 0 14 14
495 0 ▶ unless ( $constraint->( $value, $name, $self ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$constraint($value, $name, $self)) 0 0 0
496 0 push @errors, sprintf( 'Value of key "%s" is invalid', $name );
497 0 $error++;
498 }
499 }
500 else {
501 14 ▶ unless ( $value =~ $constraint ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$constraint/) 6 8 14
502 6 push @errors, sprintf( 'Value of key "%s" is invalid', $name );
503 6 $error++;
504 }
505 }
506 }
507
508 # get triggers
509 my ( $anytrigger, $settrigger, $oktrigger, $failtrigger )
510 58 = map { $ref->{ $_ } } qw/ anytrigger settrigger oktrigger failtrigger /;
511
512 # no error? valid value -> assi
513 58 ▶ unless ( $error ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($error) { } 10 48 58
if (do { $failtrigger }) 1 9 10
514 48 ▶ $value = $postfilter->( $value )
Branch: 1 branch
BranchTrueFalseTotal
if $postfilter 2 46 48
515 if $postfilter;
516 48 $valid_args{ $name } = $value;
517
518 # call ok trigger?
519 48 ▶ $oktrigger->( $value, $name, $self )
Branch: 1 branch
BranchTrueFalseTotal
if $oktrigger 1 47 48
520 if $oktrigger;
521 }
522
523 # call fail trigger?
524 0 elsif ( $failtrigger ) {
525 1 $failtrigger->( $value, $name, $self );
526 }
527
528 # call any trigger?
529 58 ▶ $anytrigger->( $value, $name, $self )
Branch: 1 branch
BranchTrueFalseTotal
if $anytrigger 2 56 58
530 if $anytrigger;
531
532 # call set trigger?
533 58 ▶ $settrigger->( $value, $name, $self )
Branch: 1 branch
BranchTrueFalseTotal
if $settrigger and $value 1 57 58
Condition100%3/3
$settrigger and $value
!ll&&!rl&&r
39181
MC/DC100%2/2
$settrigger and $value
$settrigger $value
Truth table100%3/3
A and B
A= $settriggerB= $value
ABresult
0X0
100
111
534 if $settrigger && $value;
535 }
536
537
538 17 ▶ if ( $self->{ underscore } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"underscore"}) 12 5 17
539 12 foreach my $k( keys %valid_args ) {
540 30 ( my $ku = $k ) =~ s#\-#_#gms;
541 30 ▶ $valid_args{ $ku } = delete $valid_args{ $k }
Branch: 1 branch
BranchTrueFalseTotal
if $ku ne $k 17 13 30
542 if $ku ne $k;
543 }
544 }
545
546 17 $self->{ valid_args } = \%valid_args;
547 17 $self->{ errors } = \@errors;
548 17 @ERRORS = @errors;
549 17 $ERROR = join( ' ** ', @ERRORS );
550
551 17 ▶ return wantarray ? ( @errors ? 0 : 1, \%valid_args ) : @errors ? 0 : 1;
Branch: 3 branches
BranchTrueFalseTotal
wantarray ? : 0 17 17
@errors ? : 0 0 0
@errors ? : 6 11 17
552 }
553  
554 =head2 valid_args
555  
556 Returns validated args
557  
558 =cut
559  
560 15 ▶sub valid_args {
Subroutine valid_args: called CC=1 SCAR=0.0
Pod documented
561 15 shift->{ valid_args };
562 }
563  
564 =head2 usage
565  
566 Returns usage as string
567  
568 =cut
569  
570 3 ▶sub usage {
Subroutine usage: called CC=9 SCAR=23.3
Pod documented
571 3 my ( $self, $do_print ) = @_;
572 my @output = (
573 sprintf( 'Program: %s', $self->{ name } || $0 ),
574 0 ▶ sprintf( 'Version: %s', $self->{ version } || eval { $main::VERSION } || 'unknown' ),
Condition37%3/8
$self->{'name'} || $0
l!l&&r!l&&!r
300
$self->{'version'} || eval { do { $main::VERSION } } || 'unknown'
l!l
30
$self->{"version"} or eval { do { $main::VERSION } }
l!l&&r!l&&!r
300
MC/DC0%0/2
$self->{'name'} || $0
$self->{'name'} $0
MC/DC0%0/2
$self->{'version'} || eval { do { $main::VERSION } } || 'unknown'
$self->{"version"} eval { do { $main::VERSION } }
Truth table33%1/3
A || B
A= $self->{'name'}B= $0
ABresult
1X1
011
000
Truth table33%1/3
A or B
A= $self->{"version"}B= eval { do { $main::VERSION } }
ABresult
1X1
011
000
575 '',
576 'Usage: '. $0. ' <parameters>',
577 '',
578 'Parameter:'
579 );
580 16 ▶ my $mode_out = sub {
Subroutine __ANON__: called CC=6 SCAR=19.9
581 16 my $m = shift;
582 16 ▶ if ( $m eq 's' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($m eq 's') { } 7 9 16
elsif ($m eq 'i') { } 3 6 9
elsif ($m eq 'f') { } 0 6 6
elsif ($m eq 'o') { } 0 6 6
elsif ($m =~ /^[0-9]|\+$/) { } 0 6 6
583 7 return 'string';
584 }
585 elsif ( $m eq 'i' ) {
586 3 return 'integer';
587 }
588 elsif ( $m eq 'f' ) {
589 0 return 'real';
590 }
591 elsif ( $m eq 'o' ) {
592 0 return 'octet or hex';
593 }
594 elsif ( $m =~ /^[0-9]|\+$/ ) {
595 0 return 'optional:integer'
596 }
597 else {
598 6 return 'bool';
599 }
600 3 };
601 3 foreach my $name( @{ $self->{ order_struct } } ) {
602 16 my $ref = $self->{ updated_struct }->{ $name };
603 16 my @arg_out = ( ' --'. $name );
604 16 ▶ push @arg_out, ' | -'. $ref->{ short } if $ref->{ short };
Branch: 1 branch
BranchTrueFalseTotal
if $ref->{"short"} 16 0 16
605 16 push @arg_out, ' : '. $mode_out->( $ref->{ mode } );
606 16 my $arg_out = join( '', @arg_out );
607 16 ▶ $arg_out .= ' '. $REQUIRED_STR if $ref->{ required };
Branch: 1 branch
BranchTrueFalseTotal
if $ref->{"required"} 10 6 16
608 16 push @output, $arg_out;
609
610 my @description = ref( $ref->{ description } )
611 3 ? ( map { ' '. $_ } @{ $ref->{ description } } )
612 16 ▶ : ( ' '. $ref->{ description } );
Branch: 1 branch
BranchTrueFalseTotal
ref $ref->{'description'} ? : 1 15 16
613 16 push @output, join( "\n", @description );
614 16 push @output, '';
615 }
616 3 push @output, '';
617
618 3 my $output = join( "\n", @output );
619 3 ▶ print $output if $do_print;
Branch: 1 branch
BranchTrueFalseTotal
if $do_print 0 3 3
620 3 return $output;
621 }
622  
623 =head2 errors
624  
625 Returns errors as joined string or array of strings of the last valid-run
626  
627 =cut
628  
629 1 ▶sub errors {
Subroutine errors: called CC=6 SCAR=26.2
Pod documented
630 1 my ( $self, $sep ) = @_;
631 return ! $sep && wantarray
632 1 ▶ ? @{ $self->{ errors } ||= [] }
Condition25%1/4
$self->{"errors"} ||= []
l!l
10
$sep || "\n"
l!l
00
MC/DC0%0/1
$self->{"errors"} ||= []
$self->{"errors"}
MC/DC0%0/1
$sep || "\n"
$sep
Truth table50%1/2
A
A= $self->{"errors"}
Aresult
11
00
Truth table0%0/2
A
A= $sep
Aresult
11
00
633 0 ▶ : join( $sep || "\n", @{ $self->{ errors } ||= [] } );
Branch: 1 branch
BranchTrueFalseTotal
!$sep && wantarray ? : 1 0 1
Condition20%1/5
not $sep and wantarray
!ll&&!rl&&r
001
$self->{"errors"} ||= []
l!l
00
MC/DC0%0/2
not $sep and wantarray
not $sep wantarray
MC/DC0%0/1
$self->{"errors"} ||= []
$self->{"errors"}
Truth table33%1/3
not(A) and B
A= not $sepB= wantarray
ABresult
0X0
100
111
Truth table0%0/2
A
A= $self->{"errors"}
Aresult
11
00
634 }
635  
636 =head1 SEE ALSO
637  
638 =over
639  
640 =item * L<Getopt::Long>
641  
642 =item * Latest release on Github L<http://github.com/ukautz/Getopt-Valid>
643  
644 =back
645  
646 =head1 AUTHOR
647  
648 =over
649  
650 =item * Ulrich Kautz <uk@fortrabbit.de>
651  
652  
653 =back
654  
655 =head1 COPYRIGHT AND WARRANTY
656  
657 Copyright (c) 2012 the L</AUTHOR> as listed above.
658  
659 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
660  
661 =head1 LICENCSE
662  
663 This library is free software and may be distributed under the same terms as perl itself.
664  
665 =cut
666  
667 5 1;
↑ Summary