CLI/Argument/Enum.pm

filter: Statementstmt 50.0% 17 / 34 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 6 / 8 Podpod n/a totaltotal 46.0% 23 / 50 CC 5 SCAR 21.0
CC
5
Cov
40%
validate
20.0
complete
6.0
BEGIN
1.0
CRAP 8.2
« CLI/Argument/Bool.pm ↑ Summary CLI/Argument.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/01/18
9 #
10 # Copyright (c) 2018 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 1 ▶use 5.014_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 package Term::CLI::Argument::Enum 0.051003 {
24  
25 1 ▶use Modern::Perl 1.20140107;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Term::CLI::L10N;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 use Types::Standard 1.000005 qw(
29 ArrayRef
30 1 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 1 ▶use Moo 1.000001;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use namespace::clean 0.25;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 extends 'Term::CLI::Argument';
36  
37 has value_list => (
38 is => 'ro',
39 isa => ArrayRef,
40 required => 1,
41 );
42  
43  
44 sub validate {
45 0 ▶ my ($self, $value) = @_;
Subroutine validate: not called CC=4 SCAR=30.0
46  
47 0 ▶ defined $self->SUPER::validate($value) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->SUPER::validate($value) or return 0 0 0
48  
49 0 my @found = grep { rindex($_, $value, 0) == 0 } @{$self->value_list};
50 0 ▶ if (@found == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@found == 0) { } 0 0 0
elsif (@found == 1) { } 0 0 0
51 0 return $self->set_error(loc("not a valid value"));
52 }
53 elsif (@found == 1) {
54 0 return $found[0];
55 }
56 else {
57 0 @found = sort @found;
58 0 return $self->set_error(
59 loc("ambiguous value (matches: [_1])", join(", ", @found))
60 );
61 }
62 }
63  
64  
65 sub complete {
66 0 ▶ my ($self, $value) = @_;
Subroutine complete: not called CC=2 SCAR=17.9
67  
68 0 ▶ if (!length $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $value) { } 0 0 0
69 0 return sort @{$self->value_list};
70 }
71 else {
72 return sort grep
73 0 { substr($_, 0, length($value)) eq $value }
74 0 @{$self->value_list};
75 }
76 }
77  
78 }
79  
80 1;
81  
82 __END__
« CLI/Argument/Bool.pm ↑ Summary CLI/Argument.pm »