| 1 | #============================================================================= | ||||||||||||
| 2 | # | ||||||||||||
| 3 | # Module: Term::CLI::Argument::TypeTiny | ||||||||||||
| 4 | # | ||||||||||||
| 5 | # Description: Class for Type::Tiny validated arguments in Term::CLI | ||||||||||||
| 6 | # | ||||||||||||
| 7 | # Author: Diab Jerius (DJERIUS), <djerius@cpan.org> | ||||||||||||
| 8 | # Created: 16/Apr/2022 | ||||||||||||
| 9 | # | ||||||||||||
| 10 | # Copyright (c) 2022 Diab Jerius, Smithsonian Astrophysical Observatory | ||||||||||||
| 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::TypeTiny 0.061000; | ||||||||||||
| 22 | |||||||||||||
| 23 | 1 | use Types::Standard 1.000005 qw( | |||||||||||
| 24 | Bool | ||||||||||||
| 25 | InstanceOf | ||||||||||||
| 26 | 1 | ▶ | ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 27 | |||||||||||||
| 28 | 1 | ▶ | use Moo 1.000001; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 29 | |||||||||||||
| 30 | 1 | ▶ | use namespace::clean 0.25; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 31 | |||||||||||||
| 32 | 1 | extends 'Term::CLI::Argument'; | |||||||||||
| 33 | |||||||||||||
| 34 | 1 | has coerce => ( | |||||||||||
| 35 | is => 'ro', | ||||||||||||
| 36 | isa => Bool, | ||||||||||||
| 37 | default => 0, | ||||||||||||
| 38 | ); | ||||||||||||
| 39 | |||||||||||||
| 40 | 1 | has typetiny => ( | |||||||||||
| 41 | is => 'ro', | ||||||||||||
| 42 | isa => InstanceOf ['Type::Tiny'], | ||||||||||||
| 43 | required => 1, | ||||||||||||
| 44 | ); | ||||||||||||
| 45 | |||||||||||||
| 46 | sub validate { | ||||||||||||
| 47 | 2 | ▶ | my ( $self, $value ) = @_; | ||||||||||
|
Subroutine
validate: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||
| 48 | |||||||||||||
| 49 | 2 | ▶ | $value = $self->typetiny->coerce( $value ) if $self->coerce; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 50 | 2 | ▶ | my $error = $self->typetiny->validate( $value ) // return $value; | ||||||||||
|
Condition50%1/2
$self->typetiny->validate($value) // (return $value)
MC/DC0%0/1
$self->typetiny->validate($value) // (return $value)
$self->typetiny->validate($value)
Truth table50%1/2
A
A= $self->typetiny->validate($value)
| |||||||||||||
| 51 | 0 | return $self->set_error( $error ); | |||||||||||
| 52 | } | ||||||||||||
| 53 | |||||||||||||
| 54 | |||||||||||||
| 55 | 1 | 1; | |||||||||||
| 56 | |||||||||||||
| 57 | __END__ | ||||||||||||
| 58 | |||||||||||||
| 59 | =pod | ||||||||||||
| 60 | |||||||||||||
| 61 | =head1 NAME | ||||||||||||
| 62 | |||||||||||||
| 63 | Term::CLI::Argument::TypeTiny - class for Type::Tiny validated arguments in Term::CLI | ||||||||||||
| 64 | |||||||||||||
| 65 | =head1 VERSION | ||||||||||||
| 66 | |||||||||||||
| 67 | version 0.061000 | ||||||||||||
| 68 | |||||||||||||
| 69 | =head1 SYNOPSIS | ||||||||||||
| 70 | |||||||||||||
| 71 | use Term::CLI::Argument::TypeTiny; | ||||||||||||
| 72 | use Types::Standard qw( ArrayRef Split ); | ||||||||||||
| 73 | use Types::Common::String qw( NonEmptyStr ); | ||||||||||||
| 74 | |||||||||||||
| 75 | # accept a comma separated list of strings, split them and return an array | ||||||||||||
| 76 | my $arg = Term::CLI::Argument::TypeTiny->new( | ||||||||||||
| 77 | name => 'arg1', | ||||||||||||
| 78 | typetiny => ArrayRef->of(NonEmptyStr)->plus_coercions(Split[qr/,/]), | ||||||||||||
| 79 | coerce => 1, | ||||||||||||
| 80 | ); | ||||||||||||
| 81 | |||||||||||||
| 82 | |||||||||||||
| 83 | =head1 DESCRIPTION | ||||||||||||
| 84 | |||||||||||||
| 85 | Class for Type::Tiny validated arguments in L<Term::CLI|Term::CLI>(3p). | ||||||||||||
| 86 | |||||||||||||
| 87 | This class inherits from | ||||||||||||
| 88 | the L<Term::CLI::Argument|Term::CLI::Argument>(3p) class. | ||||||||||||
| 89 | |||||||||||||
| 90 | =head1 CLASS STRUCTURE | ||||||||||||
| 91 | |||||||||||||
| 92 | =head2 Inherits from: | ||||||||||||
| 93 | |||||||||||||
| 94 | L<Term::CLI::Argument|Term::CLI::Argument>(3p). | ||||||||||||
| 95 | |||||||||||||
| 96 | =head2 Consumes: | ||||||||||||
| 97 | |||||||||||||
| 98 | None. | ||||||||||||
| 99 | |||||||||||||
| 100 | =head1 CONSTRUCTORS | ||||||||||||
| 101 | |||||||||||||
| 102 | =over | ||||||||||||
| 103 | |||||||||||||
| 104 | =item B<new> | ||||||||||||
| 105 | |||||||||||||
| 106 | OBJ = Term::CLI::Argument::TypeTiny( | ||||||||||||
| 107 | name => STRING, | ||||||||||||
| 108 | typetiny => InstanceOf['Type::Tiny'], | ||||||||||||
| 109 | coerce => Bool, | ||||||||||||
| 110 | ); | ||||||||||||
| 111 | |||||||||||||
| 112 | See also L<Term::CLI::Argument|Term::CLI::Argument>(3p). The B<typetiny> | ||||||||||||
| 113 | argument is mandatory and must be a L<Type::Tiny|Type::Tiny>(3p) object. It | ||||||||||||
| 114 | will be used to validate values. | ||||||||||||
| 115 | |||||||||||||
| 116 | If B<coerce> is true, L<validate|/validate> will call the | ||||||||||||
| 117 | L<Type::Tiny|Type::Tiny> object's coercion method to coerce the input value. | ||||||||||||
| 118 | |||||||||||||
| 119 | =back | ||||||||||||
| 120 | |||||||||||||
| 121 | =head1 ACCESSORS | ||||||||||||
| 122 | |||||||||||||
| 123 | See also L<Term::CLI::Argument|Term::CLI::Argument>(3p). | ||||||||||||
| 124 | |||||||||||||
| 125 | =over | ||||||||||||
| 126 | |||||||||||||
| 127 | =item B<typetiny> | ||||||||||||
| 128 | |||||||||||||
| 129 | The L<Type::Tiny|Type::Tiny> object which will validate values. | ||||||||||||
| 130 | |||||||||||||
| 131 | =item B<coerce> | ||||||||||||
| 132 | |||||||||||||
| 133 | Boolean that indicates whether L<validate|/validate> will call the | ||||||||||||
| 134 | L<Type::Tiny|Type::Tiny> object's coercion method to coerce the input value. | ||||||||||||
| 135 | |||||||||||||
| 136 | =back | ||||||||||||
| 137 | |||||||||||||
| 138 | =head1 METHODS | ||||||||||||
| 139 | |||||||||||||
| 140 | See also L<Term::CLI::Argument|Term::CLI::Argument>(3p). | ||||||||||||
| 141 | |||||||||||||
| 142 | The following methods are added or overloaded: | ||||||||||||
| 143 | |||||||||||||
| 144 | =over | ||||||||||||
| 145 | |||||||||||||
| 146 | =item B<validate> | ||||||||||||
| 147 | |||||||||||||
| 148 | Overloaded from L<Term::CLI::Argument|Term::CLI::Argument>(3p). | ||||||||||||
| 149 | |||||||||||||
| 150 | =back | ||||||||||||
| 151 | |||||||||||||
| 152 | =head1 EXAMPLES | ||||||||||||
| 153 | |||||||||||||
| 154 | =over | ||||||||||||
| 155 | |||||||||||||
| 156 | =item * | ||||||||||||
| 157 | |||||||||||||
| 158 | Only allow positive or zero numbers for # of zombies seen. | ||||||||||||
| 159 | Allow non integers, in case they've lost bits and pieces: | ||||||||||||
| 160 | |||||||||||||
| 161 | use Types::Common::Numeric qw( PositiveOrZeroNum); | ||||||||||||
| 162 | my $arg = Term::CLI::Argument::TypeTiny( | ||||||||||||
| 163 | name => 'zombies', | ||||||||||||
| 164 | typetiny => PositiveOrZeroNum | ||||||||||||
| 165 | ); | ||||||||||||
| 166 | |||||||||||||
| 167 | =item * | ||||||||||||
| 168 | |||||||||||||
| 169 | Accept a URI, convert to a L<URI|URI> object: | ||||||||||||
| 170 | |||||||||||||
| 171 | use Types::URI 'URI'; | ||||||||||||
| 172 | my $arg = Term::CLI::Argument::TypeTiny( | ||||||||||||
| 173 | name => 'uri', | ||||||||||||
| 174 | typetiny => URI, | ||||||||||||
| 175 | coerce => 1 | ||||||||||||
| 176 | ); | ||||||||||||
| 177 | |||||||||||||
| 178 | =item * | ||||||||||||
| 179 | |||||||||||||
| 180 | Accept a file which must exist, convert to a L<Path::Tiny|Path::Tiny> | ||||||||||||
| 181 | object: | ||||||||||||
| 182 | |||||||||||||
| 183 | use Types::Path::Tiny 'File'; | ||||||||||||
| 184 | my $arg = Term::CLI::Argument::TypeTiny( | ||||||||||||
| 185 | name => 'file', | ||||||||||||
| 186 | typetiny => File, | ||||||||||||
| 187 | coerce => 1 | ||||||||||||
| 188 | ); | ||||||||||||
| 189 | |||||||||||||
| 190 | =back | ||||||||||||
| 191 | |||||||||||||
| 192 | =head1 SEE ALSO | ||||||||||||
| 193 | |||||||||||||
| 194 | L<Term::CLI::Argument|Term::CLI::Argument>(3p), | ||||||||||||
| 195 | L<Term::CLI|Term::CLI>(3p), | ||||||||||||
| 196 | L<Type::Tiny|Type::Tiny>(3p). | ||||||||||||
| 197 | |||||||||||||
| 198 | =head1 AUTHOR | ||||||||||||
| 199 | |||||||||||||
| 200 | Diab Jerius E<lt>djerius@cpan.orgE<gt>, 2022. | ||||||||||||
| 201 | |||||||||||||
| 202 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 203 | |||||||||||||
| 204 | Copyright (c) 2022 Diab Jerius, Smithsonian Astrophysical Observatory | ||||||||||||
| 205 | |||||||||||||
| 206 | This module is free software; you can redistribute it and/or modify | ||||||||||||
| 207 | it under the same terms as Perl itself. See "perldoc perlartistic." | ||||||||||||
| 208 | |||||||||||||
| 209 | This software is distributed in the hope that it will be useful, | ||||||||||||
| 210 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||
| 211 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||||||||||||
| 212 | |||||||||||||
| 213 | =cut | ||||||||||||