CLI/Argument.pm

filter: Statementstmt 100% 47 / 47 Branchbran 100% 10 / 10 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 4 / 4 totaltotal 100% 79 / 79 CC 7 SCAR 6.1
CC
7
Cov
100%
occur
3.0
validate
3.0
BUILD
2.0
CRAP 1.8
« CLI/L10N.pm ↑ Summary CLI/Base.pm »
1 #=============================================================================
2 #
3 # Module: Term::CLI::Argument
4 #
5 # Description: Generic parent class for 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 0.061000;
22  
23 19 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 19 ▶use Moo 1.000001;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 19 ▶use Term::CLI::L10N qw( loc );
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 19 ▶use Types::Standard 1.000005 qw( Int );
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 19 ▶use namespace::clean 0.25;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 19 extends 'Term::CLI::Element';
35  
36 43 ▶has min_occur => ( is => 'rw', isa => Int, default => sub {1} );
Subroutine __ANON__: called CC=1 SCAR=0.0
37 39 ▶has max_occur => ( is => 'rw', isa => Int, default => sub {1} );
Subroutine __ANON__: called CC=1 SCAR=0.0
38  
39 sub BUILD {
40 49 ▶ my ( $self, $args ) = @_;
Subroutine BUILD: called CC=2 SCAR=6.9
Pod documented
41  
42 # Allow "occur" as a shortcut for min/max setting.
43 49 ▶ if ( defined $args->{occur} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"occur"}) 6 43 49
44 6 $self->min_occur( $args->{occur} );
45 6 $self->max_occur( $args->{occur} );
46 }
47 49 return;
48 }
49  
50 sub occur {
51 5 ▶ my ( $self, @args ) = @_;
Subroutine occur: called CC=3 SCAR=11.0
Pod documented
52 5 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 3 2 5
53 3 my $min = shift @args;
54 3 ▶ my $max = @args ? shift @args : $min;
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 1 2 3
55 3 $self->min_occur($min);
56 3 $self->max_occur($max);
57 }
58 5 return ( $self->min_occur, $self->max_occur );
59 }
60  
61 sub type {
62 10 ▶ my ($self) = @_;
Subroutine type: called CC=2 SCAR=6.9
Pod documented
63 10 my $class = ref $self;
64 10 ▶ if ( $class eq 'Term::CLI::Argument' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "Term::CLI::Argument") 1 9 10
65 1 return 'GENERIC';
66 }
67 9 return $class =~ s/\A Term::CLI::Argument:://rxms;
68 }
69  
70 before validate => sub {
71 41 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
72 41 $self->clear_error;
73 6 };
74  
75 sub validate {
76 54 ▶ my ( $self, $value, $state ) = @_;
Subroutine validate: called CC=3 SCAR=11.0
Pod documented
77  
78 54 ▶ if ( not defined $value or $value eq q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value or $value eq "") 9 32 41
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
5432
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
79 22 return $self->set_error( loc('value cannot be empty') );
80 }
81 45 return $value;
82 }
83  
84 6 1;
85  
86 __END__
87  
88 =pod
89  
90 =head1 NAME
91  
92 Term::CLI::Argument - generic parent class for arguments in Term::CLI
93  
94 =head1 VERSION
95  
96 version 0.061000
97  
98 =head1 SYNOPSIS
99  
100 use Term::CLI::Argument;
101  
102 my $arg = Term::CLI::Argument->new(name => 'varname');
103  
104 =head1 DESCRIPTION
105  
106 Generic parent class for arguments in L<Term::CLI>(3p).
107 Inherits from L<Term::CLI::Element>(3p).
108  
109 =head1 CLASS STRUCTURE
110  
111 =head2 Inherits from:
112  
113 L<Term::CLI::Element>(3p).
114  
115 =head2 Consumes:
116  
117 None.
118  
119 =head1 CONSTRUCTORS
120  
121 =over
122  
123 =item B<new> ( B<name> =E<gt> I<VARNAME> ... )
124 X<new>
125  
126 Create a new Term::CLI::Argument object and return a reference to it.
127  
128 The B<name> attribute is required.
129  
130 Other possible attributes are:
131  
132 =over
133  
134 =item B<min_occur> =E<gt> I<INT>
135  
136 The minimal number of times the argument must occur.
137 A negative or zero value means there is no minimum.
138  
139 The default is C<1>.
140  
141 =item B<max_occur> =E<gt> I<INT>
142  
143 The maximum number of times the argument may occur.
144 A negative or zero value means there is no maximum.
145  
146 The default is C<1>.
147  
148 =item B<occur> =E<gt> I<INT>
149  
150 A shortcut to setting C<min_occur> and C<max_occur>
151 to the same value. Specifying this will override
152 any C<min_occur> or C<max_occur> attributes.
153  
154 =back
155  
156 =back
157  
158 =head1 METHODS
159  
160 =head2 Accessors
161  
162 Accessors are inherited from L<Term::CLI::Element>(3p).
163  
164 Additionally, there are the following:
165  
166 =over
167  
168 =item B<min_occur> ( [ I<INT> ] )
169  
170 Get or set the C<min_occur> attribute.
171  
172 =item B<max_occur> ( [ I<INT> ] )
173  
174 Get or set the C<max_occur> attribute.
175  
176 =back
177  
178 =head2 Other
179  
180 =over
181  
182 =item B<occur> ( [ I<INT> [, I<INT> ] ] )
183  
184 When called with no arguments, returns two-element list containing the
185 L<min_occur|/min_occur> and L<max_occur|/max_occur> values, respectively.
186  
187 When called with one argument, it will set both the C<min_occur> and
188 C<max_occur> attributes to the given value.
189  
190 =item B<type>
191  
192 Return the argument "type". By default, this is the object's class name
193 with the C<Term::CLI::Argument::> prefix removed. Can be overloaded to
194 provide a different value.
195  
196 =item B<validate> ( I<TEXT>, I<STATE> )
197  
198 Check whether I<TEXT> is a valid value for this object. Return the
199 (possibly normalised) value if it is valid. Otherwise, return nothing
200 (i.e. C<undef> or the empty list, depending on call context) and set the
201 L<error|/error>() attribute).
202  
203 By default, this method only checks whether I<value> is defined and not
204 an empty string.
205  
206 Sub-classes should probably override this.
207  
208 The C<validate> function can be made context-aware by inspecting the
209 I<STATE> parameter, a HashRef containing the same key/value pairs
210 as in the arguments to the
211 L<command callback|Term::CLI::Role::CommandSet|/callback>
212 function (see also
213 L<Term::CLI::Role::CommandSet|Term::CLI::Role::CommandSet>):
214  
215 STATE = {
216 status => Int,
217 error => Str,
218 options => HashRef,
219 arguments => ArrayRef[Value],
220 command_line => Str,
221 command_path => ArrayRef[InstanceOf['Term::CLI::Command']],
222 }
223  
224 The C<arguments> array holds the argument values (scalars) that were parsed up
225 until now (not including the current argument value).
226  
227 The C<command_path> array holds the L<Term::CLI::Command|Term::CLI::Command>
228 objects that were parsed up until now. This means that the command to which
229 this argument applies can be accessed with:
230  
231 CURRENT_COMMAND = STATE->{command_path}->[-1];
232  
233 The C<command_line> is the full string as it was passed from
234 L<readline|Term::CLI/readline>.
235  
236 =over 6
237  
238 =item B<NOTE:>
239  
240 Care should be taken to not modify the values in the I<STATE> HashRef.
241 Modifications may result in unexpected behaviour.
242  
243 =back
244  
245 =back
246  
247 =head1 BUNDLED SUB-CLASSES
248  
249 The L<Term::CLI|Term::CLI>(3p) distribution comes bundled with a number of
250 arugment classes.
251  
252 =over
253  
254 =item L<Term::CLI::Argument::Bool>
255  
256 Parse, complete, and validate boolean arguments.
257  
258 =item L<Term::CLI::Argument::Enum>
259  
260 Parse, complete, and validate "enum"-like arguments (pre-defined lists of words).
261  
262 =item L<Term::CLI::Argument::Filename>
263  
264 Parse, complete, and validate file/path names.
265  
266 =item L<Term::CLI::Argument::Number>
267  
268 Parse and validate numbers.
269  
270 =item L<Term::CLI::Argument::String>
271  
272 Parse and validate generic strings.
273  
274 =item L<Term::CLI::Argument::TypeTiny>
275  
276 Parse and validate L<Type::Tiny>(3p) arguments.
277  
278 =back
279  
280  
281 =head1 SEE ALSO
282  
283 L<Term::CLI>(3p),
284 L<Term::CLI::Argument::Enum>(3p),
285 L<Term::CLI::Argument::Filename>(3p),
286 L<Term::CLI::Argument::Number>(3p),
287 L<Term::CLI::Argument::String>(3p),
288 L<Term::CLI::Argument::TypeTiny>(3p),
289 L<Term::CLI::Element>(3p).
290  
291 =head1 AUTHOR
292  
293 Steven Bakker E<lt>sbakker@cpan.orgE<gt>, 2018-2022.
294  
295 =head1 COPYRIGHT AND LICENSE
296  
297 Copyright (c) 2018 Steven Bakker
298  
299 This module is free software; you can redistribute it and/or modify
300 it under the same terms as Perl itself. See "perldoc perlartistic."
301  
302 This software is distributed in the hope that it will be useful,
303 but WITHOUT ANY WARRANTY; without even the implied warranty of
304 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
305  
306 =begin __PODCOVERAGE
307  
308 =head1 THIS SECTION SHOULD BE HIDDEN
309  
310 This section is meant for methods that should not be considered
311 for coverage. This typically includes things like BUILD and DEMOLISH from
312 Moo/Moose. It is possible to skip these when using the Pod::Coverage class
313 (using C<also_private>), but this is not an option when running C<cover>
314 from the command line.
315  
316 The simplest trick is to add a hidden section with an item list containing
317 these methods.
318  
319 =over
320  
321 =item BUILD
322  
323 =item DEMOLISH
324  
325 =back
326  
327 =end __PODCOVERAGE
328  
329 =cut
« CLI/L10N.pm ↑ Summary CLI/Base.pm »