Pad/Parser.pm

filter: Statementstmt 100% 198 / 198 Branchbran 95.0% 38 / 40 Conditioncond 93.3% 14 / 15 MC/DCmcdc 88.8% 8 / 9 Subroutinesub 100% 23 / 23 Podpod 0.0% 0 / 12 totaltotal 94.6% 281 / 297 CC 31 SCAR 12.5
CC
31
Cov
99%
consumeArgs
8.0
parseOptions
4.0
selectCommand
4.0
CRAP 3.5
« Pad/Type/Number.pm ↑ Summary Pad/Spec/Option.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  
12 8 our $VERSION = '0.05';
13  
14 8 field $spec :param;
15 8 field $argv :param;
16  
17 # The command line is parsed Level by Level first, and the Levels it
18 # selects are resolved afterwards, innermost first, since each Result
19 # holds the one below it. Every Trigger has fired before anything is
20 # resolved, so --help anywhere on the line wins over a missing required
21 # option or a broken config file.
22 120 ▶ method parse() {
Subroutine parse: called CC=2 SCAR=6.9
Pod undocumented
23 120 my @words = $argv->@*;
24 120 my @steps = $self->parseCommandLine(\@words);
25 98 my $inheritedValues = $steps[-1]{inheritedValues};
26 98 ▶ my $configValues = $self->inContext($spec->root, sub { $self->loadConfigValues($inheritedValues) });
Subroutine __ANON__: called CC=1 SCAR=0.0
27  
28 81 my $result;
29 81 foreach my $step (reverse @steps) {
30 110 my $subResult = $result;
31 110 ▶ $result = $self->inContext($step->{level}, sub { $self->resolveStep($step, $inheritedValues, $configValues, \@words, $subResult) });
Subroutine __ANON__: called CC=1 SCAR=0.0
32 }
33 56 return $result;
34 8 }
35  
36 # User errors raised by $code are attributed to $level, whose help the
37 # user sees next to the message.
38 382 ▶ method inContext($level, $code) {
Subroutine inContext: called CC=3 SCAR=11.0
Pod undocumented
39 382 try {
40 382 return $code->();
41 }
42 catch ($error) {
43 64 ▶ $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") 52 12 64
Condition100%3/3
builtin::blessed($error) and $error->isa("Getopt::Pad::Error")
!ll&&!rl&&r
11152
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
44 64 die $error;
45 }
46 8 }
47  
48 # One step per selected Level: its own command line values, the values
49 # of every inherited option given so far, and the command it names.
50 120 ▶ method parseCommandLine($words) {
Subroutine parseCommandLine: called CC=3 SCAR=11.0
Pod undocumented
51 120 my @steps;
52 120 my $level = $spec->root;
53 120 my $inheritedValues = {};
54 120 ▶ while (defined $level) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $level) 174 98 272
55 174 ▶ my $step = $self->inContext($level, sub { $self->parseLevel($level, $words, $inheritedValues) });
Subroutine __ANON__: called CC=1 SCAR=0.0
56 152 push @steps, $step;
57 152 $inheritedValues = $step->{inheritedValues};
58 152 ▶ $level = defined $step->{command} ? $level->command($step->{command}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $step->{'command'} ? : 54 98 152
59 }
60 98 return @steps;
61 8 }
62  
63 174 ▶ method parseLevel($level, $words, $inheritedSoFar) {
Subroutine parseLevel: called CC=2 SCAR=6.9
Pod undocumented
64 174 my %values = $self->parseOptions($level, $words, $inheritedSoFar);
65 169 $self->fireTriggers($level, \%values);
66  
67 # Inherited options leave the Level's own values: they carry on to
68 # the next Level and end up with the Level declaring them.
69 154 my %inheritedValues = map { $_->name => delete $values{$_->name} } grep { exists $values{$_->name} } $level->inheritableOptions, $level->inheritedOptions;
70 154 ▶ my $command = $level->hasCommands ? $self->selectCommand($level, $words) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$level->hasCommands ? : 57 97 154
71 152 return { level => $level, values => \%values, inheritedValues => \%inheritedValues, command => $command };
72 8 }
73  
74 # A Trigger sees its option's value after the Value pipeline, so an
75 # unsupported --create-completions shell is a user error, not a crash.
76 # The values inherited from an outer Level never hold the option of a
77 # Trigger: every Trigger ends the parse where it fires.
78 169 ▶ method fireTriggers($level, $values) {
Subroutine fireTriggers: called CC=3 SCAR=11.0
Pod undocumented
79 169 my $helper = $spec->helperFor($level);
80 169 ▶ foreach my $option (grep { defined $_->trigger && exists $values->{$_->name} } $level->options) {
Condition100%3/3
defined $_->trigger && exists $$values{$_->name}
!ll&&!rl&&r
46348015
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
81 15 $option->trigger->($helper, $spec, $level, $option->readerValue(commandLine => $values));
82 }
83 154 return;
84 8 }
85  
86 57 ▶ method selectCommand($level, $words) {
Subroutine selectCommand: called CC=4 SCAR=13.9
Pod undocumented
87 57 my $expected = join(', ', $level->commandNames);
88  
89 57 ▶ if (!$words->@*) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$words) 2 55 57
90 2 ▶ return undef if !$level->commandRequired;
Branch: 1 branch
BranchTrueFalseTotal
unless $level->commandRequired 1 1 2
91 1 Getopt::Pad::Error->throw("missing command, expected one of: %s", $expected);
92 }
93  
94 55 my $word = shift $words->@*;
95 55 ▶ 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 54 55
96 54 return $word;
97 8 }
98  
99 # Getopt::Long stores into a copy of the inherited values given so far,
100 # so an inherited option repeated across Levels accumulates exactly as
101 # it does when repeated on one.
102 174 ▶ method parseOptions($level, $words, $inheritedSoFar) {
Subroutine parseOptions: called CC=4 SCAR=13.9
Pod undocumented
103 174 my @glSpecs = map { $_->glSpec } $level->options;
104 174 my @config = qw(bundling no_ignore_case no_auto_abbrev);
105 174 ▶ push @config, $level->hasCommands ? 'require_order' : 'permute';
Branch: 1 branch
BranchTrueFalseTotal
$level->hasCommands ? : 60 114 174
106  
107 174 my $gl = Getopt::Long::Parser->new(config => \@config);
108 174 my %values = map { $_ => $self->copiedWords($inheritedSoFar->{$_}) } keys $inheritedSoFar->%*;
109 174 my @glWarnings;
110 my $ok;
111 {
112 174 ▶ local $SIG{__WARN__} = sub { push @glWarnings, $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
113 174 $ok = $gl->getoptionsfromarray($words, \%values, @glSpecs);
114 }
115  
116 174 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 5 169 174
117 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
118 5 Getopt::Pad::Error->throw('%s', $message);
119 }
120  
121 169 return %values;
122 8 }
123  
124 10 ▶ method copiedWords($given) {
Subroutine copiedWords: called CC=3 SCAR=11.0
Pod undocumented
125 10 ▶ return [$given->@*] if ref $given eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $given eq "ARRAY" 2 8 10
126 8 ▶ return { $given->%* } if ref $given eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $given eq "HASH" 0 8 8
127 8 return $given;
128 8 }
129  
130 98 ▶ method loadConfigValues($inheritedValues) {
Subroutine loadConfigValues: called CC=3 SCAR=11.0
Pod undocumented
131 98 ▶ my $configSpec = $spec->config // return {};
Condition100%2/2
$spec->config // (return {})
l!l
4850
MC/DC100%1/1
$spec->config // (return {})
$spec->config
Truth table100%2/2
A
A= $spec->config
Aresult
11
00
132  
133 48 my $configOption = $spec->CONFIG_OPTION;
134 48 ▶ return $configSpec->io->autoloadValues($spec->root) if !exists $inheritedValues->{$configOption};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $inheritedValues->{$configOption} 36 12 48
135 12 return $configSpec->io->explicitValues($spec->root, $inheritedValues->{$configOption});
136 8 }
137  
138 # The Result of one selected Level. Its inherited options take the words
139 # collected on every Level down from it, its options the config values
140 # of its own section.
141 110 ▶ method resolveStep($step, $inheritedValues, $configValues, $words, $subResult) {
Subroutine resolveStep: called CC=4 SCAR=13.9
Pod undocumented
142 110 my $level = $step->{level};
143 110 my %commandLine = ($step->{values}->%*, map { $_->name => $inheritedValues->{$_->name} } grep { exists $inheritedValues->{$_->name} } $level->inheritableOptions);
144 110 ▶ my $config = $configValues->{$level->path} // {};
Condition100%2/2
$$configValues{$level->path} // {}
l!l
4466
MC/DC100%1/1
$$configValues{$level->path} // {}
$$configValues{$level->path}
Truth table100%2/2
A
A= $$configValues{$level->path}
Aresult
11
00
145  
146 110 my %readerValues;
147 110 $readerValues{$_->reader} = $_->readerValue(commandLine => \%commandLine, config => $config) foreach $level->declaredOptions;
148  
149 90 my $class = Getopt::Pad::Result::Generator::generate($level);
150 90 my $helper = $spec->helperFor($level);
151 90 ▶ return $class->new(%readerValues, command => $step->{command}, subcommand => $subResult, helper => $helper) if $level->hasCommands;
Branch: 1 branch
BranchTrueFalseTotal
if $level->hasCommands 29 61 90
152 61 return $class->new(%readerValues, $self->consumeArgs($level, $words), helper => $helper);
153 8 }
154  
155 23 ▶ method validatedArgValue($arg, $value) {
Subroutine validatedArgValue: called CC=3 SCAR=11.0
Pod undocumented
156 23 my $problem = $arg->type->check($value);
157 23 ▶ Getopt::Pad::Error->throw("argument <%s>: %s", $arg->short, $problem) if defined $problem;
Branch: 1 branch
BranchTrueFalseTotal
if defined $problem 1 22 23
158  
159 22 my $coerced = $arg->type->coerce($value);
160 22 $problem = $arg->type->prepare($coerced);
161 22 ▶ Getopt::Pad::Error->throw("argument <%s>: %s", $arg->short, $problem) if defined $problem;
Branch: 1 branch
BranchTrueFalseTotal
if defined $problem 0 22 22
162 22 return $coerced;
163 8 }
164  
165 61 ▶ method consumeArgs($level, $words) {
Subroutine consumeArgs: called CC=8 SCAR=20.8
Pod undocumented
166 61 my %readerValues;
167 61 my @args = $level->args;
168  
169 61 foreach my $index (0 .. $#args) {
170 25 my $arg = $args[$index];
171  
172 25 ▶ if ($arg->multiple) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->multiple) 3 22 25
173 3 my @rest = splice($words->@*);
174 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
175 2 $readerValues{$arg->reader} = [map { $self->validatedArgValue($arg, $_) } @rest];
176 2 next;
177 }
178  
179 22 ▶ if (!$words->@*) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$words) 2 20 22
180 2 ▶ Getopt::Pad::Error->throw("missing required argument <%s>", $arg->short) if $arg->required;
Branch: 1 branch
BranchTrueFalseTotal
if $arg->required 1 1 2
181 1 next;
182 }
183  
184 20 $readerValues{$arg->reader} = $self->validatedArgValue($arg, shift $words->@*);
185 }
186  
187 58 ▶ Getopt::Pad::Error->throw("unexpected extra argument '%s'", $words->[0]) if $words->@*;
Branch: 1 branch
BranchTrueFalseTotal
if @$words 2 56 58
188  
189 56 return %readerValues;
190 8 }
191 }
192  
193 8 1;
194  
195 __END__
« Pad/Type/Number.pm ↑ Summary Pad/Spec/Option.pm »