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), <cpanman@monkey-mind.net>
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.061001;
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(
Subroutine BEGIN: called CC=1 SCAR=0.0
30 ArrayRef
31 CodeRef
32 10 );
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 15 ▶ coerce => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
47 15 my ($arg) = @_;
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 2 ▶ trigger => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
59 2 my ($self, $new) = @_;
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 29 ▶sub values {
Subroutine values: called CC=4 SCAR=13.9
Pod documented
72 29 my ($self) = @_;
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 14 ▶sub validate {
Subroutine validate: called CC=4 SCAR=13.9
Pod documented
95 14 my ( $self, $value ) = @_;
96  
97 14 ▶ defined $self->SUPER::validate($value) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->SUPER::validate($value) or return 10 4 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 15 ▶sub complete {
Subroutine complete: called CC=2 SCAR=6.9
Pod documented
114 15 my ( $self, $text ) = @_;
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__
« CLI/Role/ArgumentSet.pm ↑ Summary CLI/Argument/TypeTiny.pm »