line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Rex::Dondley::ProcessTaskArgs ; |
2
|
|
|
|
|
|
|
$Rex::Dondley::ProcessTaskArgs::VERSION = '0.012'; |
3
|
5
|
|
|
5
|
|
73615
|
use strict; |
|
5
|
|
|
|
|
35
|
|
|
5
|
|
|
|
|
152
|
|
4
|
5
|
|
|
5
|
|
24
|
use warnings; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
3690
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
require Exporter; |
7
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
8
|
|
|
|
|
|
|
our @EXPORT = 'process_task_args'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# checks validity of args passed to functions and assigns them to appropriate keys |
11
|
|
|
|
|
|
|
# Accept 3 sets of args: |
12
|
|
|
|
|
|
|
# first arg is reference to parameters passed by user to task |
13
|
|
|
|
|
|
|
# next set of args is a list of allowed params which can also indicate required params |
14
|
|
|
|
|
|
|
# last arg is an array hash for default values corresponding to each allowed params |
15
|
|
|
|
|
|
|
sub process_task_args { |
16
|
35
|
50
|
33
|
35
|
1
|
1041069
|
if (!$_[0] || (ref $_[0]) ne 'ARRAY') { |
17
|
0
|
|
|
|
|
0
|
die 'First argument must be an array ref to user supplied arguments.'; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# standardize the argument data structure |
21
|
35
|
100
|
66
|
|
|
425
|
if (ref $_[0] && ref $_[0]->[0] ne 'HASH') { |
22
|
7
|
|
|
|
|
21
|
my @args = @{$_[0]}; |
|
7
|
|
|
|
|
59
|
|
23
|
7
|
|
|
|
|
19
|
shift @_; |
24
|
7
|
|
|
|
|
100
|
@_ = ([ {}, \@args,], @_ ); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
35
|
|
|
|
|
117
|
my $passed_in = shift @_; |
28
|
35
|
|
|
|
|
82
|
my %passed_params = %{$passed_in->[0]}; |
|
35
|
|
|
|
|
223
|
|
29
|
35
|
|
|
|
|
90
|
my @unkeyed_args = @{$passed_in->[1]}; |
|
35
|
|
|
|
|
105
|
|
30
|
35
|
100
|
|
|
|
257
|
my @defaults = ref $_[-1] ? @{$_[-1]} : (); |
|
6
|
|
|
|
|
54
|
|
31
|
35
|
100
|
|
|
|
123
|
pop @_ if @defaults; |
32
|
35
|
|
|
|
|
247
|
my @valid_args = @_; |
33
|
35
|
100
|
100
|
|
|
102
|
my @key_list = grep { $_ && $_ ne '1' && (ref $_) ne 'ARRAY' } @_; |
|
137
|
|
|
|
|
958
|
|
34
|
|
|
|
|
|
|
|
35
|
35
|
|
|
|
|
100
|
my %defaults = (); |
36
|
35
|
|
|
|
|
78
|
my $count = 0; |
37
|
35
|
|
|
|
|
164
|
foreach my $key (@key_list) { |
38
|
74
|
|
|
|
|
409
|
$defaults{$key} = $defaults[$count++]; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# create a hash of valid and required keys |
42
|
|
|
|
|
|
|
# assumes all values are not required if @valid_args do not contain required value |
43
|
35
|
|
|
|
|
71
|
my @ordered_keys; |
44
|
35
|
|
|
|
|
79
|
my %valid_keys = (); |
45
|
35
|
100
|
100
|
|
|
757
|
if ((exists $valid_args[1] && ($valid_args[1] !~ /^0|1$/)) || scalar @valid_args == 1) { # checks to see if list contains required values |
|
|
|
100
|
|
|
|
|
46
|
7
|
|
|
|
|
56
|
foreach my $arg (@valid_args) { |
47
|
11
|
|
|
|
|
32
|
$valid_keys{$arg} = 0; |
48
|
11
|
|
|
|
|
37
|
@ordered_keys = @valid_args; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} else { |
51
|
28
|
|
|
|
|
184
|
%valid_keys = @valid_args; |
52
|
28
|
|
|
|
|
53
|
my $count = 0; |
53
|
28
|
|
|
|
|
72
|
foreach my $key (@valid_args) { |
54
|
126
|
100
|
|
|
|
335
|
if (!($count++ % 2)) { |
55
|
63
|
|
|
|
|
220
|
push @ordered_keys, $key; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# check to see if passed parameters are valid |
61
|
35
|
|
|
|
|
64
|
my @invalid_keys; |
62
|
35
|
|
|
|
|
139
|
foreach my $key (keys %passed_params) { |
63
|
24
|
|
|
|
|
126
|
my $is_valid = grep { $_ eq $key } keys %valid_keys; |
|
52
|
|
|
|
|
158
|
|
64
|
24
|
100
|
|
|
|
78
|
if (!$is_valid) { |
65
|
2
|
|
|
|
|
15
|
push @invalid_keys, $key; |
66
|
|
|
|
|
|
|
} |
67
|
24
|
100
|
|
|
|
129
|
die ("Invalid key(s): '" . join (', ', @invalid_keys) . "' from ". (caller)[1] . ', line ' . (caller)[2]) if @invalid_keys; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Populate the %passed_params hash with @unkeyed_args according |
72
|
|
|
|
|
|
|
# to same order they were passed to this function via @valid_args. |
73
|
|
|
|
|
|
|
# Throw error if there are more args than available keys. |
74
|
33
|
100
|
|
|
|
104
|
if (@unkeyed_args) { |
75
|
12
|
|
|
|
|
35
|
my @all_array_args = @unkeyed_args; |
76
|
12
|
|
|
|
|
21
|
foreach my $array_arg (@unkeyed_args) { |
77
|
19
|
|
|
|
|
35
|
foreach my $vkey (@key_list) { |
78
|
27
|
100
|
|
|
|
60
|
if (exists $passed_params{$vkey}) { |
79
|
9
|
|
|
|
|
25
|
next; |
80
|
|
|
|
|
|
|
} |
81
|
18
|
|
|
|
|
32
|
$passed_params{$vkey} = $array_arg; |
82
|
18
|
|
|
|
|
27
|
shift @all_array_args; |
83
|
|
|
|
|
|
|
|
84
|
18
|
|
|
|
|
34
|
last; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
} |
87
|
12
|
100
|
|
|
|
40
|
die ('Too many array arguments passed from ' . (caller)[1] . ', line ' . (caller)[2] ) if @all_array_args; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# Ensure required args are present |
92
|
32
|
|
|
|
|
98
|
my @reqd_keys = grep { $valid_keys{$_} } keys %valid_keys; |
|
70
|
|
|
|
|
270
|
|
93
|
32
|
|
|
|
|
81
|
my @missing_keys; |
94
|
|
|
|
|
|
|
|
95
|
32
|
|
|
|
|
78
|
foreach my $rkey(@reqd_keys) { |
96
|
24
|
100
|
66
|
|
|
342
|
if (!exists $passed_params{$rkey} || $passed_params{$rkey} eq '1') { |
97
|
10
|
100
|
|
|
|
74
|
push @missing_keys, $rkey unless $defaults{$rkey}; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
} |
100
|
32
|
100
|
|
|
|
251
|
die ("Missing required key(s): '" . join (', ', @missing_keys) . "' from " . (caller)[1] . ', line ' . (caller)[2]) if @missing_keys; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# handle edge case when user passes key without value |
103
|
28
|
|
|
|
|
74
|
foreach my $key (keys %passed_params) { |
104
|
36
|
0
|
33
|
|
|
161
|
if ($passed_params{$key} eq '1' && $valid_keys{$key}) { |
105
|
0
|
|
|
|
|
0
|
delete $passed_params{$key}; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
} |
108
|
28
|
|
|
|
|
170
|
my %return_hash = (%defaults, %passed_params); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
28
|
100
|
|
|
|
90
|
if (wantarray) { |
113
|
1
|
|
|
|
|
5
|
my @blah = @return_hash{ @ordered_keys }; |
114
|
1
|
|
|
|
|
32
|
return @blah; |
115
|
|
|
|
|
|
|
} else { |
116
|
27
|
|
|
|
|
343
|
return \%return_hash; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
# methods here |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
1; # Magic true value |
122
|
|
|
|
|
|
|
# ABSTRACT: easier Rex task argument handling |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
__END__ |