| 1 | #============================================================================= | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Module: Term::CLI::Argument::Number | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # Description: Base class for numerical arguments in Term::CLI | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # Author: Steven Bakker (SBAKKER), <sbakker@cpan.org> | ||||||||||||||||||||||||||||
| 8 | # Created: 22/01/18 | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # Copyright (c) 2018-2022 Steven Bakker | ||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | # This module is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 13 | # it under the same terms as Perl itself. See "perldoc perlartistic." | ||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||
| 15 | # This software is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||||||||||||||||||||||||||||
| 18 | # | ||||||||||||||||||||||||||||
| 19 | #============================================================================= | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | package Term::CLI::Argument::Number 0.061000; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 9 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 9 | ▶ | use Term::CLI::L10N qw( loc ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 9 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 9 | ▶ | use Moo 1.000001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 9 | ▶ | use namespace::clean 0.25; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 9 | extends 'Term::CLI::Argument'; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 9 | has min => ( is => 'rw', clearer => 1, predicate => 1 ); | |||||||||||||||||||||||||||
| 35 | 9 | has max => ( is => 'rw', clearer => 1, predicate => 1 ); | |||||||||||||||||||||||||||
| 36 | 3 | ▶ | has inclusive => ( is => 'rw', default => sub {1} ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub coerce_value { | ||||||||||||||||||||||||||||
| 39 | 1 | ▶ | croak "coerce_value() has not been overloaded"; | ||||||||||||||||||||||||||
|
Subroutine
coerce_value: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | sub validate { | ||||||||||||||||||||||||||||
| 43 | 32 | ▶ | my ( $self, $value ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=13 SCAR=25.6
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 32 | ▶ | if ( !defined $value || length($value) == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $value or length $value == 0
MC/DC100%2/2
not defined $value or length $value == 0
not defined $value
length $value == 0
Truth table100%3/3
not(A) or B
A= not defined $valueB= length $value == 0
| |||||||||||||||||||||||||||||
| 46 | 3 | return $self->set_error( loc('not a valid number') ); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 29 | my $num = $self->coerce_value($value); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 28 | ▶ | if ( !defined $num ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 2 | return $self->set_error( loc('not a valid number') ); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 26 | ▶ | if ( $self->inclusive ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 18 | ▶ | if ( $self->has_min && $num < $self->min ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->has_min and $num < $self->min
MC/DC100%2/2
$self->has_min and $num < $self->min
$self->has_min
$num < $self->min
Truth table100%3/3
A and B
A= $self->has_minB= $num < $self->min
| |||||||||||||||||||||||||||||
| 57 | 3 | return $self->set_error( loc('too small') ); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 15 | ▶ | if ( $self->has_max && $num > $self->max ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->has_max and $num > $self->max
MC/DC100%2/2
$self->has_max and $num > $self->max
$self->has_max
$num > $self->max
Truth table100%3/3
A and B
A= $self->has_maxB= $num > $self->max
| |||||||||||||||||||||||||||||
| 60 | 3 | return $self->set_error( loc('too large') ); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | else { | ||||||||||||||||||||||||||||
| 64 | 8 | ▶ | if ( $self->has_min && $num <= $self->min ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->has_min and $num <= $self->min
MC/DC100%2/2
$self->has_min and $num <= $self->min
$self->has_min
$num <= $self->min
Truth table100%3/3
A and B
A= $self->has_minB= $num <= $self->min
| |||||||||||||||||||||||||||||
| 65 | 3 | return $self->set_error( loc('too small') ); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 5 | ▶ | if ( $self->has_max && $num >= $self->max ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->has_max and $num >= $self->max
MC/DC100%2/2
$self->has_max and $num >= $self->max
$self->has_max
$num >= $self->max
Truth table100%3/3
A and B
A= $self->has_maxB= $num >= $self->max
| |||||||||||||||||||||||||||||
| 68 | 3 | return $self->set_error( loc('too large') ); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 14 | return $num; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 9 | 1; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | __END__ | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =pod | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head1 NAME | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | Term::CLI::Argument::Number - base class for numerical arguments in Term::CLI | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head1 VERSION | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | version 0.061000 | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | use Term::CLI::Argument::Number; | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | my $arg = Term::CLI::Argument::Number->new( | ||||||||||||||||||||||||||||
| 93 | name => 'arg1', | ||||||||||||||||||||||||||||
| 94 | min => 1 | ||||||||||||||||||||||||||||
| 95 | max => 2 | ||||||||||||||||||||||||||||
| 96 | inclusive => 1 | ||||||||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | Base class for numerical arguments in L<Term::CLI>(3p). This class cannot | ||||||||||||||||||||||||||||
| 102 | be used directly, but should be extended by sub-classes. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head1 CLASS STRUCTURE | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head2 Inherits from: | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | L<Term::CLI::Argument>(3p). | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head2 Consumes: | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | None. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head1 CONSTRUCTORS | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =over | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =item B<new> ( B<name> =E<gt> I<VARNAME>, ... ) | ||||||||||||||||||||||||||||
| 119 | X<new> | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | Create a new Term::CLI::Argument::Number object and return a reference | ||||||||||||||||||||||||||||
| 122 | to it. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | The B<name> attribute is required. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Other attributes that are recognised: | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =over | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =item B<min> =E<gt> I<NUM> | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | The minimum valid value (by default an I<inclusive> boundary, | ||||||||||||||||||||||||||||
| 133 | but see L<inclusive|/inclusive> below. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =item B<max> =E<gt> I<NUM> | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | The maximum valid value (by default an I<inclusive> boundary, | ||||||||||||||||||||||||||||
| 138 | but see L<inclusive|/inclusive> below. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =item B<inclusive> =E<gt> I<BOOLEAN> | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Default is 1 (true). Indicates whether minimum/maximum boundaries | ||||||||||||||||||||||||||||
| 143 | are inclusive or exclusive. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =back | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =back | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head1 ACCESSORS | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | Inherited from L<Term::CLI::Argument>(3p). Additionally, the | ||||||||||||||||||||||||||||
| 152 | following are defined: | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =over | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =item B<min> ( I<NUMBER> ) | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =item B<max> ( I<NUMBER> ) | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | Lower and upper boundaries, resp. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =item B<inclusive> ( I<BOOL> ) | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | Boolean indicating whether the boundaries are inclusive. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =item B<has_min> | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =item B<has_max> | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | Booleans, indicate whether C<min> and C<max> have been set, resp. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =item B<clear_min> | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =item B<clear_max> | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | Clear the C<min> and C<max> limits, resp. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =back | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head1 METHODS | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | Inherited from L<Term::CLI::Argument>(3p). | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | Additionally: | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =over | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =item B<validate> ( I<VALUE> ) | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | The L<validate|Term::CLI::Argument/validate> method uses the | ||||||||||||||||||||||||||||
| 191 | L<coerce_value|/coerce_value> method to convert I<VALUE> to | ||||||||||||||||||||||||||||
| 192 | a suitable number and then checks any boundaries. | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =item B<coerce_value> ( I<VALUE> ) | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | This method I<must> be overridden by sub-classes. | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | It will be called with a single argument (the I<VALUE>) and is | ||||||||||||||||||||||||||||
| 199 | supposed to return the converted number. If the number is not | ||||||||||||||||||||||||||||
| 200 | valid, it should return C<undef>. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =back | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | L<Term::CLI::Argument>(3p), | ||||||||||||||||||||||||||||
| 207 | L<Term::CLI>(3p). | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | Steven Bakker E<lt>sbakker@cpan.orgE<gt>, 2018. | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | Copyright (c) 2018 Steven Bakker | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | This module is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 218 | it under the same terms as Perl itself. See "perldoc perlartistic." | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | This software is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||
| 221 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||
| 222 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | =cut | ||||||||||||||||||||||||||||