CLI/Argument/String.pm

filter: Statementstmt 100% 30 / 30 Branchbran 100% 6 / 6 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 100% 54 / 54 CC 6 SCAR 12.5
CC
6
Cov
100%
validate
6.0
BEGIN
1.0
BEGIN
1.0
CRAP 3.5
« CLI/Argument/Bool.pm ↑ Summary CLI/Role/ArgumentSet.pm »
1 #=============================================================================
2 #
3 # Module: Term::CLI::Argument::String
4 #
5 # Description: Class for arbitrary string 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::String 0.061000;
22  
23 7 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 7 ▶use Term::CLI::L10N qw( loc );
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 7 ▶use Types::Standard 1.000005 qw( Int );
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 7 ▶use Moo 1.000001;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 7 ▶use namespace::clean 0.25;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 7 extends 'Term::CLI::Argument';
34  
35 7 has min_len => ( is => 'rw', isa => Int, clearer => 1, predicate => 1 );
36 7 has max_len => ( is => 'rw', isa => Int, clearer => 1, predicate => 1 );
37  
38 sub validate {
39 36 ▶ my ( $self, $value ) = @_;
Subroutine validate: called CC=6 SCAR=17.9
Pod documented
40  
41 36 $self->clear_error;
42  
43 36 ▶ if ( !defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 1 35 36
44 1 return $self->set_error( loc('value must be defined') );
45 }
46  
47 35 ▶ if ( $self->has_min_len && length $value < $self->min_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_min_len and length $value < $self->min_len) 1 34 35
Condition100%3/3
$self->has_min_len and length $value < $self->min_len
!ll&&!rl&&r
3221
MC/DC100%2/2
$self->has_min_len and length $value < $self->min_len
$self->has_min_len length $value < $self->min_len
Truth table100%3/3
A and B
A= $self->has_min_lenB= length $value < $self->min_len
ABresult
0X0
100
111
48 1 return $self->set_error(
49 loc( "too short (min. length [_1])", $self->min_len ) );
50 }
51 34 ▶ if ( $self->has_max_len && length $value > $self->max_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_max_len and length $value > $self->max_len) 1 33 34
Condition100%3/3
$self->has_max_len and length $value > $self->max_len
!ll&&!rl&&r
3211
MC/DC100%2/2
$self->has_max_len and length $value > $self->max_len
$self->has_max_len length $value > $self->max_len
Truth table100%3/3
A and B
A= $self->has_max_lenB= length $value > $self->max_len
ABresult
0X0
100
111
52 1 return $self->set_error(
53 loc( "too long (max. length [_1])", $self->max_len ) );
54 }
55 33 return $value;
56 }
57  
58 7 1;
59  
60 __END__
61  
62 =pod
63  
64 =head1 NAME
65  
66 Term::CLI::Argument::String - class for basic string arguments in Term::CLI
67  
68 =head1 VERSION
69  
70 version 0.061000
71  
72 =head1 SYNOPSIS
73  
74 use Term::CLI::Argument::String;
75  
76 my $arg1 = Term::CLI::Argument::String->new(
77 name => 'arg1'
78 );
79  
80 $arg1->validate(''); # returns ''
81 $arg1->validate('a'); # returns 'a'
82 $arg1->validate('abcde'); # returns 'abcde'
83  
84 my $arg2 = Term::CLI::Argument::String->new(
85 name => 'arg2'
86 min_length => 1,
87 max_length => 4,
88 );
89  
90 $arg2->validate(''); # returns undef, sets error
91 $arg2->validate('a'); # returns 'a'
92 $arg2->validate('abcde'); # returns undef, sets error
93  
94 =head1 DESCRIPTION
95  
96 Simple class for string arguments in L<Term::CLI>(3p). This is basically
97 the L<Term::CLI::Argument>(3p) class, but also allowing empty strings.
98  
99 =head1 CLASS STRUCTURE
100  
101 =head2 Inherits from:
102  
103 L<Term::CLI::Argument>(3p).
104  
105 =head2 Consumes:
106  
107 None.
108  
109 =head1 CONSTRUCTORS
110  
111 See L<Term::CLI::Argument>(3p). Additional attributes are:
112  
113 =over
114  
115 =item B<min_len> =E<gt> I<NUM>
116  
117 The minimum required length for any value.
118  
119 =item B<max_len> =E<gt> I<NUM>
120  
121 The maximum lenght allowed for any value.
122  
123 =back
124  
125 =head1 ACCESSORS
126  
127 Inherited from L<Term::CLI::Argument>(3p). Additionally, the
128 following are defined:
129  
130 =over
131  
132 =item B<min_len> ( I<NUMBER> )
133  
134 =item B<max_len> ( I<NUMBER> )
135  
136 Minimum and maximum length for the string, resp.
137  
138 =item B<has_min_len>
139  
140 =item B<has_max_len>
141  
142 Booleans, indicate whether C<min_len> and C<max_len> have been set,
143 resp.
144  
145 =item B<clear_min_len>
146  
147 =item B<clear_max_len>
148  
149 Clear the C<min_len> and C<max_len> limits, resp.
150  
151 =back
152  
153 See L<Term::CLI::Argument>(3p).
154  
155 =head1 METHODS
156  
157 See L<Term::CLI::Argument>(3p).
158  
159 =over
160  
161 =item B<validate> ( I<Str> )
162  
163 Overloaded from L<Term::CLI::Argument>.
164  
165 Requires the I<Str> value to be defined, and have a length
166 that is between C<min_len> and C<max_len> (if defined).
167  
168 =back
169  
170 =head1 SEE ALSO
171  
172 L<Term::CLI::Argument>(3p),
173 L<Term::CLI>(3p).
174  
175 =head1 AUTHOR
176  
177 Steven Bakker E<lt>sbakker@cpan.orgE<gt>, 2018.
178  
179 =head1 COPYRIGHT AND LICENSE
180  
181 Copyright (c) 2018 Steven Bakker
182  
183 This module is free software; you can redistribute it and/or modify
184 it under the same terms as Perl itself. See "perldoc perlartistic."
185  
186 This software is distributed in the hope that it will be useful,
187 but WITHOUT ANY WARRANTY; without even the implied warranty of
188 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
189  
190 =cut
« CLI/Argument/Bool.pm ↑ Summary CLI/Role/ArgumentSet.pm »