| 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.061001; | ||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 4 | ▶ | trigger => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 4 | my ( $self, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | 4 | ▶ | $self->_clear_value_cache if !$value; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 2 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | has _value_cache => ( | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | isa => HashRef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | predicate => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | clearer => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 37 | ▶ | sub values { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
values: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 37 | my ($self) = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | 37 | my $values = $self->_values; | |||||||||||||||||||||||||||||||||||||||||||||
| 65 | 37 | ▶ | return $values if 'HASH' eq ref($values); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 8 | ▶ | return $self->_value_cache if $self->_has_value_cache; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 6 | $values = $self->$values; | |||||||||||||||||||||||||||||||||||||||||||||
| 69 | 6 | ▶ | $self->_value_cache($values) if $self->cache_values; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 29 | ▶ | sub _descend_tree { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_descend_tree: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 29 | my ( $self, $processed ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
"HASH" eq ref $next and exists $next->{$value}
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}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 25 | push @path, $value; | |||||||||||||||||||||||||||||||||||||||||||||
| 123 | 25 | $next = $next->{$value}; | |||||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 29 | return $next, \@path; | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 14 | ▶ | sub validate { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate: called CC=5 SCAR=16.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 14 | my ( $self, $value, $state ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
"HASH" eq ref $next and exists $next->{$value} or $next eq $value
"HASH" eq ref $next and exists $next->{$value}
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | || $next eq $value ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | ? $value | ||||||||||||||||||||||||||||||||||||||||||||||
| 144 | : $self->set_error( | ||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 3 | qq/$value is not in hierarchy: ${ \join( '.', @$path) }/); | |||||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 15 | ▶ | sub complete { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
complete: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 15 | my ( $self, $value, $state ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 15 | my ( $next, $path ) = $self->_descend_tree( $state->{processed} ); | |||||||||||||||||||||||||||||||||||||||||||||
| 152 | 15 | ▶ | return if !defined $next; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 14 | ▶ | my @values = ( 'HASH' eq ref $next ? sort( keys %$next ) : $next ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 14 | ▶ | return if !@values; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 13 | ▶ | return @values if !length $value; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 9 | return find_text_matches( $value, \@values ); | |||||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||