blib/lib/Perinci/Sub/GetArgs/Argv.pm

filter: Statementstmt 94.9% 447 / 471 Branchbran 82.6% 190 / 230 Conditioncond 75.3% 104 / 138 MC/DCmcdc 56.5% 43 / 76 Subroutinesub 96.7% 30 / 31 Podpod 100% 2 / 2 totaltotal 86.0% 816 / 948 CC 188 SCAR 36.1
CC
188
Cov
88%
get_args_from_argv
73.3
_args2opts
45.1
_opt2ospec
26.1
CRAP 36.8
↑ Summary
1 package Perinci::Sub::GetArgs::Argv;
2  
3 4 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #use Log::Any '$log';
7  
8 4 ▶use Data::Sah::Normalize qw(normalize_schema);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Data::Sah::Util::Type qw(is_type is_simple);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Getopt::Long::Negate::EN qw(negations_for_option);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Getopt::Long::Util qw(parse_getopt_long_opt_spec);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Perinci::Sub::GetArgs::Array qw(get_args_from_array);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Perinci::Sub::Util qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
17 4 our $DATE = '2023-02-24'; # DATE
18 4 our $DIST = 'Perinci-Sub-GetArgs-Argv'; # DIST
19 4 our $VERSION = '0.850'; # VERSION
20  
21 4 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 our @ISA = qw(Exporter);
23 4 our @EXPORT_OK = qw(
24 gen_getopt_long_spec_from_meta
25 get_args_from_argv
26 );
27  
28 4 our %SPEC;
29  
30 4 $SPEC{':package'} = {
31 v => 1.1,
32 summary => 'Get subroutine arguments from command line arguments (@ARGV)',
33 };
34  
35 # retun ($success?, $errmsg, $res)
36 3 ▶sub _parse_json {
Subroutine _parse_json: called CC=3 SCAR=11.1
37 3 my $str = shift;
38  
39 3 state $json = do {
40 1 require JSON::PP;
41 1 JSON::PP->new->allow_nonref;
42 };
43  
44 # to rid of those JSON::PP::Boolean objects which currently choke
45 # Data::Sah-generated validator code. in the future Data::Sah can be
46 # modified to handle those, or we use a fork of JSON::PP which doesn't
47 # produce those in the first place (probably only when performance is
48 # critical).
49 3 state $cleanser = do {
50 1 ▶ if (eval { require Data::Clean::FromJSON; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Data::Clean::FromJSON; 1 } }) { } 1 0 1
51 1 Data::Clean::FromJSON->get_cleanser;
52 } else {
53 0 undef;
54 }
55 };
56  
57 3 my $res;
58 3 ▶ eval { $res = $json->decode($str); $cleanser->clean_in_place($res) if $cleanser };
Branch: 1 branch
BranchTrueFalseTotal
if $cleanser 1 0 1
59 3 my $e = $@;
60 3 return (!$e, $e, $res);
61 }
62  
63 3 ▶sub _parse_yaml {
Subroutine _parse_yaml: called CC=5 SCAR=18.7
64 4 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 3 state $yaml_xs_available = do {
67 1 ▶ if (eval { require YAML::XS; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require YAML::XS; 1 } }) { } 1 0 1
68 1 1;
69 } else {
70 0 require YAML::Old;
71 0 0;
72 }
73 };
74  
75 3 my $str = shift;
76  
77 #local $YAML::Syck::ImplicitTyping = 1;
78 3 my $res;
79 3 eval {
80 3 ▶ if ($yaml_xs_available) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml_xs_available) { } 3 0 3
81 3 $res = YAML::XS::Load($str);
82 } else {
83 # YAML::Old is too strict, it requires "--- " header and newline
84 # ending
85 0 ▶ $str = "--- $str" unless $str =~ /\A--- /;
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /\A--- / 0 0 0
86 0 ▶ $str .= "\n" unless $str =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /\n\z/ 0 0 0
87 0 $res = YAML::Old::Load($str);
88 }
89 };
90 3 my $e = $@;
91 3 return (!$e, $e, $res);
92 }
93  
94 234 ▶sub _arg2opt {
Subroutine _arg2opt: called CC=1 SCAR=0.0
95 234 my $opt = shift;
96 234 $opt =~ s/[^A-Za-z0-9-]+/-/g; # foo.bar_baz becomes --foo-bar-baz
97 234 $opt;
98 }
99  
100 # this subroutine checks whether a schema mentions a coercion rule from simple
101 # types (e.g. 'str_comma_sep', etc).
102 184 ▶sub _is_coercible_from_simple {
Subroutine _is_coercible_from_simple: called CC=7 SCAR=20.9
103 184 my $nsch = shift;
104 184 ▶ my $clset = $nsch->[1] or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $clset = $nsch->[1] or return 0 184 0 184
105 184 ▶ my $rules = $clset->{'x.perl.coerce_rules'} // $clset->{'x.coerce_rules'}
Branch: 1 branch
BranchTrueFalseTotal
my $rules = $clset->{"x.perl.coerce_rules"} // $clset->{"x.coerce_rules"} or return 0 2 182 184
Condition66%2/3
$clset->{"x.perl.coerce_rules"} // $clset->{"x.coerce_rules"}
l!l&&r!l&&!r
20182
MC/DC50%1/2
$clset->{"x.perl.coerce_rules"} // $clset->{"x.coerce_rules"}
$clset->{"x.perl.coerce_rules"} $clset->{"x.coerce_rules"}
Truth table66%2/3
A // B
A= $clset->{"x.perl.coerce_rules"}B= $clset->{"x.coerce_rules"}
ABresult
1X1
011
000
106 or return 0;
107 2 for my $rule (@$rules) {
108 2 ▶ next unless $rule =~ /\A([^_]+)_/;
Branch: 1 branch
BranchTrueFalseTotal
unless $rule =~ /\A([^_]+)_/ 0 2 2
109 2 ▶ return 1 if is_simple($1);
Branch: 1 branch
BranchTrueFalseTotal
if is_simple($1) 2 0 2
110 }
111 0 0;
112 }
113  
114 541 ▶sub _is_simple_or_coercible_from_simple {
Subroutine _is_simple_or_coercible_from_simple: called CC=2 SCAR=6.9
115 541 my $nsch = shift;
116 541 ▶ is_simple($nsch) || _is_coercible_from_simple($nsch);
Branch: 1 branch
BranchTrueFalseTotal
is_simple($nsch) or _is_coercible_from_simple($nsch) 357 184 541
117 }
118  
119 # this routine's job is to avoid using Data::Sah::Resolve unless it needs to, to
120 # reduce startup overhead
121 442 ▶sub _is_simple_or_array_of_simple_or_hash_of_simple {
Subroutine _is_simple_or_array_of_simple_or_hash_of_simple: called CC=15 SCAR=27.2
122 442 my $nsch = shift;
123  
124 442 my $is_simple = 0;
125 442 my $is_array_of_simple = 0;
126 442 my $is_hash_of_simple = 0;
127 442 my $eltype;
128  
129 442 my $type = $nsch->[0];
130 442 my $clset = $nsch->[1];
131  
132 {
133 # if not known as builtin type, then resolve it first
134 442 ▶ unless (is_type($nsch)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_type($nsch)) 1 441 442
135 1 require Data::Sah::Resolve;
136 1 my $res = Data::Sah::Resolve::resolve_schema(
137 {schema_is_normalized=>1}, $nsch);
138 1 $type = $res->{type};
139 1 ▶ $clset = $res->{clsets_after_type}[0] // {};
Condition50%1/2
$res->{"clsets_after_type"}[0] // {}
l!l
10
MC/DC0%0/1
$res->{"clsets_after_type"}[0] // {}
$res->{"clsets_after_type"}[0]
Truth table50%1/2
A
A= $res->{"clsets_after_type"}[0]
Aresult
11
00
140 }
141  
142 442 $is_simple = _is_simple_or_coercible_from_simple([$type, $clset]);
143 442 ▶ last if $is_simple;
Branch: 1 branch
BranchTrueFalseTotal
if $is_simple 262 180 442
144  
145 180 ▶ if ($type eq 'array') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "array") 100 80 180
146 100 ▶ my $elnsch = $clset->{of} // $clset->{each_elem};
Condition66%2/3
$clset->{"of"} // $clset->{"each_elem"}
l!l&&r!l&&!r
64036
MC/DC50%1/2
$clset->{"of"} // $clset->{"each_elem"}
$clset->{"of"} $clset->{"each_elem"}
Truth table66%2/3
A // B
A= $clset->{"of"}B= $clset->{"each_elem"}
ABresult
1X1
011
000
147 100 ▶ last unless $elnsch;
Branch: 1 branch
BranchTrueFalseTotal
unless $elnsch 36 64 100
148 64 $elnsch = normalize_schema($elnsch);
149 64 $eltype = $elnsch->[0];
150  
151 # if not known as builtin type, then resolve it first
152 64 ▶ unless (is_type($elnsch)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_type($elnsch)) 1 63 64
153 1 require Data::Sah::Resolve;
154 1 my $res = Data::Sah::Resolve::resolve_schema(
155 {schema_is_normalized=>1}, $elnsch);
156 1 ▶ $elnsch = [$res->{type}, $res->{clsets_after_type}[0] // {}]; # XXX we only take the first clause set
Condition50%1/2
$res->{"clsets_after_type"}[0] // {}
l!l
10
MC/DC0%0/1
$res->{"clsets_after_type"}[0] // {}
$res->{"clsets_after_type"}[0]
Truth table50%1/2
A
A= $res->{"clsets_after_type"}[0]
Aresult
11
00
157 1 $eltype = $res->{type};
158 }
159  
160 64 $is_array_of_simple = _is_simple_or_coercible_from_simple($elnsch);
161 64 last;
162 }
163  
164 80 ▶ if ($type eq 'hash') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "hash") 79 1 80
165 79 ▶ my $elnsch = $clset->{of} // $clset->{each_value} // $clset->{each_elem};
Condition66%4/6
($clset->{"of"} // $clset->{"each_value"}) // $clset->{"each_elem"}
l!l&&r!l&&!r
35044
$clset->{"of"} // $clset->{"each_value"}
l!l&&r!l&&!r
35044
MC/DC33%1/3
($clset->{"of"} // $clset->{"each_value"}) // $clset->{"each_elem"}
$clset->{"of"} $clset->{"each_value"} $clset->{"each_elem"}
Truth table50%2/4
A // B // C
A= $clset->{"of"}B= $clset->{"each_value"}C= $clset->{"each_elem"}
ABCresult
1XX1
01X1
0011
0000
166 79 ▶ last unless $elnsch;
Branch: 1 branch
BranchTrueFalseTotal
unless $elnsch 44 35 79
167 35 $elnsch = normalize_schema($elnsch);
168 35 $eltype = $elnsch->[0];
169  
170 # if not known as builtin type, then resolve it first
171 35 ▶ unless (is_type($elnsch)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_type($elnsch)) 1 34 35
172 1 require Data::Sah::Resolve;
173 1 my $res = Data::Sah::Resolve::resolve_schema(
174 {schema_is_normalized=>1}, $elnsch);
175 1 ▶ $elnsch = [$res->{type}, $res->{clsets_after_type}[0] // {}]; # XXX we only take the first clause set
Condition50%1/2
$res->{"clsets_after_type"}[0] // {}
l!l
10
MC/DC0%0/1
$res->{"clsets_after_type"}[0] // {}
$res->{"clsets_after_type"}[0]
Truth table50%1/2
A
A= $res->{"clsets_after_type"}[0]
Aresult
11
00
176 1 $eltype = $res->{type};
177 }
178  
179 35 $is_hash_of_simple = _is_simple_or_coercible_from_simple($elnsch);
180 35 last;
181 }
182 }
183  
184 #{ no warnings 'uninitialized'; say "D:$nsch->[0]: is_simple=<$is_simple>, is_array_of_simple=<$is_array_of_simple>, is_hash_of_simple=<$is_hash_of_simple>, type=<$type>, eltype=<$eltype>" };
185 442 ($is_simple, $is_array_of_simple, $is_hash_of_simple, $type, $clset, $eltype);
186 }
187  
188 # return one or more triplets of Getopt::Long option spec, its parsed structure,
189 # and extra stuffs. we do this to avoid having to call
190 # parse_getopt_long_opt_spec().
191 224 ▶sub _opt2ospec {
Subroutine _opt2ospec: called CC=26 SCAR=32.6
192 224 my ($opt, $schema, $arg_spec) = @_;
193 224 my ($is_simple, $is_array_of_simple, $is_hash_of_simple, $type, $clset, $eltype) =
194 _is_simple_or_array_of_simple_or_hash_of_simple($schema);
195  
196 224 my (@opts, @types, @isaos, @ishos);
197  
198 224 ▶ if ($is_array_of_simple || $is_hash_of_simple) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_array_of_simple or $is_hash_of_simple) 54 170 224
Condition100%3/3
$is_array_of_simple or $is_hash_of_simple
l!l&&r!l&&!r
3519170
MC/DC100%2/2
$is_array_of_simple or $is_hash_of_simple
$is_array_of_simple $is_hash_of_simple
Truth table100%3/3
A or B
A= $is_array_of_simpleB= $is_hash_of_simple
ABresult
1X1
011
000
199 54 my $singular_opt;
200 54 ▶ if ($arg_spec && $arg_spec->{'x.name.is_plural'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_spec and $arg_spec->{'x.name.is_plural'}) { } 2 52 54
Condition100%3/3
$arg_spec and $arg_spec->{"x.name.is_plural"}
!ll&&!rl&&r
17352
MC/DC100%2/2
$arg_spec and $arg_spec->{"x.name.is_plural"}
$arg_spec $arg_spec->{"x.name.is_plural"}
Truth table100%3/3
A and B
A= $arg_specB= $arg_spec->{"x.name.is_plural"}
ABresult
0X0
100
111
201 2 ▶ if ($arg_spec->{'x.name.singular'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_spec->{'x.name.singular'}) { } 1 1 2
202 1 $singular_opt = _arg2opt($arg_spec->{'x.name.singular'});
203 } else {
204 1 require Lingua::EN::PluralToSingular;
205 1 $singular_opt = Lingua::EN::PluralToSingular::to_singular($opt);
206 }
207 } else {
208 52 $singular_opt = $opt;
209 }
210 54 push @opts , $singular_opt;
211 54 push @types, $eltype;
212 54 ▶ push @isaos, $is_array_of_simple ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$is_array_of_simple ? : 35 19 54
213 54 ▶ push @ishos, $is_hash_of_simple ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$is_hash_of_simple ? : 19 35 54
214 }
215  
216 224 ▶ if ($is_simple || !@opts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_simple or not @opts) 170 54 224
Condition100%3/3
$is_simple or not @opts
l!l&&r!l&&!r
1323854
MC/DC100%2/2
$is_simple or not @opts
$is_simple not @opts
Truth table100%3/3
A or not(B)
A= $is_simpleB= not @opts
ABresult
1X1
011
000
217 170 push @opts , $opt;
218 170 push @types, $type;
219 170 push @isaos, 0;
220 170 push @ishos, 0;
221 }
222  
223 224 my @res;
224  
225 224 for my $i (0..$#opts) {
226 224 my $opt = $opts[$i];
227 224 my $type = $types[$i];
228 224 my $isaos = $isaos[$i];
229 224 my $ishos = $ishos[$i];
230  
231 224 ▶ if ($type eq 'bool') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'bool') { } 22 202 224
elsif ($type eq 'buf') { } 3 199 202
232 22 ▶ if (length $opt == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (length $opt == 1) { } 4 18 22
elsif ($clset->{'is'} or $clset->{'is_true'}) { } 2 16 18
elsif (defined $clset->{'is'} and not $clset->{'is'} or defined $clset->{'is_true'} and not $clset->{'is_true'}) { } 1 15 16
233 # single-letter option like -b doesn't get --nob.
234 4 push @res, ($opt, {opts=>[$opt]}), undef;
235 ▶ } elsif ($clset->{is} || $clset->{is_true}) {
Condition100%3/3
$clset->{"is"} or $clset->{"is_true"}
l!l&&r!l&&!r
1116
MC/DC100%2/2
$clset->{"is"} or $clset->{"is_true"}
$clset->{"is"} $clset->{"is_true"}
Truth table100%3/3
A or B
A= $clset->{"is"}B= $clset->{"is_true"}
ABresult
1X1
011
000
236 # an always-true bool ('true' or [bool => {is=>1}] or
237 # [bool=>{is_true=>1}] also means it's a flag and should not get
238 # --nofoo.
239 2 push @res, ($opt, {opts=>[$opt]}), undef;
240 ▶ } elsif ((defined $clset->{is} && !$clset->{is}) ||
Condition55%5/9
defined $clset->{"is"} and not $clset->{"is"} or defined $clset->{"is_true"} and not $clset->{"is_true"}
l!l&&r!l&&!r
0115
defined $clset->{"is"} and not $clset->{"is"}
!ll&&!rl&&r
1600
defined $clset->{"is_true"} and not $clset->{"is_true"}
!ll&&!rl&&r
1501
MC/DC25%1/4
defined $clset->{"is"} and not $clset->{"is"} or defined $clset->{"is_true"} and not $clset->{"is_true"}
defined $clset->{"is"} not $clset->{"is"} defined $clset->{"is_true"} not $clset->{"is_true"}
Truth table28%2/7
A and not(B) or C and not(D)
A= defined $clset->{"is"}B= not $clset->{"is"}C= defined $clset->{"is_true"}D= not $clset->{"is_true"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
241 (defined $clset->{is_true} && !$clset->{is_true})) {
242 # an always-false bool ('false' or [bool => {is=>0}] or
243 # [bool=>{is_true=>0}] also means it's a flag and should only be
244 # getting --nofoo.
245 1 for (negations_for_option($opt)) {
246 2 push @res, $_, {opts=>[$_]}, {is_neg=>1, pos_opts=>[$opt]};
247 }
248 } else {
249 # a regular bool gets --foo as well as --nofoo
250 15 my @negs = negations_for_option($opt);
251 15 push @res, $opt, {opts=>[$opt]}, {is_neg=>0, neg_opts=>\@negs};
252 15 for (@negs) {
253 29 push @res, $_, {opts=>[$_]}, {is_neg=>1, pos_opts=>[$opt]};
254 }
255 }
256 } elsif ($type eq 'buf') {
257 3 push @res, (
258 "$opt=s", {opts=>[$opt], desttype=>"", type=>"s"}, undef,
259 "$opt-base64=s", {opts=>["$opt-base64"], desttype=>"", type=>"s"}, {is_base64=>1},
260 );
261 } else {
262 199 ▶ my $t = ($type eq 'int' ? 's' : $type eq 'float' ? 's' : 's') .
Branch: 4 branches
BranchTrueFalseTotal
$type eq 'int' ? : 17 182 199
$type eq 'float' ? : 2 180 182
$isaos ? : 35 164 199
$ishos ? : 19 145 164
263 ($isaos ? '@' : $ishos ? '%' : '');
264 199 push @res, ("$opt=$t", {opts=>[$opt], desttype=>"", type=>$t}, undef);
265 }
266 }
267  
268 224 @res;
269 }
270  
271 79 ▶sub _args2opts {
Subroutine _args2opts: called CC=42 SCAR=38.1
272 79 my %args = @_;
273  
274 79 my $argprefix = $args{argprefix};
275 79 my $parent_args = $args{parent_args};
276 79 my $meta = $args{meta};
277 79 my $seen_opts = $args{seen_opts};
278 79 my $seen_common_opts = $args{seen_common_opts};
279 79 my $seen_func_opts = $args{seen_func_opts};
280 79 my $rargs = $args{rargs};
281 79 my $go_spec = $args{go_spec};
282 79 my $specmeta = $args{specmeta};
283  
284 79 ▶ my $args_prop = $meta->{args} // {};
Condition50%1/2
$meta->{"args"} // {}
l!l
790
MC/DC0%0/1
$meta->{"args"} // {}
$meta->{"args"}
Truth table50%1/2
A
A= $meta->{"args"}
Aresult
11
00
285  
286 79 for my $arg (keys %$args_prop) {
287 198 my $fqarg = "$argprefix$arg";
288 198 ▶ my $optlabel = "arg=$arg".($fqarg ne $arg ? ", fqarg=$fqarg":""); # written to %$seen_opts values
Branch: 1 branch
BranchTrueFalseTotal
$fqarg ne $arg ? : 8 190 198
289 198 my $arg_spec = $args_prop->{$arg};
290 0 ▶ next if grep { $_ eq 'hidden' || $_ eq 'hidden-cli' }
Condition0%0/3
$_ eq 'hidden' || $_ eq 'hidden-cli'
l!l&&r!l&&!r
000
MC/DC0%0/2
$_ eq 'hidden' || $_ eq 'hidden-cli'
$_ eq 'hidden' $_ eq 'hidden-cli'
Truth table0%0/3
A || B
A= $_ eq 'hidden'B= $_ eq 'hidden-cli'
ABresult
1X1
011
000
291 198 ▶ @{ $arg_spec->{tags} // [] };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "hidden" or $_ eq "hidden-cli";} @{$arg_spec->{"tags"} // [];} 0 198 198
Condition50%1/2
$arg_spec->{"tags"} // []
l!l
0198
MC/DC0%0/1
$arg_spec->{"tags"} // []
$arg_spec->{"tags"}
Truth table50%1/2
A
A= $arg_spec->{"tags"}
Aresult
11
00
292 198 ▶ my $sch = $arg_spec->{schema} // ['any', {}];
Condition50%1/2
$arg_spec->{"schema"} // ["any", {}]
l!l
1980
MC/DC0%0/1
$arg_spec->{"schema"} // ["any", {}]
$arg_spec->{"schema"}
Truth table50%1/2
A
A= $arg_spec->{"schema"}
Aresult
11
00
293 198 my ($is_simple, $is_array_of_simple, $is_hash_of_simple, $type, $clset, $eltype) =
294 _is_simple_or_array_of_simple_or_hash_of_simple($sch);
295  
296 # XXX normalization of 'of' clause should've been handled by sah itself
297 198 ▶ if ($type eq 'array' && $clset->{of}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "array" and $clset->{"of"}) 25 173 198
Condition100%3/3
$type eq "array" and $clset->{"of"}
!ll&&!rl&&r
1561725
MC/DC100%2/2
$type eq "array" and $clset->{"of"}
$type eq "array" $clset->{"of"}
Truth table100%3/3
A and B
A= $type eq "array"B= $clset->{"of"}
ABresult
0X0
100
111
298 25 $clset->{of} = normalize_schema($clset->{of});
299 }
300 198 my $opt = _arg2opt($fqarg);
301 198 ▶ if ($seen_opts->{$opt}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_opts->{$opt}) 3 195 198
302 3 my $i = 1;
303 3 my $opt2;
304 3 while (1) {
305 3 ▶ $opt2 = "$opt-arg" . ($i > 1 ? $i : '');
Branch: 1 branch
BranchTrueFalseTotal
$i > 1 ? : 0 3 3
306 3 ▶ last unless $seen_opts->{$opt2};
Branch: 1 branch
BranchTrueFalseTotal
unless $seen_opts->{$opt2} 3 0 3
307 0 $i++;
308 }
309 3 $opt = $opt2;
310 }
311  
312 198 my $stash = {};
313  
314 # why we use coderefs here? due to Getopt::Long's behavior. when
315 # @ARGV=qw() and go_spec is ('foo=s' => \$opts{foo}) then %opts will
316 # become (foo=>undef). but if go_spec is ('foo=s' => sub { $opts{foo} =
317 # $_[1] }) then %opts will become (), which is what we prefer, so we can
318 # later differentiate "unspecified" (exists($opts{foo}) == false) and
319 # "specified as undef" (exists($opts{foo}) == true but
320 # defined($opts{foo}) == false).
321  
322 76 ▶ my $handler = sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
323 76 my ($val, $val_set);
324  
325 # how many times have been called for this argument?
326 76 my $num_called = ++$stash->{called}{$arg};
327  
328 # hashify rargs till the end of the handler scope if it happens to
329 # be an array (this is the case when we want to fill values using
330 # element_meta).
331 76 my $rargs = do {
332 76 ▶ if (ref($rargs) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rargs eq 'ARRAY') { } 5 71 76
333 5 ▶ $rargs->[$num_called-1] //= {};
Condition100%2/2
$$rargs[$num_called - 1] //= {}
l!l
23
MC/DC100%1/1
$$rargs[$num_called - 1] //= {}
$$rargs[$num_called - 1]
Truth table100%2/2
A
A= $$rargs[$num_called - 1]
Aresult
11
00
334 5 $rargs->[$num_called-1];
335 } else {
336 71 $rargs;
337 }
338 };
339  
340 76 ▶ if ($is_array_of_simple) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_array_of_simple) { } 16 60 76
elsif ($is_hash_of_simple) { } 7 53 60
341 16 ▶ $rargs->{$arg} //= [];
Condition100%2/2
$rargs->{$arg} //= []
l!l
79
MC/DC100%1/1
$rargs->{$arg} //= []
$rargs->{$arg}
Truth table100%2/2
A
A= $rargs->{$arg}
Aresult
11
00
342 16 $val_set = 1; $val = $_[1];
343 16 push @{ $rargs->{$arg} }, $val;
344 } elsif ($is_hash_of_simple) {
345 7 ▶ $rargs->{$arg} //= {};
Condition100%2/2
$rargs->{$arg} //= {}
l!l
25
MC/DC100%1/1
$rargs->{$arg} //= {}
$rargs->{$arg}
Truth table100%2/2
A
A= $rargs->{$arg}
Aresult
11
00
346 7 $val_set = 1; $val = $_[2];
347 7 $rargs->{$arg}{$_[1]} = $val;
348 } else {
349 53 $val_set = 1; $val = $_[1];
350 53 $rargs->{$arg} = $val;
351 }
352 76 ▶ if ($val_set && $arg_spec->{cmdline_on_getopt}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val_set and $arg_spec->{"cmdline_on_getopt"}) 5 71 76
Condition66%2/3
$val_set and $arg_spec->{"cmdline_on_getopt"}
!ll&&!rl&&r
0715
MC/DC50%1/2
$val_set and $arg_spec->{"cmdline_on_getopt"}
$val_set $arg_spec->{"cmdline_on_getopt"}
Truth table66%2/3
A and B
A= $val_setB= $arg_spec->{"cmdline_on_getopt"}
ABresult
0X0
100
111
353 5 $arg_spec->{cmdline_on_getopt}->(
354 arg=>$arg, fqarg=>$fqarg, value=>$val, args=>$rargs,
355 opt=>$opt,
356 );
357 }
358 198 }; # handler
359  
360 198 my @triplets = _opt2ospec($opt, $sch, $arg_spec);
361 198 my $aliases_processed;
362 198 ▶ while (my ($ospec, $parsed, $extra) = splice @triplets, 0, 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@triplets, 0, 3)) 231 196 427
363 231 my $optlabel = $optlabel . ", spec=$ospec"; # note: redefine
364 231 ▶ $extra //= {};
Condition100%2/2
$extra //= {}
l!l
49182
MC/DC100%1/1
$extra //= {}
$extra
Truth table100%2/2
A
A= $extra
Aresult
11
00
365 231 ▶ if ($extra->{is_neg}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($extra->{'is_neg'}) { } 31 200 231
elsif (defined $extra->{'is_neg'}) { } 15 185 200
elsif ($extra->{'is_base64'}) { } 3 182 185
366 31 ▶ $go_spec->{$ospec} = sub { $handler->($_[0], 0) };
Subroutine __ANON__: called CC=1 SCAR=0.0
367 } elsif (defined $extra->{is_neg}) {
368 15 ▶ $go_spec->{$ospec} = sub { $handler->($_[0], 1) };
Subroutine __ANON__: called CC=1 SCAR=0.0
369 } elsif ($extra->{is_base64}) {
370 1 ▶ $go_spec->{$ospec} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
371 1 require MIME::Base64;
372 1 my $decoded = MIME::Base64::decode($_[1]);
373 1 $handler->($_[0], $decoded);
374 3 };
375 } else {
376 182 $go_spec->{$ospec} = $handler;
377 }
378  
379 231 $specmeta->{$ospec} = {arg=>$arg, fqarg=>$fqarg, parsed=>$parsed, %$extra};
380 231 for (@{ $parsed->{opts} }) {
381 231 $seen_opts->{$_} = $optlabel;
382 231 $seen_func_opts->{$_} = $fqarg;
383 }
384  
385 231 ▶ if ($parent_args->{per_arg_json} && !$is_simple) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_args->{"per_arg_json"} and not $is_simple) 8 223 231
Condition100%3/3
$parent_args->{"per_arg_json"} and not $is_simple
!ll&&!rl&&r
206178
MC/DC100%2/2
$parent_args->{"per_arg_json"} and not $is_simple
$parent_args->{"per_arg_json"} not $is_simple
Truth table100%3/3
A and not(B)
A= $parent_args->{"per_arg_json"}B= not $is_simple
ABresult
0X0
100
111
386 8 my $jopt = "$opt-json";
387 8 ▶ if ($seen_opts->{$jopt}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_opts->{$jopt}) { } 0 8 8
388 0 warn "Clash of option $jopt ($optlabel) vs existing ($seen_opts->{$jopt}), not added";
389 } else {
390 8 my $jospec = "$jopt=s";
391 8 my $parsed = {type=>"s", opts=>[$jopt]};
392 1 ▶ $go_spec->{$jospec} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
393 1 my ($success, $e, $decoded);
394 1 ($success, $e, $decoded) = _parse_json($_[1]);
395 1 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 1 0 1
396 1 $rargs->{$arg} = $decoded;
397 } else {
398 0 die "Invalid JSON in option --$jopt: $_[1]: $e";
399 }
400 8 };
401 8 $specmeta->{$jospec} = {arg=>$arg, fqarg=>$fqarg, is_json=>1, parsed=>$parsed, %$extra};
402 8 $seen_opts->{$jopt} = $optlabel;
403 8 $seen_func_opts->{$jopt} = $fqarg;
404 }
405 }
406 231 ▶ if ($parent_args->{per_arg_yaml} && !$is_simple) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_args->{"per_arg_yaml"} and not $is_simple) 8 223 231
Condition100%3/3
$parent_args->{"per_arg_yaml"} and not $is_simple
!ll&&!rl&&r
206178
MC/DC100%2/2
$parent_args->{"per_arg_yaml"} and not $is_simple
$parent_args->{"per_arg_yaml"} not $is_simple
Truth table100%3/3
A and not(B)
A= $parent_args->{"per_arg_yaml"}B= not $is_simple
ABresult
0X0
100
111
407 8 my $yopt = "$opt-yaml";
408 8 ▶ if ($seen_opts->{$yopt}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_opts->{$yopt}) { } 0 8 8
409 0 warn "Clash of option: $yopt ($optlabel) vs existing ($seen_opts->{$yopt}), not added";
410 } else {
411 8 my $yospec = "$yopt=s";
412 8 my $parsed = {type=>"s", opts=>[$yopt]};
413 1 ▶ $go_spec->{$yospec} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
414 1 my ($success, $e, $decoded);
415 1 ($success, $e, $decoded) = _parse_yaml($_[1]);
416 1 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 1 0 1
417 1 $rargs->{$arg} = $decoded;
418 } else {
419 0 die "Invalid YAML in option --$yopt: $_[1]: $e";
420 }
421 8 };
422 8 $specmeta->{$yospec} = {arg=>$arg, fqarg=>$fqarg, is_yaml=>1, parsed=>$parsed, %$extra};
423 8 $seen_opts->{$yopt} = $optlabel;
424 8 $seen_func_opts->{$yopt} = $fqarg;
425 }
426 }
427  
428 # parse argv_aliases
429 231 ▶ if ($arg_spec->{cmdline_aliases} && !$aliases_processed++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_spec->{"cmdline_aliases"} and not $aliases_processed++) 31 200 231
Condition100%3/3
$arg_spec->{"cmdline_aliases"} and not $aliases_processed++
!ll&&!rl&&r
192831
MC/DC100%2/2
$arg_spec->{"cmdline_aliases"} and not $aliases_processed++
$arg_spec->{"cmdline_aliases"} not $aliases_processed++
Truth table100%3/3
A and not(B)
A= $arg_spec->{"cmdline_aliases"}B= not $aliases_processed++
ABresult
0X0
100
111
430 31 for my $al (keys %{$arg_spec->{cmdline_aliases}}) {
431 35 my $alspec = $arg_spec->{cmdline_aliases}{$al};
432 my $alsch = $alspec->{schema} //
433 35 ▶ $alspec->{is_flag} ? [bool=>{req=>1,is=>1}] : $sch;
Branch: 1 branch
BranchTrueFalseTotal
$alspec->{'schema'} // $alspec->{'is_flag'} ? : 5 30 35
Condition66%2/3
$alspec->{"schema"} // $alspec->{"is_flag"}
l!l&&r!l&&!r
5030
MC/DC50%1/2
$alspec->{"schema"} // $alspec->{"is_flag"}
$alspec->{"schema"} $alspec->{"is_flag"}
Truth table66%2/3
A // B
A= $alspec->{"schema"}B= $alspec->{"is_flag"}
ABresult
1X1
011
000
434 35 my $altype = $alsch->[0];
435 35 my $alopt = _arg2opt("$argprefix$al");
436 35 ▶ if ($seen_opts->{$alopt}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_opts->{$alopt}) 0 35 35
437 0 warn "Clash of cmdline_alias option $al ($optlabel) vs existing ($seen_opts->{$alopt})";
438 0 next;
439 }
440 35 my $alcode = $alspec->{code};
441 35 my $alospec;
442 my $parsed;
443 35 ▶ if ($alcode && $alsch->[0] eq 'bool') {
Branch: 1 branch
BranchTrueFalseTotal
if ($alcode and $alsch->[0] eq 'bool') { } 9 26 35
Condition100%3/3
$alcode and $alsch->[0] eq "bool"
!ll&&!rl&&r
2339
MC/DC100%2/2
$alcode and $alsch->[0] eq "bool"
$alcode $alsch->[0] eq "bool"
Truth table100%3/3
A and B
A= $alcodeB= $alsch->[0] eq "bool"
ABresult
0X0
100
111
444 # bool --alias doesn't get --noalias if has code
445 9 $alospec = $alopt; # instead of "$alopt!"
446 9 $parsed = {opts=>[$alopt]};
447 } else {
448 26 ($alospec, $parsed) = _opt2ospec($alopt, $alsch);
449 }
450  
451 35 ▶ if ($alcode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alcode) { } 12 23 35
452 12 ▶ if ($alcode eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($alcode eq "CODE") 3 9 12
453 3 ▶ if ($parent_args->{ignore_converted_code}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_args->{'ignore_converted_code'}) { } 1 2 3
454 1 ▶ $alcode = sub {};
Subroutine __ANON__: called CC=1 SCAR=0.0
455 } else {
456 return [
457 2 501,
458 join("",
459 "Code in cmdline_aliases for arg $fqarg ",
460 "got converted into string, probably ",
461 "because of JSON/YAML transport"),
462 ];
463 }
464 }
465 # alias handler
466 3 ▶ $go_spec->{$alospec} = sub {
Subroutine __ANON__: called CC=3 SCAR=13.5
467  
468 # do the same like in arg handler
469 3 my $num_called = ++$stash->{called}{$arg};
470 3 my $rargs = do {
471 3 ▶ if (ref($rargs) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rargs eq 'ARRAY') { } 0 3 3
472 0 ▶ $rargs->[$num_called-1] //= {};
Condition0%0/2
$$rargs[$num_called - 1] //= {}
l!l
00
MC/DC0%0/1
$$rargs[$num_called - 1] //= {}
$$rargs[$num_called - 1]
Truth table0%0/2
A
A= $$rargs[$num_called - 1]
Aresult
11
00
473 0 $rargs->[$num_called-1];
474 } else {
475 3 $rargs;
476 }
477 };
478  
479 3 $alcode->($rargs, $_[1]);
480 10 };
481 } else {
482 23 $go_spec->{$alospec} = $handler;
483 }
484 $specmeta->{$alospec} = {
485 33 ▶ (summary => $alspec->{summary}) x !!defined($alspec->{summary}),
Branch: 1 branch
BranchTrueFalseTotal
$alcode ? : 10 23 33
486 alias => $al,
487 is_alias => 1,
488 alias_for => $ospec,
489 arg => $arg,
490 fqarg => $fqarg,
491 is_code => $alcode ? 1:0,
492 parsed => $parsed,
493 %$extra,
494 };
495 33 ▶ push @{$specmeta->{$ospec}{($alcode ? '':'non').'code_aliases'}},
Branch: 1 branch
BranchTrueFalseTotal
$alcode ? : 10 23 33
496 $alospec;
497 33 $seen_opts->{$alopt} = $optlabel;
498 33 $seen_func_opts->{$alopt} = $fqarg;
499 }
500 } # cmdline_aliases
501  
502 # submetadata
503 229 ▶ if ($arg_spec->{meta}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_spec->{"meta"}) 2 227 229
504 2 $rargs->{$arg} = {};
505 my $res = _args2opts(
506 %args,
507 argprefix => "$argprefix$arg\::",
508 meta => $arg_spec->{meta},
509 2 rargs => $rargs->{$arg},
510 );
511 2 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 2 2
512 }
513  
514 # element submetadata
515 229 ▶ if ($arg_spec->{element_meta}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_spec->{"element_meta"}) 3 226 229
516 3 $rargs->{$arg} = [];
517 my $res = _args2opts(
518 %args,
519 argprefix => "$argprefix$arg\::",
520 meta => $arg_spec->{element_meta},
521 3 rargs => $rargs->{$arg},
522 );
523 3 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 3 3
524 }
525 } # for ospec triplet
526  
527 } # for arg
528  
529 77 undef;
530 }
531  
532 $SPEC{gen_getopt_long_spec_from_meta} = {
533 4 v => 1.1,
534 summary => 'Generate Getopt::Long spec from Rinci function metadata',
535 description => <<'_',
536  
537 This routine will produce a <pm:Getopt::Long> specification from Rinci function
538 metadata, as well as some more data structure in the result metadata to help
539 producing a command-line help/usage message.
540  
541 Function arguments will be mapped to command-line options with the same name,
542 with non-alphanumeric characters changed to `-` (`-` is preferred over `_`
543 because it lets user avoid pressing Shift on popular keyboards). For example:
544 `file_size` becomes `file-size`, `file_size.max` becomes `file-size-max`. If
545 function argument option name clashes with command-line option or another
546 existing option, it will be renamed to `NAME-arg` (or `NAME-arg2` and so on).
547 For example: `help` will become `help-arg` (if `common_opts` contains `help`,
548 that is).
549  
550 Each command-line alias (`cmdline_aliases` property) in the argument
551 specification will also be added as command-line option, except if it clashes
552 with an existing option, in which case this function will warn and skip adding
553 the alias. For more information about `cmdline_aliases`, see `Rinci::function`.
554  
555 For arguments with type of `bool`, Getopt::Long will by default also
556 automatically recognize `--noNAME` or `--no-NAME` in addition to `--name`. So
557 this function will also check those names for clashes.
558  
559 For arguments with type array of simple scalar, `--NAME` can be specified more
560 than once to append to the array.
561  
562 If `per_arg_json` setting is active, and argument's schema is not a "required
563 simple scalar" (e.g. an array, or a nullable string), then `--NAME-json` will
564 also be added to let users input undef (through `--NAME-json null`) or a
565 non-scalar value (e.g. `--NAME-json '[1,2,3]'`). If this name conflicts with
566 another existing option, a warning will be displayed and the option will not be
567 added.
568  
569 If `per_arg_yaml` setting is active, and argument's schema is not a "required
570 simple scalar" (e.g. an array, or a nullable string), then `--NAME-yaml` will
571 also be added to let users input undef (through `--NAME-yaml '~'`) or a
572 non-scalar value (e.g. `--NAME-yaml '[foo, bar]'`). If this name conflicts with
573 another existing option, a warning will be displayed and the option will not be
574 added. YAML can express a larger set of values, e.g. binary data, circular
575 references, etc.
576  
577 Will produce a hash (Getopt::Long spec), with `func.specmeta`, `func.opts`,
578 `func.common_opts`, `func.func_opts` that contain extra information
579 (`func.specmeta` is a hash of getopt spec name and a hash of extra information
580 while `func.*opts` lists all used option names).
581  
582 _
583 args => {
584 meta => {
585 summary => 'Rinci function metadata',
586 schema => 'hash*',
587 req => 1,
588 },
589 meta_is_normalized => {
590 schema => 'bool*',
591 },
592 args => {
593 summary => 'Reference to hash which will store the result',
594 schema => 'hash*',
595 },
596 common_opts => {
597 summary => 'Common options',
598 description => <<'_',
599  
600 A hash where the values are hashes containing these keys: `getopt` (Getopt::Long
601 option specification), `handler` (Getopt::Long handler). Will be passed to
602 `get_args_from_argv()`. Example:
603  
604 {
605 help => {
606 getopt => 'help|h|?',
607 handler => sub { ... },
608 summary => 'Display help and exit',
609 },
610 version => {
611 getopt => 'version|v',
612 handler => sub { ... },
613 summary => 'Display version and exit',
614 },
615 }
616  
617 _
618 schema => ['hash*'],
619 },
620 per_arg_json => {
621 summary => 'Whether to add --NAME-json for non-simple arguments',
622 schema => 'bool',
623 default => 0,
624 description => <<'_',
625  
626 Will also interpret command-line arguments as JSON if assigned to function
627 arguments, if arguments' schema is not simple scalar.
628  
629 _
630 },
631 per_arg_yaml => {
632 summary => 'Whether to add --NAME-yaml for non-simple arguments',
633 schema => 'bool',
634 default => 0,
635 description => <<'_',
636  
637 Will also interpret command-line arguments as YAML if assigned to function
638 arguments, if arguments' schema is not simple scalar.
639  
640 _
641 },
642 ignore_converted_code => {
643 summary => 'Whether to ignore coderefs converted to string',
644 schema => 'bool',
645 default => 0,
646 description => <<'_',
647  
648 Across network through JSON encoding, coderef in metadata (e.g. in
649 `cmdline_aliases` property) usually gets converted to string `CODE`. In some
650 cases, like for tab completion, this is pretty harmless so you can turn this
651 option on. For example, in the case of `cmdline_aliases`, the effect is just
652 that command-line aliases code are not getting executed, but this is usually
653 okay.
654  
655 _
656 },
657 },
658 };
659 74 ▶sub gen_getopt_long_spec_from_meta {
Subroutine gen_getopt_long_spec_from_meta: called CC=23 SCAR=31.4
Pod documented
660 74 my %fargs = @_;
661  
662 74 ▶ my $meta = $fargs{meta} or return [400, "Please specify meta"];
Branch: 1 branch
BranchTrueFalseTotal
my $meta = $fargs{"meta"} or return [400, "Please specify meta"] 74 0 74
663 74 ▶ unless ($fargs{meta_is_normalized}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fargs{"meta_is_normalized"}) 1 73 74
664 1 require Perinci::Sub::Normalize;
665 1 $meta = Perinci::Sub::Normalize::normalize_function_metadata($meta);
666 }
667 74 ▶ my $co = $fargs{common_opts} // {};
Condition50%1/2
$fargs{"common_opts"} // {}
l!l
740
MC/DC0%0/1
$fargs{"common_opts"} // {}
$fargs{"common_opts"}
Truth table50%1/2
A
A= $fargs{"common_opts"}
Aresult
11
00
668 74 ▶ my $per_arg_yaml = $fargs{per_arg_yaml} // 0;
Condition50%1/2
$fargs{"per_arg_yaml"} // 0
l!l
740
MC/DC0%0/1
$fargs{"per_arg_yaml"} // 0
$fargs{"per_arg_yaml"}
Truth table50%1/2
A
A= $fargs{"per_arg_yaml"}
Aresult
11
00
669 74 ▶ my $per_arg_json = $fargs{per_arg_json} // 0;
Condition50%1/2
$fargs{"per_arg_json"} // 0
l!l
740
MC/DC0%0/1
$fargs{"per_arg_json"} // 0
$fargs{"per_arg_json"}
Truth table50%1/2
A
A= $fargs{"per_arg_json"}
Aresult
11
00
670 74 my $ignore_converted_code = $fargs{ignore_converted_code};
671 74 ▶ my $rargs = $fargs{args} // {};
Condition100%2/2
$fargs{"args"} // {}
l!l
731
MC/DC100%1/1
$fargs{"args"} // {}
$fargs{"args"}
Truth table100%2/2
A
A= $fargs{"args"}
Aresult
11
00
672  
673 74 my %go_spec;
674 my %specmeta; # key = option spec, val = hash of extra info
675 74 my %seen_opts;
676 74 my %seen_common_opts;
677 74 my %seen_func_opts;
678  
679 74 for my $k (keys %$co) {
680 9 my $v = $co->{$k};
681 9 my $ospec = $v->{getopt};
682 9 my $handler = $v->{handler};
683 9 ▶ my $res = parse_getopt_long_opt_spec($ospec)
Branch: 1 branch
BranchTrueFalseTotal
my $res = parse_getopt_long_opt_spec($ospec) or return [400, "Can't parse common opt spec '${ospec}'"] 9 0 9
684 or return [400, "Can't parse common opt spec '$ospec'"];
685 9 $go_spec{$ospec} = $handler;
686 9 $specmeta{$ospec} = {common_opt=>$k, arg=>undef, parsed=>$res};
687 9 for (@{ $res->{opts} }) {
688 12 ▶ return [412, "Clash of common opt '$_' ($seen_opts{$_})"] if $seen_opts{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $seen_opts{$_} 0 12 12
689 12 $seen_opts{$_} = "common option $k";
690 12 $seen_common_opts{$_} = $ospec;
691 12 ▶ if ($res->{is_neg}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"is_neg"}) 1 11 12
692 1 $seen_opts{"no$_"} = "common option $k, negative form";
693 1 $seen_common_opts{"no$_"} = $ospec;
694 1 $seen_opts{"no-$_"} = "common option $k, negative form";
695 1 $seen_common_opts{"no-$_"} = $ospec;
696 }
697 }
698 }
699  
700 74 my $res = _args2opts(
701 argprefix => "",
702 parent_args => \%fargs,
703 meta => $meta,
704 seen_opts => \%seen_opts,
705 seen_common_opts => \%seen_common_opts,
706 seen_func_opts => \%seen_func_opts,
707 rargs => $rargs,
708 go_spec => \%go_spec,
709 specmeta => \%specmeta,
710 );
711 74 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 2 72 74
712  
713 72 ▶ my $opts = [sort(map {length($_)>1 ? "--$_":"-$_"} keys %seen_opts)];
Branch: 1 branch
BranchTrueFalseTotal
length $_ > 1 ? : 233 59 292
714 72 ▶ my $common_opts = [sort(map {length($_)>1 ? "--$_":"-$_"} keys %seen_common_opts)];
Branch: 1 branch
BranchTrueFalseTotal
length $_ > 1 ? : 11 3 14
715 72 ▶ my $func_opts = [sort(map {length($_)>1 ? "--$_":"-$_"} keys %seen_func_opts)];
Branch: 1 branch
BranchTrueFalseTotal
length $_ > 1 ? : 222 56 278
716 72 my $opts_by_common = {};
717 72 for my $k (keys %$co) {
718 9 my $v = $co->{$k};
719 9 my $ospec = $v->{getopt};
720 9 my @opts;
721 9 for (keys %seen_common_opts) {
722 58 ▶ next unless $seen_common_opts{$_} eq $ospec;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen_common_opts{$_} eq $ospec 44 14 58
723 14 ▶ push @opts, (length($_)>1 ? "--$_":"-$_");
Branch: 1 branch
BranchTrueFalseTotal
length $_ > 1 ? : 11 3 14
724 }
725 9 $opts_by_common->{$ospec} = [sort @opts];
726 }
727  
728 72 my $opts_by_arg = {};
729 72 for (keys %seen_func_opts) {
730 278 my $fqarg = $seen_func_opts{$_};
731 278 ▶ push @{ $opts_by_arg->{$fqarg} }, length($_)>1 ? "--$_":"-$_";
Branch: 1 branch
BranchTrueFalseTotal
length $_ > 1 ? : 222 56 278
732 }
733 72 for (keys %$opts_by_arg) {
734 196 $opts_by_arg->{$_} = [sort @{ $opts_by_arg->{$_} }];
735 }
736  
737 72 [200, "OK", \%go_spec,
738 {
739 "func.specmeta" => \%specmeta,
740 "func.opts" => $opts,
741 "func.common_opts" => $common_opts,
742 "func.func_opts" => $func_opts,
743 "func.opts_by_arg" => $opts_by_arg,
744 "func.opts_by_common" => $opts_by_common,
745 }];
746 }
747  
748 $SPEC{get_args_from_argv} = {
749 4 v => 1.1,
750 summary => 'Get subroutine arguments (%args) from command-line arguments '.
751 '(@ARGV)',
752 description => <<'_',
753  
754 Using information in Rinci function metadata's `args` property, parse command
755 line arguments `@argv` into hash `%args`, suitable for passing into subroutines.
756  
757 Currently uses <pm:Getopt::Long>'s `GetOptions` to do the parsing.
758  
759 As with GetOptions, this function modifies its `argv` argument, so you might
760 want to copy the original `argv` first (or pass a copy instead) if you want to
761 preserve the original.
762  
763 See also: gen_getopt_long_spec_from_meta() which is the routine that generates
764 the specification.
765  
766 _
767 args => {
768 argv => {
769 schema => ['array*' => {
770 of => 'str*',
771 }],
772 description => 'If not specified, defaults to @ARGV',
773 },
774 args => {
775 summary => 'Specify input args, with some arguments preset',
776 schema => ['hash'],
777 },
778 meta => {
779 schema => ['hash*' => {}],
780 req => 1,
781 },
782 meta_is_normalized => {
783 summary => 'Can be set to 1 if your metadata is normalized, '.
784 'to avoid duplicate effort',
785 schema => 'bool',
786 default => 0,
787 },
788 strict => {
789 schema => ['bool' => {default=>1}],
790 summary => 'Strict mode',
791 description => <<'_',
792  
793 If set to 0, will still return parsed argv even if there are parsing errors
794 (reported by Getopt::Long). If set to 1 (the default), will die upon error.
795  
796 Normally you would want to use strict mode, for more error checking. Setting off
797 strict is used by, for example, Perinci::Sub::Complete during completion where
798 the command-line might still be incomplete.
799  
800 Should probably be named `ignore_errors` or `allow_unknown_options`. :-)
801  
802 _
803 },
804 per_arg_yaml => {
805 schema => ['bool' => {default=>0}],
806 summary => 'Whether to recognize --ARGNAME-yaml',
807 description => <<'_',
808  
809 This is useful for example if you want to specify a value which is not
810 expressible from the command-line, like 'undef'.
811  
812 % script.pl --name-yaml '~'
813  
814 See also: per_arg_json. You should enable just one instead of turning on both.
815  
816 _
817 },
818 per_arg_json => {
819 schema => ['bool' => {default=>0}],
820 summary => 'Whether to recognize --ARGNAME-json',
821 description => <<'_',
822  
823 This is useful for example if you want to specify a value which is not
824 expressible from the command-line, like 'undef'.
825  
826 % script.pl --name-json 'null'
827  
828 But every other string will need to be quoted:
829  
830 % script.pl --name-json '"foo"'
831  
832 See also: per_arg_yaml. You should enable just one instead of turning on both.
833  
834 _
835 },
836 common_opts => {
837 summary => 'Common options',
838 description => <<'_',
839  
840 A hash where the values are hashes containing these keys: `getopt` (Getopt::Long
841 option specification), `handler` (Getopt::Long handler). Will be passed to
842 `get_args_from_argv()`. Example:
843  
844 {
845 help => {
846 getopt => 'help|h|?',
847 handler => sub { ... },
848 summary => 'Display help and exit',
849 },
850 version => {
851 getopt => 'version|v',
852 handler => sub { ... },
853 summary => 'Display version and exit',
854 },
855 }
856  
857 _
858 schema => ['hash*'],
859 },
860 allow_extra_elems => {
861 schema => ['bool' => {default=>0}],
862 summary => 'Allow extra/unassigned elements in argv',
863 description => <<'_',
864  
865 If set to 1, then if there are array elements unassigned to one of the
866 arguments, instead of generating an error, this function will just ignore them.
867  
868 This option will be passed to Perinci::Sub::GetArgs::Array's allow_extra_elems.
869  
870 _
871 },
872 on_missing_required_args => {
873 schema => 'code',
874 summary => 'Execute code when there is missing required args',
875 description => <<'_',
876  
877 This can be used to give a chance to supply argument value from other sources if
878 not specified by command-line options. Perinci::CmdLine, for example, uses this
879 hook to supply value from STDIN or file contents (if argument has `cmdline_src`
880 specification key set).
881  
882 This hook will be called for each missing argument. It will be supplied hash
883 arguments: (arg => $the_missing_argument_name, args =>
884 $the_resulting_args_so_far, spec => $the_arg_spec).
885  
886 The hook can return true if it succeeds in making the missing situation
887 resolved. In this case, this function will not report the argument as missing.
888  
889 _
890 },
891 ignore_converted_code => {
892 summary => 'Whether to ignore coderefs converted to string',
893 schema => 'bool',
894 default => 0,
895 description => <<'_',
896  
897 Across network through JSON encoding, coderef in metadata (e.g. in
898 `cmdline_aliases` property) usually gets converted to string `CODE`. In some
899 cases, like for tab completion, this is harmless so you can turn this option on.
900  
901 _
902 },
903 ggls_res => {
904 summary => 'Full result from gen_getopt_long_spec_from_meta()',
905 schema => 'array*', # XXX envres
906 description => <<'_',
907  
908 If you already call `gen_getopt_long_spec_from_meta()`, you can pass the _full_ enveloped result
909 here, to avoid calculating twice.
910  
911 _
912 tags => ['category:optimization'],
913 },
914 },
915 result => {
916 description => <<'_',
917  
918 Error codes:
919  
920 * 400 - Error in Getopt::Long option specification, e.g. in common_opts.
921  
922 * 500 - failure in GetOptions, meaning argv is not valid according to metadata
923 specification (only if 'strict' mode is enabled).
924  
925 * 501 - coderef in cmdline_aliases got converted into a string, probably because
926 the metadata was transported (e.g. through Riap::HTTP/Riap::Simple).
927  
928 _
929 },
930 };
931 73 ▶sub get_args_from_argv {
Subroutine get_args_from_argv: called CC=61 SCAR=42.9
Pod documented
932 73 require Getopt::Long;
933  
934 73 my %fargs = @_;
935 73 ▶ my $argv = $fargs{argv} // \@ARGV;
Condition100%2/2
$fargs{"argv"} // \@ARGV
l!l
721
MC/DC100%1/1
$fargs{"argv"} // \@ARGV
$fargs{"argv"}
Truth table100%2/2
A
A= $fargs{"argv"}
Aresult
11
00
936 73 ▶ my $meta = $fargs{meta} or return [400, "Please specify meta"];
Branch: 1 branch
BranchTrueFalseTotal
my $meta = $fargs{"meta"} or return [400, "Please specify meta"] 73 0 73
937 73 ▶ unless ($fargs{meta_is_normalized}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fargs{"meta_is_normalized"}) 73 0 73
938 73 require Perinci::Sub::Normalize;
939 73 $meta = Perinci::Sub::Normalize::normalize_function_metadata($meta);
940 }
941 73 ▶ my $strict = $fargs{strict} // 1;
Condition100%2/2
$fargs{"strict"} // 1
l!l
271
MC/DC100%1/1
$fargs{"strict"} // 1
$fargs{"strict"}
Truth table100%2/2
A
A= $fargs{"strict"}
Aresult
11
00
942 73 ▶ my $common_opts = $fargs{common_opts} // {};
Condition100%2/2
$fargs{"common_opts"} // {}
l!l
271
MC/DC100%1/1
$fargs{"common_opts"} // {}
$fargs{"common_opts"}
Truth table100%2/2
A
A= $fargs{"common_opts"}
Aresult
11
00
943 73 ▶ my $per_arg_yaml = $fargs{per_arg_yaml} // 0;
Condition100%2/2
$fargs{"per_arg_yaml"} // 0
l!l
370
MC/DC100%1/1
$fargs{"per_arg_yaml"} // 0
$fargs{"per_arg_yaml"}
Truth table100%2/2
A
A= $fargs{"per_arg_yaml"}
Aresult
11
00
944 73 ▶ my $per_arg_json = $fargs{per_arg_json} // 0;
Condition100%2/2
$fargs{"per_arg_json"} // 0
l!l
370
MC/DC100%1/1
$fargs{"per_arg_json"} // 0
$fargs{"per_arg_json"}
Truth table100%2/2
A
A= $fargs{"per_arg_json"}
Aresult
11
00
945 73 ▶ my $allow_extra_elems = $fargs{allow_extra_elems} // 0;
Condition100%2/2
$fargs{"allow_extra_elems"} // 0
l!l
172
MC/DC100%1/1
$fargs{"allow_extra_elems"} // 0
$fargs{"allow_extra_elems"}
Truth table100%2/2
A
A= $fargs{"allow_extra_elems"}
Aresult
11
00
946 73 my $on_missing = $fargs{on_missing_required_args};
947 73 my $ignore_converted_code = $fargs{ignore_converted_code};
948 #$log->tracef("-> get_args_from_argv(), argv=%s", $argv);
949  
950 # to store the resulting args
951 73 ▶ my $rargs = $fargs{args} // {};
Condition100%2/2
$fargs{"args"} // {}
l!l
370
MC/DC100%1/1
$fargs{"args"} // {}
$fargs{"args"}
Truth table100%2/2
A
A= $fargs{"args"}
Aresult
11
00
952  
953 # 1. first we generate Getopt::Long spec
954 73 ▶ my $genres = $fargs{ggls_res} // gen_getopt_long_spec_from_meta(
Condition33%1/3
$fargs{"ggls_res"} // gen_getopt_long_spec_from_meta("meta", $meta, "meta_is_normalized", 1, "args", $rargs, "common_opts", $common_opts, "per_arg_json", $per_arg_json, "per_arg_yaml", $per_arg_yaml, "ignore_converted_code", $ignore_converted_code)
l!l&&r!l&&!r
0730
MC/DC0%0/2
$fargs{"ggls_res"} // gen_getopt_long_spec_from_meta("meta", $meta, "meta_is_normalized", 1, "args", $rargs, "common_opts", $common_opts, "per_arg_json", $per_arg_json, "per_arg_yaml", $per_arg_yaml, "ignore_converted_code", $ignore_converted_code)
$fargs{"ggls_res"} gen_getopt_long_spec_from_meta("meta", $meta, "meta_is_normalized", 1, "args", $rargs, "common_opts", $common_opts, "per_arg_json", $per_arg_json, "per_arg_yaml", $per_arg_yaml, "ignore_converted_code", $ignore_converted_code)
Truth table33%1/3
A // B
A= $fargs{"ggls_res"}B= gen_getopt_long_spec_from_meta("meta", $meta, "meta_is_normalized", 1, "args", $rargs, "common_opts", $common_opts, "per_arg_json", $per_arg_json, "per_arg_yaml", $per_arg_yaml, "ignore_converted_code", $ignore_converted_code)
ABresult
1X1
011
000
955 meta => $meta, meta_is_normalized => 1,
956 args => $rargs,
957 common_opts => $common_opts,
958 per_arg_json => $per_arg_json,
959 per_arg_yaml => $per_arg_yaml,
960 ignore_converted_code => $ignore_converted_code,
961 );
962 73 ▶ return err($genres->[0], "Can't generate Getopt::Long spec", $genres)
Branch: 1 branch
BranchTrueFalseTotal
if $genres->[0] != 200 2 71 73
963 if $genres->[0] != 200;
964 71 my $go_spec = $genres->[2];
965  
966 # 2. then we run GetOptions to fill $rargs from command-line opts
967 #$log->tracef("GetOptions spec: %s", \@go_spec);
968 {
969 71 ▶ local $SIG{__WARN__} = sub{} if !$strict;
Subroutine __ANON__: not called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless $strict 1 70 71
970 71 ▶ my $old_go_conf = Getopt::Long::Configure(
Branch: 1 branch
BranchTrueFalseTotal
$strict ? : 70 1 71
971 $strict ? "no_pass_through" : "pass_through",
972 "no_ignore_case", "permute", "no_getopt_compat", "gnu_compat", "bundling");
973 71 my $res = Getopt::Long::GetOptionsFromArray($argv, %$go_spec);
974 71 Getopt::Long::Configure($old_go_conf);
975 71 ▶ unless ($res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 8 63 71
976 8 ▶ return [500, "GetOptions failed"] if $strict;
Branch: 1 branch
BranchTrueFalseTotal
if $strict 8 0 8
977 }
978 }
979  
980 # 3. then we try to fill $rargs from remaining command-line arguments (for
981 # args which have 'pos' spec specified)
982  
983 63 my $args_prop = $meta->{args};
984  
985 63 ▶ if (@$argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$argv) 13 50 63
986 13 my $res = get_args_from_array(
987 array=>$argv, meta => $meta,
988 meta_is_normalized => 1,
989 allow_extra_elems => $allow_extra_elems,
990 );
991 13 ▶ if ($res->[0] != 200 && $strict) {
Branch: 3 branches
BranchTrueFalseTotal
if ($res->[0] != 200 and $strict) { } 2 11 13
elsif ($strict and $res->[0] != 200) { } 0 11 11
elsif ($res->[0] == 200) { } 10 1 11
Condition100%3/3
$res->[0] != 200 and $strict
!ll&&!rl&&r
1012
MC/DC100%2/2
$res->[0] != 200 and $strict
$res->[0] != 200 $strict
Truth table100%3/3
A and B
A= $res->[0] != 200B= $strict
ABresult
0X0
100
111
992 2 return err(500, "Get args from array failed", $res);
993 ▶ } elsif ($strict && $res->[0] != 200) {
Condition66%2/3
$strict and $res->[0] != 200
!ll&&!rl&&r
1100
MC/DC0%0/2
$strict and $res->[0] != 200
$strict $res->[0] != 200
Truth table66%2/3
A and B
A= $strictB= $res->[0] != 200
ABresult
0X0
100
111
994 0 return err("Can't get args from argv", $res);
995 } elsif ($res->[0] == 200) {
996 10 my $pos_args = $res->[2];
997 10 for my $name (keys %$pos_args) {
998 11 my $arg_spec = $args_prop->{$name};
999 11 my $val = $pos_args->{$name};
1000 11 ▶ if (exists $rargs->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rargs->{$name}) 2 9 11
1001 return [400, "You specified option --$name but also ".
1002 2 ▶ "argument #".$arg_spec->{pos}] if $strict;
Branch: 1 branch
BranchTrueFalseTotal
if $strict 2 0 2
1003 }
1004 my ($is_simple, $is_array_of_simple, $is_hash_of_simple, $type, $clset, $eltype) =
1005 9 _is_simple_or_array_of_simple_or_hash_of_simple($arg_spec->{schema});
1006  
1007 9 ▶ if (($arg_spec->{slurpy} // $arg_spec->{greedy}) && ref($val) eq 'ARRAY' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_spec->{"slurpy"} // $arg_spec->{"greedy"} and ref $val eq "ARRAY" and not $is_array_of_simple and not $is_hash_of_simple) 1 8 9
Condition75%9/12
$arg_spec->{"slurpy"} // $arg_spec->{"greedy"} and ref $val eq "ARRAY" and not $is_array_of_simple and not $is_hash_of_simple
!ll&&!rl&&r
801
$arg_spec->{"slurpy"} // $arg_spec->{"greedy"} and ref $val eq "ARRAY" and not $is_array_of_simple
!ll&&!rl&&r
621
$arg_spec->{"slurpy"} // $arg_spec->{"greedy"} and ref $val eq "ARRAY"
!ll&&!rl&&r
603
$arg_spec->{"slurpy"} // $arg_spec->{"greedy"}
l!l&&r!l&&!r
306
MC/DC40%2/5
$arg_spec->{"slurpy"} // $arg_spec->{"greedy"} and ref $val eq "ARRAY" and not $is_array_of_simple and not $is_hash_of_simple
$arg_spec->{"slurpy"} $arg_spec->{"greedy"} ref $val eq "ARRAY" not $is_array_of_simple not $is_hash_of_simple
Truth table33%3/9
A // B and C and not(D) and not(E)
A= $arg_spec->{"slurpy"}B= $arg_spec->{"greedy"}C= ref $val eq "ARRAY"D= not $is_array_of_simpleE= not $is_hash_of_simple
ABCDEresult
00XXX0
1X0XX0
010XX0
1X10X0
0110X0
1X1100
011100
1X1111
011111
1008 !$is_array_of_simple && !$is_hash_of_simple) {
1009 1 my $i = 0;
1010 1 for (@$val) {
1011 TRY_PARSING_AS_JSON_YAML:
1012 {
1013 1 my ($success, $e, $decoded);
1014 1 ▶ if ($per_arg_json) {
Branch: 1 branch
BranchTrueFalseTotal
if ($per_arg_json) 1 0 1
1015 1 ($success, $e, $decoded) = _parse_json($_);
1016 1 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 0 1 1
1017 0 $_ = $decoded;
1018 0 last TRY_PARSING_AS_JSON_YAML;
1019 } else {
1020 #warn "Failed trying to parse argv #$i as JSON: $e";
1021 }
1022 }
1023 1 ▶ if ($per_arg_yaml) {
Branch: 1 branch
BranchTrueFalseTotal
if ($per_arg_yaml) 1 0 1
1024 1 ($success, $e, $decoded) = _parse_yaml($_);
1025 1 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 1 0 1
1026 1 $_ = $decoded;
1027 1 last TRY_PARSING_AS_JSON_YAML;
1028 } else {
1029 #warn "Failed trying to parse argv #$i as YAML: $e";
1030 }
1031 }
1032 }
1033 1 $i++;
1034 }
1035 }
1036 9 ▶ if (!($arg_spec->{slurpy} // $arg_spec->{greedy}) && !$is_simple) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg_spec->{"slurpy"} // $arg_spec->{"greedy"} or $is_simple) 1 8 9
Condition83%5/6
$arg_spec->{"slurpy"} // $arg_spec->{"greedy"} or $is_simple
l!l&&r!l&&!r
351
$arg_spec->{"slurpy"} // $arg_spec->{"greedy"}
l!l&&r!l&&!r
306
MC/DC66%2/3
$arg_spec->{"slurpy"} // $arg_spec->{"greedy"} or $is_simple
$arg_spec->{"slurpy"} $arg_spec->{"greedy"} $is_simple
Truth table75%3/4
A // B or C
A= $arg_spec->{"slurpy"}B= $arg_spec->{"greedy"}C= $is_simple
ABCresult
1XX1
01X1
0011
0000
1037 TRY_PARSING_AS_JSON_YAML:
1038 {
1039 1 my ($success, $e, $decoded);
1040 1 ▶ if ($per_arg_json) {
Branch: 1 branch
BranchTrueFalseTotal
if ($per_arg_json) 1 0 1
1041 1 ($success, $e, $decoded) = _parse_json($val);
1042 1 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 0 1 1
1043 0 $val = $decoded;
1044 0 last TRY_PARSING_AS_JSON_YAML;
1045 } else {
1046 #warn "Failed trying to parse argv #$arg_spec->{pos} as JSON: $e";
1047 }
1048 }
1049 1 ▶ if ($per_arg_yaml) {
Branch: 1 branch
BranchTrueFalseTotal
if ($per_arg_yaml) 1 0 1
1050 1 ($success, $e, $decoded) = _parse_yaml($val);
1051 1 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 1 0 1
1052 1 $val = $decoded;
1053 1 last TRY_PARSING_AS_JSON_YAML;
1054 } else {
1055 #warn "Failed trying to parse argv #$arg_spec->{pos} as YAML: $e";
1056 }
1057 }
1058 }
1059 }
1060 9 $rargs->{$name} = $val;
1061 # we still call cmdline_on_getopt for this
1062 9 ▶ if ($arg_spec->{cmdline_on_getopt}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_spec->{"cmdline_on_getopt"}) 2 7 9
1063 2 ▶ if ($arg_spec->{slurpy} // $arg_spec->{greedy}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_spec->{'slurpy'} // $arg_spec->{'greedy'}) { } 2 0 2
Condition33%1/3
$arg_spec->{"slurpy"} // $arg_spec->{"greedy"}
l!l&&r!l&&!r
200
MC/DC0%0/2
$arg_spec->{"slurpy"} // $arg_spec->{"greedy"}
$arg_spec->{"slurpy"} $arg_spec->{"greedy"}
Truth table33%1/3
A // B
A= $arg_spec->{"slurpy"}B= $arg_spec->{"greedy"}
ABresult
1X1
011
000
1064 $arg_spec->{cmdline_on_getopt}->(
1065 arg=>$name, fqarg=>$name, value=>$_, args=>$rargs,
1066 opt=>undef, # this marks that value is retrieved from cmdline arg
1067 2 ) for @$val;
1068 } else {
1069 0 $arg_spec->{cmdline_on_getopt}->(
1070 arg=>$name, fqarg=>$name, value=>$val, args=>$rargs,
1071 opt=>undef, # this marks that value is retrieved from cmdline arg
1072 );
1073 }
1074 }
1075 }
1076 }
1077 }
1078  
1079 # 4. check missing required args
1080  
1081 59 my %missing_args; # value = order
1082 59 my $i = 0;
1083 59 for my $arg (sort {
1084 121 ▶ (($args_prop->{$a}{pos}//9999) <=> ($args_prop->{$b}{pos}//9999)) ||
Condition85%6/7
($args_prop->{$a}{'pos'} // 9999) <=> ($args_prop->{$b}{'pos'} // 9999) || $a cmp $b
l!l&&r!l&&!r
44770
$args_prop->{$a}{"pos"} // 9999
l!l
2596
$args_prop->{$b}{"pos"} // 9999
l!l
2893
MC/DC0%0/2
($args_prop->{$a}{'pos'} // 9999) <=> ($args_prop->{$b}{'pos'} // 9999) || $a cmp $b
$args_prop->{$a}{"pos"} $a cmp $b
MC/DC100%1/1
$args_prop->{$b}{"pos"} // 9999
$args_prop->{$b}{"pos"}
Truth table66%2/3
A || B
A= $args_prop->{$a}{"pos"}B= $a cmp $b
ABresult
1X1
011
000
Truth table100%2/2
A
A= $args_prop->{$b}{"pos"}
Aresult
11
00
1085 ($a cmp $b) } keys %$args_prop) {
1086 144 my $arg_spec = $args_prop->{$arg};
1087 144 ▶ if (!exists($rargs->{$arg})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $rargs->{$arg}) 68 76 144
1088 68 ▶ next unless $arg_spec->{req};
Branch: 1 branch
BranchTrueFalseTotal
unless $arg_spec->{"req"} 63 5 68
1089 # give a chance to hook to set missing arg
1090 5 ▶ if ($on_missing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_missing) 2 3 5
1091 2 ▶ next if $on_missing->(arg=>$arg, args=>$rargs, spec=>$arg_spec);
Branch: 1 branch
BranchTrueFalseTotal
if &$on_missing("arg", $arg, "args", $rargs, "spec", $arg_spec) 1 1 2
1092 }
1093 4 ▶ next if exists $rargs->{$arg};
Branch: 1 branch
BranchTrueFalseTotal
if exists $rargs->{$arg} 1 3 4
1094 3 $missing_args{$arg} = ++$i;
1095 }
1096 }
1097  
1098 # 5. check 'deps', currently we only support 'arg' dep type
1099 {
1100 59 ▶ last unless $strict;
Branch: 1 branch
BranchTrueFalseTotal
unless $strict 1 58 59
1101  
1102 58 for my $arg (keys %$args_prop) {
1103 143 my $arg_spec = $args_prop->{$arg};
1104 143 ▶ next unless exists $rargs->{$arg};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rargs->{$arg} 67 76 143
1105 76 ▶ next unless $arg_spec->{deps};
Branch: 1 branch
BranchTrueFalseTotal
unless $arg_spec->{"deps"} 74 2 76
1106 2 my $dep_arg = $arg_spec->{deps}{arg};
1107 2 ▶ next unless $dep_arg;
Branch: 1 branch
BranchTrueFalseTotal
unless $dep_arg 0 2 2
1108 return [400, "You specify '$arg', but don't specify '$dep_arg' ".
1109 "(upon which '$arg' depends)"]
1110 2 ▶ unless exists $rargs->{$dep_arg};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rargs->{$dep_arg} 1 1 2
1111 }
1112 }
1113  
1114 #$log->tracef("<- get_args_from_argv(), args=%s, remaining argv=%s",
1115 # $rargs, $argv);
1116 [200, "OK", $rargs, {
1117 0 "func.missing_args" => [sort {$missing_args{$a} <=> $missing_args{$b} } keys %missing_args],
1118 "func.gen_getopt_long_spec_result" => $genres,
1119 }];
1120 }
1121  
1122 4 1;
1123 # ABSTRACT: Get subroutine arguments from command line arguments (@ARGV)
1124  
1125 __END__
↑ Summary