| 1 | package Getopt::Pad::Type; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 12 | ▶ | use v5.26; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use Object::Pad; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 12 | ▶ | use Getopt::Pad::Registry; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 12 | ▶ | use Getopt::Pad::Util qw(specError); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 12 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 12 | my @builtins = map { "Getopt::Pad::Type::$_" } qw(Flag Bool Counter String Int Float File Dir Url Date Duration); | |||||||||||||||||||||||||||||||||||||||||
| 15 | 12 | my $registry; | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 1329 | ▶ | sub registry() { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
registry: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1329 | ▶ | return $registry //= Getopt::Pad::Registry->new(kind => 'option type')->register(@builtins); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$registry //= "Getopt::Pad::Registry"->new("kind", "option type")->register(@builtins)
MC/DC0%0/2
$registry //= "Getopt::Pad::Registry"->new("kind", "option type")->register(@builtins)
$registry
"Getopt::Pad::Registry"->new("kind", "option type")->register(@builtins)
Truth table66%2/3
A //= B
A= $registryB= "Getopt::Pad::Registry"->new("kind", "option type")->register(@builtins)
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 4 | ▶ | sub registerType($class) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
registerType: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | return registry()->register($class); | |||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | # Build the Type an option or arg spec names. Takes the type name (or | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # $defaultName when the spec has none) and the keys the Type declares in | ||||||||||||||||||||||||||||||||||||||||||
| 27 | # SPEC_KEYS out of %$spec, so the caller can treat whatever is left as | ||||||||||||||||||||||||||||||||||||||||||
| 28 | # unknown. The Type checks those keys first; a problem is reported for | ||||||||||||||||||||||||||||||||||||||||||
| 29 | # $owner (e.g. "option 'retries'"). Returns the Type and the name it was | ||||||||||||||||||||||||||||||||||||||||||
| 30 | # resolved under. | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 1324 | ▶ | sub takeFromSpec($spec, $defaultName, $owner) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
takeFromSpec: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 1324 | ▶ | my $typeName = delete $spec->{type} // $defaultName; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
delete $spec->{"type"} // $defaultName
MC/DC0%0/2
delete $spec->{"type"} // $defaultName
delete $spec->{"type"}
$defaultName
Truth table66%2/3
A // B
A= delete $spec->{"type"}B= $defaultName
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 1324 | my $typeClass = registry()->resolve($typeName, $owner); | |||||||||||||||||||||||||||||||||||||||||
| 34 | 1321 | ▶ | my @typeKeys = $typeClass->can('SPEC_KEYS') ? $typeClass->SPEC_KEYS->@* : (); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 1321 | my %typeArgs = map { $_ => delete $spec->{$_} } grep { exists $spec->{$_} } @typeKeys; | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 1321 | my $problem = $typeClass->checkSpecKeys(%typeArgs); | |||||||||||||||||||||||||||||||||||||||||
| 38 | 1321 | ▶ | specError("%s: %s", $owner, $problem) if defined $problem; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 1318 | return ($typeClass->new(%typeArgs), $typeName); | |||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | class Getopt::Pad::Type :abstract { | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 12 | method glSuffix; | |||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | # Types with SPEC_KEYS check them here, before construction: return a | ||||||||||||||||||||||||||||||||||||||||||
| 46 | # problem description without the option name, or undef. | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 1122 | ▶ | method checkSpecKeys :common (%args) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
checkSpecKeys: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 1122 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 12 | } | |||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 376 | ▶ | method takesValue() { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
takesValue: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 376 | ▶ | return $self->glSuffix =~ /=/ ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 12 | } | |||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 235 | ▶ | method negatable() { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
negatable: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 235 | ▶ | return $self->glSuffix eq '!' ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 12 | } | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 1115 | ▶ | method glSpec($names, %flags) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
glSpec: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 1115 | my $suffix = $self->glSuffix; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 1115 | ▶ | $suffix =~ s/^=/:/ if $flags{optionalValue}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 1115 | my $glSpec = $names . $suffix; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 1115 | ▶ | $glSpec .= '@' if $flags{multiple}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 1115 | ▶ | $glSpec .= '%' if $flags{hash}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 1115 | return $glSpec; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 12 | } | |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 222 | ▶ | method coerce($value) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
coerce: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 222 | return $value; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 12 | } | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 221 | ▶ | method check($value) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 221 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 12 | } | |||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | # Runs once per parse on every scalar of the value an option or arg | ||||||||||||||||||||||||||||||||||||||||||
| 77 | # settles on, for checks that depend on the machine rather than on the | ||||||||||||||||||||||||||||||||||||||||||
| 78 | # value (a path exists). Never runs on a spec default when the spec is | ||||||||||||||||||||||||||||||||||||||||||
| 79 | # built. Return a problem description without the option name, or | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # undef. | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 129 | ▶ | method verify($value) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
verify: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 129 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 12 | } | |||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | # Like verify, for types whose values need the world arranged (a path | ||||||||||||||||||||||||||||||||||||||||||
| 86 | # created on demand). Runs only after every value of the parse passed | ||||||||||||||||||||||||||||||||||||||||||
| 87 | # verify, so a parse that fails arranges nothing. | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 129 | ▶ | method prepare($value) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepare: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 129 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 12 | } | |||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 83 | ▶ | method label() { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
label: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 83 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 12 | } | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 94 | ▶ | method constraintNotes() { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
constraintNotes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 94 | return (); | |||||||||||||||||||||||||||||||||||||||||
| 98 | 12 | } | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | # Which of the shell's own completions a value of this type gets: | ||||||||||||||||||||||||||||||||||||||||||
| 101 | # 'files', 'dirs', or undef for none. | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 16 | ▶ | method completes() { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
completes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 16 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 12 | } | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 12 | class Getopt::Pad::Type::Flag :isa(Getopt::Pad::Type) :strict(params) { | |||||||||||||||||||||||||||||||||||||||||
| 108 | 12 | ▶ | use JSON::PP (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 12 | ▶ | use constant NAMES => ['flag']; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 361 | ▶ | method glSuffix() { return '' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
glSuffix: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | # Config files give JSON or YAML booleans, 1/0 or ''; an undefined spec | ||||||||||||||||||||||||||||||||||||||||||
| 115 | # default stays undefined. | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 18 | ▶ | method check($value) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 18 | ▶ | return undef if !defined $value || JSON::PP::is_bool($value) || $value =~ /\A[01]?\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not defined $value or JSON::PP::is_bool($value) or $value =~ /\A[01]?\z/u
not defined $value or JSON::PP::is_bool($value)
MC/DC66%2/3
not defined $value or JSON::PP::is_bool($value) or $value =~ /\A[01]?\z/u
not defined $value
JSON::PP::is_bool($value)
$value =~ /\A[01]?\z/u
Truth table75%3/4
not(A) or B or C
A= not defined $valueB= JSON::PP::is_bool($value)C= $value =~ /\A[01]?\z/u
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 1 | return sprintf("'%s' is not a boolean (use true or false)", $value); | |||||||||||||||||||||||||||||||||||||||||
| 119 | 12 | } | |||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 14 | ▶ | method coerce($value) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
coerce: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 14 | ▶ | return $value if !defined $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 14 | ▶ | return $value ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 12 | } | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 12 | class Getopt::Pad::Type::Bool :isa(Getopt::Pad::Type::Flag) :strict(params) { | |||||||||||||||||||||||||||||||||||||||||
| 128 | 12 | ▶ | use constant NAMES => ['!', 'bool', 'boolean']; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 49 | ▶ | method glSuffix() { return '!' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
glSuffix: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 12 | class Getopt::Pad::Type::Counter :isa(Getopt::Pad::Type) :strict(params) { | |||||||||||||||||||||||||||||||||||||||||
| 134 | 12 | ▶ | use constant NAMES => ['+', 'counter', 'count']; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 23 | ▶ | method glSuffix() { return '+' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
glSuffix: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 4 | ▶ | method check($value) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 4 | ▶ | return undef if !defined $value || $value =~ /\A[0-9]+\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $value or $value =~ /\A[0-9]+\z/u
MC/DC50%1/2
not defined $value or $value =~ /\A[0-9]+\z/u
not defined $value
$value =~ /\A[0-9]+\z/u
Truth table66%2/3
not(A) or B
A= not defined $valueB= $value =~ /\A[0-9]+\z/u
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | return sprintf("'%s' is not a count", $value); | |||||||||||||||||||||||||||||||||||||||||
| 141 | 12 | } | |||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 2 | ▶ | method coerce($value) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
coerce: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 2 | ▶ | return $value if !defined $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 2 | return $value + 0; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 12 | } | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 12 | class Getopt::Pad::Type::String :isa(Getopt::Pad::Type) :strict(params) { | |||||||||||||||||||||||||||||||||||||||||
| 150 | 12 | ▶ | use constant NAMES => ['s', 'string', 'str']; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 1057 | ▶ | method glSuffix() { return '=s' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
glSuffix: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 12 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | __END__ | ||||||||||||||||||||||||||||||||||||||||||