Pad/Spec/Level.pm

filter: Statementstmt 93.4% 142 / 152 Branchbran 85.7% 12 / 14 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 85.7% 24 / 28 Podpod 95.6% 22 / 23 totaltotal 92.3% 206 / 223 CC 13 SCAR 7.1
CC
13
Cov
93%
hasArgs
4.5
addOption
4.0
inheritOption
4.0
CRAP 2.0
« Pad/Type.pm ↑ Summary Pad/Type/Float.pm »
1 11 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 11 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 11 ▶use Getopt::Pad::Spec::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Getopt::Pad::Spec::Arg;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 class Getopt::Pad::Spec::Level :strict(params) {
8 11 ▶ use Getopt::Pad::Util qw(specError isValidName);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 our $VERSION = '0.06';
11  
12 11 field $raw :param;
13 0 ▶ field $path :param :reader = '';
Subroutine path: not called CC=1 SCAR=0.4
Pod documented
14  
15 0 ▶ field @options;
Subroutine path: called CC=1 SCAR=0.2
Pod documented
16 11 field @inheritedOptions;
17 11 field %optionByName;
18 11 field %takenNames;
19 11 field @args;
20 11 field %commands;
21 0 ▶ field $commandRequired :reader = 1;
Subroutine commandRequired: not called CC=1 SCAR=0.8
Pod documented
22 0 ▶ field $description :reader = '';
Subroutine commandRequired: called CC=1 SCAR=0.6
Subroutine description: not called CC=1 SCAR=0.4
Pod documented
Pod documented
23 0 ▶ field @examples;
Subroutine description: called CC=1 SCAR=0.2
Pod documented
24  
25 ADJUST {
26 my $where = $self->where;
27 specError("%sexpects a hash reference", $where) if ref $raw ne 'HASH';
28 my %spec = $raw->%*;
29  
30 my $rawOptions = delete $spec{options} // {};
31 specError("%s'options' must be a hash reference", $where) if ref $rawOptions ne 'HASH';
32 foreach my $key (sort keys $rawOptions->%*) {
33 $self->addOption(Getopt::Pad::Spec::Option->new(key => $key, raw => $rawOptions->{$key}, where => $where));
34 }
35  
36 my $rawArgs = delete $spec{args} // [];
37 specError("%s'args' must be an array reference", $where) if ref $rawArgs ne 'ARRAY';
38 foreach my $rawArg ($rawArgs->@*) {
39 push @args, Getopt::Pad::Spec::Arg->new(raw => $rawArg, where => $where);
40 }
41 my $sawOptional = 0;
42 foreach my $index (0 .. $#args) {
43 my $arg = $args[$index];
44 specError("%sarg '%s': multiple is only allowed on the last arg", $where, $arg->short) if $arg->multiple && $index != $#args;
45 specError("%sarg '%s': a required arg cannot follow an optional one", $where, $arg->short) if $arg->required && $sawOptional;
46 $sawOptional = 1 if !$arg->required;
47 }
48  
49 my $rawCommands = delete $spec{commands} // {};
50 specError("%s'commands' must be a hash reference", $where) if ref $rawCommands ne 'HASH';
51 foreach my $name (sort keys $rawCommands->%*) {
52 specError("%sinvalid command name '%s'", $where, $name) if !isValidName($name);
53 my $childPath = $path eq '' ? $name : $path . ' ' . $name;
54 $commands{$name} = __CLASS__->new(raw => $rawCommands->{$name}, path => $childPath);
55 }
56  
57 specError("%sargs and commands are mutually exclusive on one level", $where) if @args && %commands;
58  
59 my @inheritable = grep { $_->inherit } @options;
60 specError("%soption '%s': inherit requires commands on the same level", $where, $inheritable[0]->name) if @inheritable && !%commands;
61  
62 if (exists $spec{commandRequired}) {
63 specError("%scommandRequired without commands", $where) if !%commands;
64 $commandRequired = delete $spec{commandRequired} ? 1 : 0;
65 }
66  
67 $description = delete $spec{description} // '';
68  
69 my $rawExamples = delete $spec{examples} // [];
70 specError("%s'examples' must be an array reference", $where) if ref $rawExamples ne 'ARRAY';
71 foreach my $example ($rawExamples->@*) {
72 specError("%seach example must be a hash with 'text' and 'args'", $where)
73 if ref $example ne 'HASH' || !defined $example->{text} || !defined $example->{args};
74 push @examples, { text => $example->{text}, args => $example->{args} };
75 }
76  
77 specError("%sunknown key(s): %s", $where, join(', ', sort keys %spec)) if %spec;
78  
79 my %readerSource;
80 foreach my $entry (@options, @args) {
81 my $label = $entry->isa('Getopt::Pad::Spec::Option') ? sprintf("option '%s'", $entry->name) : sprintf("arg '%s'", $entry->short);
82 my $seen = $readerSource{$entry->reader};
83 specError("%s%s and %s both map to reader '%s'", $where, $label, $seen, $entry->reader) if defined $seen;
84 $readerSource{$entry->reader} = $label;
85 }
86 }
87  
88 1217 ▶ method addOption($option) {
Subroutine addOption: called CC=4 SCAR=13.9
Pod documented
89 1217 my $name = $option->name;
90 1217 foreach my $candidate ($name, $option->aliases) {
91 1239 ▶ my $owner = $takenNames{$candidate} // next;
Condition100%2/2
$takenNames{$candidate} // (next)
l!l
21237
MC/DC100%1/1
$takenNames{$candidate} // (next)
$takenNames{$candidate}
Truth table100%2/2
A
A= $takenNames{$candidate}
Aresult
11
00
92 2 ▶ specError("%soption '%s' collides with the automatic --%s option", $self->where, $candidate, $candidate) if $option->auto;
Branch: 1 branch
BranchTrueFalseTotal
if $option->auto 1 1 2
93 1 specError("%soption '%s': name '%s' is already used by %s", $self->where, $name, $candidate, $owner);
94 }
95  
96 1215 $self->takeNames($option);
97 1215 push @options, $option;
98 1215 $optionByName{$name} = $option;
99 1215 return $self;
100 11 }
101  
102 # An option an outer Level passes on: accepted on this Level's command
103 # line, but neither configurable nor readable here.
104 239 ▶ method inheritOption($option, $fromLevel) {
Subroutine inheritOption: called CC=4 SCAR=13.9
Pod documented
105 239 my $name = $option->name;
106 239 foreach my $candidate ($name, $option->aliases) {
107 249 ▶ my $owner = $takenNames{$candidate} // next;
Condition100%2/2
$takenNames{$candidate} // (next)
l!l
2247
MC/DC100%1/1
$takenNames{$candidate} // (next)
$takenNames{$candidate}
Truth table100%2/2
A
A= $takenNames{$candidate}
Aresult
11
00
108 2 ▶ specError("%soption '%s' collides with the automatic --%s option", $self->where, $candidate, $candidate) if $option->auto;
Branch: 1 branch
BranchTrueFalseTotal
if $option->auto 1 1 2
109 1 specError("%soption '%s' inherited from %s: name '%s' is already used by %s", $self->where, $name, $fromLevel->label, $candidate, $owner);
110 }
111  
112 237 $self->takeNames($option);
113 237 push @inheritedOptions, $option;
114 237 return $self;
115 11 }
116  
117 1452 ▶ method takeNames($option) {
Subroutine takeNames: called CC=2 SCAR=6.9
Pod undocumented
118 1452 my $name = $option->name;
119 1452 $takenNames{$name} = sprintf("option '%s'", $name);
120 1452 $takenNames{$_} = sprintf("an alias of option '%s'", $name) foreach $option->aliases;
121 1452 return;
122 11 }
123  
124 497 ▶ method isRoot() { return $path eq '' ? 1 : 0 }
Subroutine isRoot: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$path eq '' ? : 240 257 497
125 119 ▶ method label() { return $self->isRoot ? 'the top level' : sprintf("command '%s'", $path) }
Subroutine label: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$self->isRoot ? : 1 118 119
126 324 ▶ method where() { return $self->isRoot ? '' : $self->label . ': ' }
Subroutine where: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$self->isRoot ? : 206 118 324
127 455 ▶ method options() { return (@options, @inheritedOptions) }
Subroutine options: called CC=1 SCAR=0.0
Pod documented
128 374 ▶ method declaredOptions() { return grep { !$_->auto } @options }
Subroutine declaredOptions: called CC=1 SCAR=0.0
Pod documented
129 563 ▶ method inheritableOptions() { return grep { $_->inherit } @options }
Subroutine inheritableOptions: called CC=1 SCAR=0.0
Pod documented
130 174 ▶ method inheritedOptions() { return @inheritedOptions }
Subroutine inheritedOptions: called CC=1 SCAR=0.0
Pod documented
131 381 ▶ method args() { return @args }
Subroutine args: called CC=1 SCAR=0.0
Pod documented
132 27 ▶ method examples() { return @examples }
Subroutine examples: called CC=1 SCAR=0.0
Pod documented
133 0 ▶ method hasArgs() { return @args ? 1 : 0 }
Subroutine hasArgs: not called CC=2 SCAR=15.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 0 0 0
134 869 ▶ method hasCommands() { return %commands ? 1 : 0 }
Subroutine hasCommands: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
%commands ? : 291 578 869
135  
136 68 ▶ method optionByName($name) { return $optionByName{$name} }
Subroutine optionByName: called CC=1 SCAR=0.0
Pod documented
137 674 ▶ method commandNames() { return sort keys %commands }
Subroutine commandNames: called CC=1 SCAR=0.0
Pod documented
138 462 ▶ method command($name) { return $commands{$name} }
Subroutine command: called CC=1 SCAR=0.0
Pod documented
139 }
140  
141 11 1;
142  
143 __END__
« Pad/Type.pm ↑ Summary Pad/Type/Float.pm »