CLI/Argument/Enum.pm

filter: Statementstmt 100% 59 / 59 Branchbran 100% 18 / 18 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 97.9% 97 / 99 CC 11 SCAR 8.8
CC
11
Cov
99%
values
4.0
validate
4.0
__ANON__
3.0
CRAP 2.4
« CLI/Role/ArgumentSet.pm ↑ Summary CLI/Argument/TypeTiny.pm »
1 #=============================================================================
2 #
3 # Module: Term::CLI::Argument::Enum
4 #
5 # Description: Class for "enum" arguments in Term::CLI
6 #
7 # Author: Steven Bakker (SBAKKER), <sbakker@cpan.org>
8 # Created: 22/Jan/2018
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::Enum 0.061000;
22  
23 10 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 10 ▶use Term::CLI::L10N qw( loc );
Subroutine BEGIN: called CC=1 SCAR=0.0
27 10 ▶use Term::CLI::Util qw( is_prefix_str find_text_matches );
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 10 use Types::Standard 1.000005 qw(
30 ArrayRef
31 CodeRef
32 10 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 10 ▶use Moo 1.000001;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 10 ▶use List::Util 1.23 qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
36 10 ▶use Scalar::Util 1.23 qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 10 ▶use namespace::clean 0.25;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 10 extends 'Term::CLI::Argument';
41  
42 has value_list => (
43 is => 'ro',
44 isa => ArrayRef | CodeRef,
45 required => 1,
46 coerce => sub {
47 15 ▶ my ($arg) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
48 15 ▶ return $arg if ref $arg && reftype $arg ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $arg and builtin::reftype($arg) ne "ARRAY" 2 13 15
Condition66%2/3
ref $arg and builtin::reftype($arg) ne "ARRAY"
!ll&&!rl&&r
0132
MC/DC50%1/2
ref $arg and builtin::reftype($arg) ne "ARRAY"
ref $arg builtin::reftype($arg) ne "ARRAY"
Truth table66%2/3
A and B
A= ref $argB= builtin::reftype($arg) ne "ARRAY"
ABresult
0X0
100
111
49 13 return [ sort @$arg ];
50 }
51 10 );
52  
53 has cache_values => (
54 is => 'rw',
55 default => 0,
56 # Trigger for when caching gets disabled to immediately clear the
57 # cache. This allows for quicker cleanup of objects, file handles, etc.
58 trigger => sub {
59 2 ▶ my ($self, $new) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
60 2 ▶ $self->_clear_value_cache if ! $new;
Branch: 1 branch
BranchTrueFalseTotal
if not $new 1 1 2
61 }
62 10 );
63  
64 10 has _value_cache => (
65 is => 'rw',
66 isa => ArrayRef,
67 predicate => 1,
68 clearer => 1,
69 );
70  
71 sub values {
72 29 ▶ my ($self) = @_;
Subroutine values: called CC=4 SCAR=13.9
Pod documented
73  
74 29 my $value_list = $self->value_list;
75 29 ▶ return $value_list if reftype $value_list eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($value_list) eq "ARRAY" 18 11 29
76  
77 # Dynamic values...
78  
79 # Return cache if possible.
80 11 ▶ if ( $self->_has_value_cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_value_cache) 1 10 11
81 1 return $self->_value_cache;
82 }
83  
84 10 my $list_ref = [ sort @{ $value_list->($self) } ];
85  
86 10 ▶ if ( $self->cache_values ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache_values) 1 9 10
87 1 $self->_value_cache( $list_ref );
88 }
89  
90 10 return $list_ref;
91 }
92  
93  
94 sub validate {
95 14 ▶ my ( $self, $value ) = @_;
Subroutine validate: called CC=4 SCAR=13.9
Pod documented
96  
97 14 ▶ defined $self->SUPER::validate($value) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->SUPER::validate($value) or return 4 10 14
98  
99 10 my $values_r = $self->values;
100  
101 10 my @found = find_text_matches( $value, $values_r, exact => 1 );
102  
103 10 ▶ if ( @found == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@found == 0) 3 7 10
104 3 return $self->set_error( loc("not a valid value") );
105 }
106  
107 7 ▶ return $found[0] if @found == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @found == 1 5 2 7
108  
109 2 return $self->set_error(
110 loc( "ambiguous value (matches: [_1])", join( ", ", @found ) ) );
111 }
112  
113 sub complete {
114 15 ▶ my ( $self, $text ) = @_;
Subroutine complete: called CC=2 SCAR=6.9
Pod documented
115  
116 15 my $values_r = $self->values;
117  
118 15 ▶ return @{$values_r} if !length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 2 13 15
119 13 return find_text_matches( $text, $values_r );
120 }
121  
122 10 1;
123  
124 __END__
125  
126 =pod
127  
128 =head1 NAME
129  
130 Term::CLI::Argument::Enum - class for "enum" string arguments in Term::CLI
131  
132 =head1 VERSION
133  
134 version 0.061000
135  
136 =head1 SYNOPSIS
137  
138 use Term::CLI::Argument::Enum;
139  
140 # static value list
141 my $arg = Term::CLI::Argument::Enum->new(
142 name => 'arg1',
143 value_list => [qw( foo bar baz )],
144 );
145  
146 my $val_list = $arg->values; # returns ['bar', 'baz', 'foo']
147  
148 # dynamic value list
149 my $arg = Term::CLI::Argument::Enum->new(
150 name => 'arg1',
151 value_list => sub { my @values = (...); \@values },
152 );
153  
154 =head1 DESCRIPTION
155  
156 Class for "enum" string arguments in L<Term::CLI>(3p).
157  
158 This class inherits from
159 the L<Term::CLI::Argument>(3p) class.
160  
161 =head1 CLASS STRUCTURE
162  
163 =head2 Inherits from:
164  
165 L<Term::CLI::Argument>(3p).
166  
167 =head2 Consumes:
168  
169 None.
170  
171 =head1 CONSTRUCTORS
172  
173 =over
174  
175 =item B<new>
176  
177 OBJ = Term::CLI::Argument::Enum(
178 name => STRING,
179 value_list => ArrayRef | CodeRef,
180 cache_values => BOOL,
181 );
182  
183 See also L<Term::CLI::Argument>(3p). The B<value_list> argument is
184 mandatory and can either be a reference to an array, or a code refrerence.
185  
186 A value list consisting of a code reference can be used to implement
187 dynamic values or delayed expansion (where the values have to be
188 fetched from a database or remote system). The code reference will
189 be called with a single argument consisting of the reference to the
190 C<Term::CLI::Argument::Enum|Term::CLI::Argument::Enum>
191 object.
192  
193 The C<cache_values> attribute can be set to a true value to prevent
194 repeated calls to the C<value_list> code reference. For dynamic value
195 lists this is not desired, but for lists that are generated through
196 expensive queries, this can be useful. The default is 0 (false).
197  
198 =back
199  
200 =head1 ACCESSORS
201  
202 See also L<Term::CLI::Argument>(3p).
203  
204 =over
205  
206 =item B<value_list>
207 X<value_list>
208  
209 A reference to a either a list of valid values for the argument or a
210 subroutine which returns a reference to such a list.
211  
212 Note that once set, changing the list pointed to by an I<ArrayRef>
213 will result in undefined behaviour.
214  
215 =item B<cache_values>
216 X<cache_values>
217  
218 =item B<cache_values> ( [ I<BOOL> ] )
219  
220 Returns or sets whether the value list should be cached in case
221 C<value_list> is a code reference.
222  
223 For dynamic value lists this should be false, but for lists that are
224 generated through expensive queries, it can be useful to set this to
225 true.
226  
227 If the value is changed from true to false, any cached list is
228 immediately cleared.
229  
230 =back
231  
232 =head1 METHODS
233  
234 See also L<Term::CLI::Argument>(3p).
235  
236 The following methods are added or overloaded:
237  
238 =over
239  
240 =item B<validate>
241  
242 =item B<complete>
243  
244 Overloaded from L<Term::CLI::Argument>(3p).
245  
246 =item B<values>
247  
248 Returns an ArrayRef containing a sorted list of valid values for this
249 argument object.
250  
251 In case L<value_list|/value_list> is a CodeRef, it will call the
252 code to expand the list, sort it, and return the result.
253  
254 =back
255  
256 =head1 EXAMPLES
257  
258 Return values depending on the time of day:
259  
260 # Valid values for 'at' depend on the current time of day.
261 # Before 1pm, 'today' is possible, otherwise only 'tomorrow'.
262 my $arg = Term::CLI::Argument::Enum(
263 name => 'at',
264 value_list => sub {
265 my ($self) = @_;
266 my $hr = (localtime)[2];
267 return ($hr < 13) ? ['today', 'tomorrow'] : ['tomorrow'];
268 }
269 );
270  
271 Return values based on a predefined list of values that can be
272 (temporarily) overridden with C<local()>:
273  
274 my @LIST = qw( one two three );
275  
276 my $arg = Term::CLI::Argument::Enum(
277 name => 'arg',
278 value_list => sub { return \@LIST }
279 );
280  
281 ...
282  
283 # Will allow 'one', 'two', 'three' for 'arg'.
284 $cli->execute($cli->readline);
285  
286 {
287 local(@LIST) = qw( four five six );
288 # Now allow 'four', 'five', 'six' for 'arg'.
289 $cli->execute($cli->readline);
290 }
291  
292 # Allow 'one', 'two', 'three' for 'arg' again.
293 $cli->execute($cli->readline);
294  
295 =head1 SEE ALSO
296  
297 L<Term::CLI::Argument>(3p),
298 L<Term::CLI>(3p).
299  
300 =head1 AUTHOR
301  
302 Steven Bakker E<lt>sbakker@cpan.orgE<gt>, 2018.
303  
304 =head1 COPYRIGHT AND LICENSE
305  
306 Copyright (c) 2018-2022 Steven Bakker
307  
308 This module is free software; you can redistribute it and/or modify
309 it under the same terms as Perl itself. See "perldoc perlartistic."
310  
311 This software is distributed in the hope that it will be useful,
312 but WITHOUT ANY WARRANTY; without even the implied warranty of
313 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
314  
315 =cut
« CLI/Role/ArgumentSet.pm ↑ Summary CLI/Argument/TypeTiny.pm »