CLI/Argument/Tree.pm

filter: Statementstmt 98.1% 54 / 55 Branchbran 90.9% 20 / 22 Conditioncond 55.5% 5 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 89.4% 93 / 104 CC 16 SCAR 13.2
CC
16
Cov
92%
validate
5.4
complete
5.0
_descend_tree
4.1
CRAP 3.7
« CLI/Role/CommandSet.pm ↑ Summary CLI/Argument/Bool.pm »
1 #=============================================================================
2 #
3 # Module: Term::CLI::Argument::Tree
4 #
5 # Description: Class for nested arguments in Term::CLI
6 #
7 # Author: Diab Jerius (DJERIUS), <djerius@cpan.org>
8 # Created: 19/April/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::Tree 0.061000;
22  
23 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 ▶use Scalar::Util qw( refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Term::CLI::Util qw( find_text_matches );
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Types::Standard qw( CodeRef HashRef InstanceOf );
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 2 extends 'Term::CLI::Argument';
32  
33 2 has +max_occur => (
34 is => 'ro',
35 default => 0,
36 init_arg => undef,
37 );
38  
39 2 has _values => (
40 is => 'ro',
41 required => 1,
42 init_arg => 'values',
43 isa => CodeRef | HashRef
44 );
45  
46 has cache_values => (
47 is => 'rw',
48 default => 0,
49 trigger => sub {
50 4 ▶ my ( $self, $value ) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
51 4 ▶ $self->_clear_value_cache if !$value;
Branch: 1 branch
BranchTrueFalseTotal
if not $value 2 2 4
52 },
53 2 );
54  
55 2 has _value_cache => (
56 is => 'rw',
57 isa => HashRef,
58 predicate => 1,
59 clearer => 1,
60 );
61  
62 sub values {
63 37 ▶ my ($self) = shift;
Subroutine values: called CC=4 SCAR=13.9
Pod documented
64 37 my $values = $self->_values;
65 37 ▶ return $values if 'HASH' eq ref($values);
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" eq ref $values 29 8 37
66 8 ▶ return $self->_value_cache if $self->_has_value_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_value_cache 2 6 8
67  
68 6 $values = $self->$values;
69 6 ▶ $self->_value_cache($values) if $self->cache_values;
Branch: 1 branch
BranchTrueFalseTotal
if $self->cache_values 2 4 6
70 6 return $values;
71 }
72  
73 =begin internals
74  
75 =sub _find_parent
76  
77 $parent = $self->find_parent( $parent, $processed );
78  
79 traverse the argument list, identifying arguments associated with this
80 tree, and use them to descend the tree.
81  
82 Returns one of the following:
83  
84 =over
85  
86 =item *
87  
88 a non-empty hashref, which is a node
89  
90 =item *
91  
92 * an empty hashref or I<undef> in which case we've moved off of the
93 tree
94  
95 =item *
96  
97 anything else is a leaf, hopefully it's a string, but that's up to the caller
98 to have correctly populated the values structure
99  
100 =back
101  
102 =end internals
103  
104 =cut
105  
106 sub _descend_tree {
107 29 ▶ my ( $self, $processed ) = @_;
Subroutine _descend_tree: called CC=4 SCAR=14.1
108  
109 29 my $next = $self->values;
110  
111 # find first processed argument corresponding to $self
112 29 my $addr = refaddr($self);
113 29 my @path;
114 29 for my $arg ( grep { refaddr( $_->{element} ) == $addr } @{$processed} ) {
115 25 my $value = $arg->{value};
116  
117 # this protects against treating $parent as a node when it's actually
118 # a leaf. this happens if the user is passing too many arguments
119 # for the depth of this path through the tree.
120 return ( undef, \@path )
121 25 ▶ unless 'HASH' eq ref $next && exists $next->{$value};
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $next and exists $next->{$value} 0 25 25
Condition33%1/3
"HASH" eq ref $next and exists $next->{$value}
!ll&&!rl&&r
0025
MC/DC0%0/2
"HASH" eq ref $next and exists $next->{$value}
"HASH" eq ref $next exists $next->{$value}
Truth table33%1/3
A and B
A= "HASH" eq ref $nextB= exists $next->{$value}
ABresult
0X0
100
111
122 25 push @path, $value;
123 25 $next = $next->{$value};
124 }
125  
126 29 return $next, \@path;
127 }
128  
129 sub validate {
130 14 ▶ my ( $self, $value, $state ) = @_;
Subroutine validate: called CC=5 SCAR=16.8
Pod documented
131  
132 14 my ( $next, $path ) = $self->_descend_tree( $state->{processed} );
133  
134 # normally this should not happen, as previous calls to validate on early
135 # elements will hae signalled an error, but just in case the calling program
136 # is ignoring errors...
137 14 ▶ return $self->set_error(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $next 0 14 14
138 0 qq/hierarchy validates up to ${ \join( '.', @$path) } /)
139 if !defined $next;
140  
141 14 ▶ return ( ( 'HASH' eq ref $next && exists $next->{$value} )
Branch: 1 branch
BranchTrueFalseTotal
'HASH' eq ref $next && exists $next->{$value} || $next eq $value ? : 11 3 14
Condition66%4/6
"HASH" eq ref $next and exists $next->{$value} or $next eq $value
l!l&&r!l&&!r
1103
"HASH" eq ref $next and exists $next->{$value}
!ll&&!rl&&r
0311
MC/DC33%1/3
"HASH" eq ref $next and exists $next->{$value} or $next eq $value
"HASH" eq ref $next exists $next->{$value} $next eq $value
Truth table40%2/5
A and B or C
A= "HASH" eq ref $nextB= exists $next->{$value}C= $next eq $value
ABCresult
11X1
0X11
1011
0X00
1000
142 || $next eq $value )
143 ? $value
144 : $self->set_error(
145 3 qq/$value is not in hierarchy: ${ \join( '.', @$path) }/);
146 }
147  
148 sub complete {
149 15 ▶ my ( $self, $value, $state ) = @_;
Subroutine complete: called CC=5 SCAR=16.1
Pod documented
150  
151 15 my ( $next, $path ) = $self->_descend_tree( $state->{processed} );
152 15 ▶ return if !defined $next;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $next 1 14 15
153  
154 14 ▶ my @values = ( 'HASH' eq ref $next ? sort( keys %$next ) : $next );
Branch: 1 branch
BranchTrueFalseTotal
'HASH' eq ref $next ? : 13 1 14
155 14 ▶ return if !@values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 1 13 14
156 13 ▶ return @values if !length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless length $value 4 9 13
157 9 return find_text_matches( $value, \@values );
158 }
159  
160 2 1;
161  
162 __END__
163  
164 =pod
165  
166 =head1 NAME
167  
168 Term::CLI::Argument::Tree - class for tree arguments in Term::CLI
169  
170 =head1 VERSION
171  
172 version 0.061000
173  
174 =head1 SYNOPSIS
175  
176 use Term::CLI::Argument::Tree;
177  
178 # static values
179 my $arg = Term::CLI::Argument::Tree->new(
180 name => 'arg1',
181 values => { l1 => { l2 => 't1', l3 => undef } }
182 );
183  
184 my $values = $arg->values; # returns { l1 => { l2 => 't1', l3 => undef } }
185  
186 # dynamic values
187 my $arg = Term::CLI::Argument::Tree->new(
188 name => 'arg1',
189 values => sub { my %values = (...); \%values },
190 );
191  
192 =head1 DESCRIPTION
193  
194 This class provides a multi-valued argument representing levels in a
195 hierarchical data structure. For example, with the structure
196 presented in the L</Synopsis>, the following sequences of command line
197 argument values are accepted:
198  
199 l1
200 l1 l2
201 l1 l2 t1
202 l1 l3
203  
204 The object provides completion at each level.
205  
206 Because this argument hoovers up an indeterminate number of input tokens
207 it should be the last argument object specified.
208  
209 This class inherits from the L<Term::CLI::Argument>(3p) class.
210  
211 =head1 CLASS STRUCTURE
212  
213 =head2 Inherits from:
214  
215 L<Term::CLI::Argument>(3p).
216  
217 =head2 Consumes:
218  
219 None.
220  
221 =head1 CONSTRUCTORS
222  
223 =over
224  
225 =item B<new>
226  
227 OBJ = Term::CLI::Argument::Tree(
228 name => STRING,
229 values => HashRef | CodeRef,
230 cache_values => BOOL,
231 );
232  
233 See also L<Term::CLI::Argument>(3p). The B<values> argument is
234 mandatory and can either be a reference to a hash, or a code
235 refrerence. The latter can be used to implement dynamic values or
236 delayed expansion (where the values have to be fetched from a database
237 or remote system). The code reference will be called with a single
238 argument consisting of the reference to the
239 L</Term::CLI::Argument::Tree> object.
240  
241 The C<cache_values> attribute can be set to a true value to prevent
242 repeated calls to the C<value> code reference. For dynamic values this
243 is not desired, but for values that are generated through expensive
244 queries, this can be useful. The default is 0 (false).
245  
246 =back
247  
248 =head1 ACCESSORS
249  
250 See also L<Term::CLI::Argument>(3p).
251  
252 =over
253  
254 =item B<values>
255 X<values>
256  
257 A reference to a either a hash of valid values for the argument or a
258 subroutine which returns a reference to such a hash.
259  
260 Note that once set, changing the hash pointed to by the reference
261 will result in undefined behaviour.
262  
263 =item B<cache_values>
264 X<cache_values>
265  
266 =item B<cache_values> ( [ I<BOOL> ] )
267  
268 Returns or sets whether the values hash should be cached in case
269 C<values> is a code reference.
270  
271 For dynamic hashes this should be false, but for hashes that are
272 generated through expensive queries, it can be useful to set this to
273 true.
274  
275 If the value is changed from true to false, the cache is
276 immediately cleared.
277  
278 =back
279  
280 =head1 METHODS
281  
282 See also L<Term::CLI::Argument>(3p).
283  
284 The following methods are added or overloaded:
285  
286 =over
287  
288 =item B<validate>
289  
290 =item B<complete>
291  
292 Overloaded from L<Term::CLI::Argument>(3p).
293  
294 =item B<values>
295  
296 Returns an Hashref containing the valid values for this
297 argument object.
298  
299 In case L</values> is a CodeRef, it will call the
300 code to expand the list, sort it, and return the result.
301  
302 =back
303  
304 =head1 SEE ALSO
305  
306 L<Term::CLI::Argument>(3p),
307 L<Term::CLI>(3p).
308  
309 =head1 AUTHOR
310  
311 Diab Jerius E<lt>djeriusr@cpan.orgE<gt>.
312  
313 =head1 COPYRIGHT AND LICENSE
314  
315 Copyright (c) 2022 Diab Jerius, Smithsonian Astrophysical Observatory
316  
317 This module is free software; you can redistribute it and/or modify
318 it under the same terms as Perl itself. See "perldoc perlartistic."
319  
320 This software is distributed in the hope that it will be useful,
321 but WITHOUT ANY WARRANTY; without even the implied warranty of
322 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
323  
324 =cut
« CLI/Role/CommandSet.pm ↑ Summary CLI/Argument/Bool.pm »