Pad/Spec.pm

filter: Statementstmt 97.9% 96 / 98 Branchbran 100% 12 / 12 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 96.3% 26 / 27 Podpod 57.1% 4 / 7 totaltotal 96.1% 148 / 154 CC 14 SCAR 10.7
CC
14
Cov
98%
attachAutoOptions
8.0
checkCommandsKeyIsFree
4.0
passInheritedOptions
3.0
CRAP 2.9
« Pad/Type/Int.pm ↑ Summary Pad/Type/Temporal.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::Level;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Getopt::Pad::Spec::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Getopt::Pad::Spec::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Getopt::Pad::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Getopt::Pad::ExitRequest;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Getopt::Pad::Help;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Getopt::Pad::Completion;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 class Getopt::Pad::Spec :strict(params) {
13 11 ▶ use Getopt::Pad::Util qw(specError);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 our $VERSION = '0.06';
16  
17 11 ▶ use constant CONFIG_OPTION => 'config';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # The one table of Auto options: name, placement ('every' Level, the
20 # 'root', or the root of a Spec with a 'config' block), option spec, and
21 # the Trigger the parser fires when the parsed command line sets it.
22 # Every Trigger ends the parse with an ExitRequest carrying its finished
23 # output. CONFIG_OPTION carries no Trigger; the parser consumes it for
24 # config loading. The config options act on the whole config file, so
25 # every Level below the root inherits them.
26 my @autoOptions = (
27 {
28 key => 'help',
29 placement => 'every',
30 266 ▶ raw => sub ($config) { return { hidden => 1, help => 'Print this help text and exit' } },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 4 ▶ trigger => sub ($helper, $spec, $level, $value) {
Subroutine __ANON__: called CC=1 SCAR=0.0
32 4 die Getopt::Pad::ExitRequest->new(output => $helper->renderHelp);
33 },
34 },
35 {
36 key => 'version',
37 placement => 'root',
38 157 ▶ raw => sub ($config) { return { hidden => 1, help => 'Print the program version and exit' } },
Subroutine __ANON__: called CC=1 SCAR=0.0
39 1 ▶ trigger => sub ($helper, $spec, $level, $value) {
Subroutine __ANON__: called CC=1 SCAR=0.0
40 1 die Getopt::Pad::ExitRequest->new(output => $helper->renderVersion . "\n");
41 },
42 },
43 {
44 key => 'create-completions',
45 placement => 'root',
46 157 ▶ raw => sub ($config) {
Subroutine __ANON__: called CC=1 SCAR=0.0
47 return {
48 157 type => 's',
49 valid => Getopt::Pad::Completion->SHELLS,
50 group => 'Completion',
51 help => 'Print a completion script for this shell to STDOUT and exit',
52 };
53 },
54 1 ▶ trigger => sub ($helper, $spec, $level, $value) {
Subroutine __ANON__: called CC=1 SCAR=0.0
55 1 die Getopt::Pad::ExitRequest->new(output => Getopt::Pad::Completion->new(spec => $spec)->renderScript($value));
56 },
57 },
58 {
59 key => CONFIG_OPTION,
60 placement => 'config',
61 optionalValue => 1,
62 75 ▶ raw => sub ($config) {
Subroutine __ANON__: called CC=2 SCAR=6.9
63 75 my $help = sprintf('Load options from this %s config file', $config->formatName);
64 75 ▶ $help .= sprintf(' (bare --config loads %s)', $config->defaultPath) if defined $config->defaultPath;
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->defaultPath 2 73 75
65 75 return { type => 's', group => 'Config', help => $help, inherit => 1 };
66 },
67 },
68 {
69 key => 'create-default-config',
70 placement => 'config',
71 74 ▶ raw => sub ($config) {
Subroutine __ANON__: called CC=1 SCAR=0.0
72 74 return { type => 's', group => 'Config', help => 'Write a config file prefilled with the default values to this path and exit', inherit => 1 };
73 },
74 9 ▶ trigger => sub ($helper, $spec, $level, $value) {
Subroutine __ANON__: called CC=1 SCAR=0.0
75 9 die Getopt::Pad::ExitRequest->new(output => sprintf("Wrote default config to %s\n", $spec->config->io->writeDefaultFile($spec->root, $value)));
76 },
77 },
78 11 );
79  
80 11 field $raw :param;
81  
82 264 ▶ field $root :reader;
Subroutine root: called CC=1 SCAR=0.0
Pod documented
83 124 ▶ field $config :reader;
Subroutine config: called CC=1 SCAR=0.0
Pod documented
84 0 ▶ field $version :reader;
Subroutine version: not called CC=1 SCAR=2.6
Pod documented
85  
86 ADJUST {
87 specError('GetOptions expects key/value pairs') if ref $raw ne 'HASH';
88  
89 my %spec = $raw->%*;
90 $config = delete $spec{config};
91 $version = delete $spec{version};
92 $config = Getopt::Pad::Spec::Config->new(raw => $config) if defined $config;
93  
94 $root = Getopt::Pad::Spec::Level->new(raw => \%spec);
95 $self->attachAutoOptions($root, 1);
96 $self->passInheritedOptions($root);
97 $self->checkCommandsKeyIsFree($root) if defined $config;
98 }
99  
100 266 ▶ method attachAutoOptions($level, $isRoot) {
Subroutine attachAutoOptions: called CC=8 SCAR=20.8
Pod undocumented
101 266 foreach my $entry (@autoOptions) {
102 1329 ▶ next if $entry->{placement} ne 'every' && !$isRoot;
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"placement"} ne "every" and not $isRoot 436 893 1329
Condition100%3/3
$entry->{"placement"} ne "every" and not $isRoot
!ll&&!rl&&r
266627436
MC/DC100%2/2
$entry->{"placement"} ne "every" and not $isRoot
$entry->{"placement"} ne "every" not $isRoot
Truth table100%3/3
A and not(B)
A= $entry->{"placement"} ne "every"B= not $isRoot
ABresult
0X0
100
111
103 893 ▶ next if $entry->{placement} eq 'config' && !defined $config;
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"placement"} eq "config" and not defined $config 164 729 893
Condition100%3/3
$entry->{"placement"} eq "config" and not defined $config
!ll&&!rl&&r
580149164
MC/DC100%2/2
$entry->{"placement"} eq "config" and not defined $config
$entry->{"placement"} eq "config" not defined $config
Truth table100%3/3
A and not(B)
A= $entry->{"placement"} eq "config"B= not defined $config
ABresult
0X0
100
111
104  
105 $level->addOption(Getopt::Pad::Spec::Option->new(
106 auto => 1,
107 key => $entry->{key},
108 raw => $entry->{raw}->($config),
109 trigger => $entry->{trigger},
110 729 ▶ ($entry->{optionalValue} ? (optionalValue => 1) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$entry->{'optionalValue'} ? : 75 654 729
111 ));
112 }
113  
114 265 $self->attachAutoOptions($level->command($_), 0) foreach $level->commandNames;
115 11 }
116  
117 # Every Level below hands on what it received plus its own inheritable
118 # options, each paired with the Level declaring it.
119 263 ▶ method passInheritedOptions($level, @received) {
Subroutine passInheritedOptions: called CC=3 SCAR=11.0
Pod undocumented
120 263 my @passed = (@received, map { [$_, $level] } $level->inheritableOptions);
121 263 foreach my $name ($level->commandNames) {
122 109 my $command = $level->command($name);
123 109 $command->inheritOption($_->@*) foreach @passed;
124 107 $self->passInheritedOptions($command, @passed);
125 }
126 11 }
127  
128 # A config file holds the command sections of a Level with commands
129 # under the COMMANDS_KEY, so no group of such a Level may take its name.
130 141 ▶ method checkCommandsKeyIsFree($level) {
Subroutine checkCommandsKeyIsFree: called CC=4 SCAR=13.9
Pod undocumented
131 141 ▶ return if !$level->hasCommands;
Branch: 1 branch
BranchTrueFalseTotal
unless $level->hasCommands 93 48 141
132  
133 48 my $key = Getopt::Pad::Config->COMMANDS_KEY;
134 48 ▶ specError("%sgroup '%s' is reserved for the command sections of config files", $level->where, $key) if grep { $_->group eq $key } $level->declaredOptions;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->group eq $key;} $level->declaredOptions 2 46 48
135 46 $self->checkCommandsKeyIsFree($level->command($_)) foreach $level->commandNames;
136 11 }
137  
138 299 ▶ method helperFor($level, %overrides) {
Subroutine helperFor: called CC=1 SCAR=0.0
Pod documented
139 299 return Getopt::Pad::Help->new(level => $level, version => $version, %overrides);
140 11 }
141 }
142  
143 11 1;
144  
145 __END__
« Pad/Type/Int.pm ↑ Summary Pad/Type/Temporal.pm »