Pad/Parser.pm

filter: Statementstmt 97.9% 242 / 247 Branchbran 93.7% 45 / 48 Conditioncond 93.3% 14 / 15 MC/DCmcdc 88.8% 8 / 9 Subroutinesub 93.7% 30 / 32 Podpod 0.0% 0 / 14 totaltotal 92.8% 339 / 365 CC 42 SCAR 13.2
CC
42
Cov
97%
consumeArgs
8.0
settleStep
6.0
__ANON__
6.0
CRAP 3.7
« Pad/Type/Number.pm ↑ Summary Pad/Config/Format/Json.pm »
1 8 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 8 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 8 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Getopt::Pad::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Getopt::Pad::Result::Generator;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 class Getopt::Pad::Parser :strict(params) {
9 8 ▶ use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶ use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶ use Getopt::Pad::Util qw(scalarsIn);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 our $VERSION = '0.06';
14  
15 8 field $spec :param;
16 8 field $argv :param;
17  
18 # The command line is parsed Level by Level first, and the Levels it
19 # selects are resolved afterwards, innermost first, since each Result
20 # holds the one below it. Every Trigger has fired before anything is
21 # resolved, so --help anywhere on the line wins over a missing required
22 # option or a broken config file. Every selected Level settles its
23 # values before any value is verified, and all are verified before any
24 # is prepared, so a parse that fails creates nothing on disk.
25 138 ▶ method parse() {
Subroutine parse: called CC=2 SCAR=6.9
Pod undocumented
26 138 my @words = $argv->@*;
27 138 my @steps = $self->parseCommandLine(\@words);
28 115 my $inheritedValues = $steps[-1]{inheritedValues};
29 115 ▶ my $configValues = $self->inContext($steps[-1]{level}, sub { $self->loadConfigValues($inheritedValues) });
Subroutine __ANON__: called CC=1 SCAR=0.0
30  
31 my @settlements = map {
32 97 my $step = $_;
33 127 ▶ $self->inContext($step->{level}, sub { $self->settleStep($step, $inheritedValues, $configValues, \@words) });
Subroutine __ANON__: called CC=1 SCAR=0.0
34 } reverse @steps;
35 69 ▶ $self->checkSettled(\@settlements, sub ($type, $scalar) { $type->verify($scalar) });
Subroutine __ANON__: called CC=1 SCAR=0.0
36 65 ▶ $self->checkSettled(\@settlements, sub ($type, $scalar) { $type->prepare($scalar) });
Subroutine __ANON__: called CC=1 SCAR=0.0
37  
38 65 my $result;
39 65 foreach my $settlement (@settlements) {
40 92 my $subResult = $result;
41 92 ▶ $result = $self->inContext($settlement->{level}, sub { $self->resultOf($settlement, $subResult) });
Subroutine __ANON__: called CC=1 SCAR=0.0
42 }
43 65 return $result;
44 8 }
45  
46 # Runs $check on every scalar of every settled value, a problem
47 # reported in the wording of the value's source.
48 134 ▶ method checkSettled($settlements, $check) {
Subroutine checkSettled: called CC=2 SCAR=7.5
Pod undocumented
49 134 foreach my $settlement ($settlements->@*) {
50 188 ▶ $self->inContext($settlement->{level}, sub {
Subroutine __ANON__: called CC=4 SCAR=17.9
51 0 ▶ foreach my $entry ($settlement->{entries}->@*) {
Subroutine __ANON__: not called CC=4 SCAR=17.9
52 0 foreach my $scalar (scalarsIn($entry->{value})) {
53 0 my $problem = $check->($entry->{type}, $scalar);
54 0 ▶ $entry->{report}->($problem) if defined $problem;
Branch: 2 branches
BranchTrueFalseTotal
if defined $problem 4 270 274
if defined $problem 0 0 0
55 }
56 }
57 188 });
58 }
59 130 return;
60 8 }
61  
62 # User errors raised by $code are attributed to $level, whose help the
63 # user sees next to the message.
64 716 ▶ method inContext($level, $code) {
Subroutine inContext: called CC=3 SCAR=11.0
Pod undocumented
65 716 try {
66 716 return $code->();
67 }
68 catch ($error) {
69 73 ▶ $error->attachContext($level) if Scalar::Util::blessed($error) && $error->isa('Getopt::Pad::Error');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($error) and $error->isa("Getopt::Pad::Error") 60 13 73
Condition100%3/3
builtin::blessed($error) and $error->isa("Getopt::Pad::Error")
!ll&&!rl&&r
11260
MC/DC100%2/2
builtin::blessed($error) and $error->isa("Getopt::Pad::Error")
builtin::blessed($error) $error->isa("Getopt::Pad::Error")
Truth table100%3/3
A and B
A= builtin::blessed($error)B= $error->isa("Getopt::Pad::Error")
ABresult
0X0
100
111
70 73 die $error;
71 }
72 8 }
73  
74 # One step per selected Level: its own command line values, the values
75 # of every inherited option given so far, and the command it names.
76 138 ▶ method parseCommandLine($words) {
Subroutine parseCommandLine: called CC=3 SCAR=11.0
Pod undocumented
77 138 my @steps;
78 138 my $level = $spec->root;
79 138 my $inheritedValues = {};
80 138 ▶ while (defined $level) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $level) 194 115 309
81 194 ▶ my $step = $self->inContext($level, sub { $self->parseLevel($level, $words, $inheritedValues) });
Subroutine __ANON__: called CC=1 SCAR=0.0
82 171 push @steps, $step;
83 171 $inheritedValues = $step->{inheritedValues};
84 171 ▶ $level = defined $step->{command} ? $level->command($step->{command}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $step->{'command'} ? : 56 115 171
85 }
86 115 return @steps;
87 8 }
88  
89 194 ▶ method parseLevel($level, $words, $inheritedSoFar) {
Subroutine parseLevel: called CC=2 SCAR=6.9
Pod undocumented
90 194 my %values = $self->parseOptions($level, $words, $inheritedSoFar);
91 189 $self->fireTriggers($level, \%values);
92  
93 # Inherited options leave the Level's own values: they carry on to
94 # the next Level and end up with the Level declaring them.
95 173 my %inheritedValues = map { $_->name => delete $values{$_->name} } grep { exists $values{$_->name} } $level->inheritableOptions, $level->inheritedOptions;
96 173 ▶ my $command = $level->hasCommands ? $self->selectCommand($level, $words) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$level->hasCommands ? : 59 114 173
97 171 return { level => $level, values => \%values, inheritedValues => \%inheritedValues, command => $command };
98 8 }
99  
100 # A Trigger sees its option's value after the Value pipeline, so an
101 # unsupported --create-completions shell is a user error, not a crash.
102 # The values inherited from an outer Level never hold the option of a
103 # Trigger: every Trigger ends the parse where it fires.
104 189 ▶ method fireTriggers($level, $values) {
Subroutine fireTriggers: called CC=3 SCAR=11.0
Pod undocumented
105 189 my $helper = $spec->helperFor($level);
106 189 ▶ foreach my $option (grep { defined $_->trigger && exists $values->{$_->name} } $level->options) {
Condition100%3/3
defined $_->trigger && exists $$values{$_->name}
!ll&&!rl&&r
52754716
MC/DC100%2/2
defined $_->trigger && exists $$values{$_->name}
defined $_->trigger exists $$values{$_->name}
Truth table100%3/3
A && B
A= defined $_->triggerB= exists $$values{$_->name}
ABresult
0X0
100
111
107 16 $option->trigger->($helper, $spec, $level, $option->readerValue(commandLine => $values));
108 }
109 173 return;
110 8 }
111  
112 59 ▶ method selectCommand($level, $words) {
Subroutine selectCommand: called CC=4 SCAR=13.9
Pod undocumented
113 59 my $expected = join(', ', $level->commandNames);
114  
115 59 ▶ if (!$words->@*) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$words) 2 57 59
116 2 ▶ return undef if !$level->commandRequired;
Branch: 1 branch
BranchTrueFalseTotal
unless $level->commandRequired 1 1 2
117 1 Getopt::Pad::Error->throw("missing command, expected one of: %s", $expected);
118 }
119  
120 57 my $word = shift $words->@*;
121 57 ▶ Getopt::Pad::Error->throw("unknown command '%s', expected one of: %s", $word, $expected) if !defined $level->command($word);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level->command($word) 1 56 57
122 56 return $word;
123 8 }
124  
125 # Getopt::Long stores into a copy of the inherited values given so far,
126 # so an inherited option repeated across Levels accumulates exactly as
127 # it does when repeated on one.
128 194 ▶ method parseOptions($level, $words, $inheritedSoFar) {
Subroutine parseOptions: called CC=4 SCAR=13.9
Pod undocumented
129 194 my @glSpecs = map { $_->glSpec } $level->options;
130 194 my @config = qw(bundling no_ignore_case no_auto_abbrev);
131 194 ▶ push @config, $level->hasCommands ? 'require_order' : 'permute';
Branch: 1 branch
BranchTrueFalseTotal
$level->hasCommands ? : 62 132 194
132  
133 194 my $gl = Getopt::Long::Parser->new(config => \@config);
134 194 my %values = map { $_ => $self->copiedWords($inheritedSoFar->{$_}) } keys $inheritedSoFar->%*;
135 194 my @glWarnings;
136 my $ok;
137 {
138 194 ▶ local $SIG{__WARN__} = sub { push @glWarnings, $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
139 194 $ok = $gl->getoptionsfromarray($words, \%values, @glSpecs);
140 }
141  
142 194 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 5 189 194
143 5 ▶ my $message = join('; ', map { s/\s+$//r } @glWarnings) || 'invalid command line';
Condition50%1/2
join('; ', map({s/\s+$//ru;} @glWarnings)) || 'invalid command line'
l!l
50
MC/DC0%0/1
join('; ', map({s/\s+$//ru;} @glWarnings)) || 'invalid command line'
join('; ', map({s/\s+$//ru;} @glWarnings))
Truth table50%1/2
A
A= join('; ', map({s/\s+$//ru;} @glWarnings))
Aresult
11
00
144 5 Getopt::Pad::Error->throw('%s', $message);
145 }
146  
147 189 return %values;
148 8 }
149  
150 10 ▶ method copiedWords($given) {
Subroutine copiedWords: called CC=3 SCAR=11.0
Pod undocumented
151 10 ▶ return [$given->@*] if ref $given eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $given eq "ARRAY" 2 8 10
152 8 ▶ return { $given->%* } if ref $given eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $given eq "HASH" 0 8 8
153 8 return $given;
154 8 }
155  
156 115 ▶ method loadConfigValues($inheritedValues) {
Subroutine loadConfigValues: called CC=3 SCAR=11.0
Pod undocumented
157 115 ▶ my $configSpec = $spec->config // return {};
Condition100%2/2
$spec->config // (return {})
l!l
5758
MC/DC100%1/1
$spec->config // (return {})
$spec->config
Truth table100%2/2
A
A= $spec->config
Aresult
11
00
158  
159 57 my $configOption = $spec->CONFIG_OPTION;
160 57 ▶ return $configSpec->io->autoloadValues($spec->root) if !exists $inheritedValues->{$configOption};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $inheritedValues->{$configOption} 45 12 57
161 12 return $configSpec->io->explicitValues($spec->root, $inheritedValues->{$configOption});
162 8 }
163  
164 # The checked values of one selected Level, as reader values, and as
165 # entries carrying what verify and prepare need. Its inherited options
166 # take the words collected on every Level down from it, its options the
167 # config values of its own section.
168 127 ▶ method settleStep($step, $inheritedValues, $configValues, $words) {
Subroutine settleStep: called CC=6 SCAR=17.9
Pod undocumented
169 127 my $level = $step->{level};
170 127 my %commandLine = ($step->{values}->%*, map { $_->name => $inheritedValues->{$_->name} } grep { exists $inheritedValues->{$_->name} } $level->inheritableOptions);
171 127 ▶ my $config = $configValues->{$level->path} // {};
Condition100%2/2
$$configValues{$level->path} // {}
l!l
5374
MC/DC100%1/1
$$configValues{$level->path} // {}
$$configValues{$level->path}
Truth table100%2/2
A
A= $$configValues{$level->path}
Aresult
11
00
172  
173 127 my (%readerValues, @entries);
174 127 foreach my $option ($level->declaredOptions) {
175 202 my ($value, $report) = $option->settledValue(commandLine => \%commandLine, config => $config);
176 179 $readerValues{$option->reader} = $value;
177 179 ▶ push @entries, { type => $option->type, value => $value, report => $report } if defined $report;
Branch: 1 branch
BranchTrueFalseTotal
if defined $report 106 73 179
178 }
179  
180 104 ▶ if (!$level->hasCommands) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($level->hasCommands) 74 30 104
181 74 my %argValues = $self->consumeArgs($level, $words);
182 69 foreach my $arg (grep { exists $argValues{$_->reader} } $level->args) {
183 21 my $short = $arg->short;
184 0 ▶ push @entries, { type => $arg->type, value => $argValues{$arg->reader}, report => sub ($problem) { Getopt::Pad::Error->throw("argument <%s>: %s", $short, $problem) } };
Subroutine __ANON__: not called CC=1 SCAR=1.2
185 }
186 69 %readerValues = (%readerValues, %argValues);
187 }
188  
189 99 return { level => $level, command => $step->{command}, readerValues => \%readerValues, entries => \@entries };
190 8 }
191  
192 # The Result of one settled Level, holding $subResult, the Result of
193 # the Level below it.
194 92 ▶ method resultOf($settlement, $subResult) {
Subroutine resultOf: called CC=4 SCAR=13.9
Pod undocumented
195 92 my $level = $settlement->{level};
196 92 my %readerValues = $settlement->{readerValues}->%*;
197 92 ▶ my %levelValues = $level->hasCommands ? (command => $settlement->{command}, subcommand => $subResult) : ();
Branch: 1 branch
BranchTrueFalseTotal
$level->hasCommands ? : 28 64 92
198  
199 92 my $class = Getopt::Pad::Result::Generator::generate($level);
200 92 my $helper = $spec->helperFor($level);
201 92 my $unprocessed = $class->new(%readerValues, %levelValues, helper => $helper);
202 92 my @processing = grep { defined $_->processValue } $level->declaredOptions, $level->args;
203 92 ▶ return $unprocessed if !@processing;
Branch: 1 branch
BranchTrueFalseTotal
unless @processing 91 1 92
204  
205 # Every processValue callback sees the unprocessed Result, so the
206 # order they run in does not matter.
207 1 $readerValues{$_->reader} = $_->processedValue($unprocessed, $readerValues{$_->reader}) foreach @processing;
208 1 return $class->new(%readerValues, %levelValues, helper => $helper);
209 8 }
210  
211 24 ▶ method validatedArgValue($arg, $value) {
Subroutine validatedArgValue: called CC=2 SCAR=6.9
Pod undocumented
212 24 my $problem = $arg->type->check($value);
213 24 ▶ Getopt::Pad::Error->throw("argument <%s>: %s", $arg->short, $problem) if defined $problem;
Branch: 1 branch
BranchTrueFalseTotal
if defined $problem 1 23 24
214 23 return $arg->type->coerce($value);
215 8 }
216  
217 74 ▶ method consumeArgs($level, $words) {
Subroutine consumeArgs: called CC=8 SCAR=20.8
Pod undocumented
218 74 my %readerValues;
219 74 my @args = $level->args;
220  
221 74 foreach my $index (0 .. $#args) {
222 26 my $arg = $args[$index];
223  
224 26 ▶ if ($arg->multiple) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->multiple) 3 23 26
225 3 my @rest = splice($words->@*);
226 3 ▶ Getopt::Pad::Error->throw("missing required argument <%s>", $arg->short) if !@rest && $arg->required;
Branch: 1 branch
BranchTrueFalseTotal
if not @rest and $arg->required 1 2 3
Condition100%3/3
not @rest and $arg->required
!ll&&!rl&&r
111
MC/DC100%2/2
not @rest and $arg->required
not @rest $arg->required
Truth table100%3/3
not(A) and B
A= not @restB= $arg->required
ABresult
0X0
100
111
227 2 $readerValues{$arg->reader} = [map { $self->validatedArgValue($arg, $_) } @rest];
228 2 next;
229 }
230  
231 23 ▶ if (!$words->@*) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$words) 2 21 23
232 2 ▶ Getopt::Pad::Error->throw("missing required argument <%s>", $arg->short) if $arg->required;
Branch: 1 branch
BranchTrueFalseTotal
if $arg->required 1 1 2
233 1 next;
234 }
235  
236 21 $readerValues{$arg->reader} = $self->validatedArgValue($arg, shift $words->@*);
237 }
238  
239 71 ▶ Getopt::Pad::Error->throw("unexpected extra argument '%s'", $words->[0]) if $words->@*;
Branch: 1 branch
BranchTrueFalseTotal
if @$words 2 69 71
240  
241 69 return %readerValues;
242 8 }
243 }
244  
245 8 1;
246  
247 __END__
« Pad/Type/Number.pm ↑ Summary Pad/Config/Format/Json.pm »