CLI/Argument/Bool.pm

filter: Statementstmt 31.4% 17 / 54 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 6 / 8 Podpod n/a totaltotal 30.2% 23 / 76 CC 8 SCAR 29.8
CC
8
Cov
25%
validate
42.0
complete
12.0
BEGIN
1.0
CRAP 19.6
« CLI/Argument/String.pm ↑ Summary CLI/Argument/Enum.pm »
1 #=============================================================================
2 #
3 # Module: Term::CLI::Argument::Bool
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::Bool 0.051003 {
24  
25 1 ▶use Modern::Perl 1.20140107;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 use Types::Standard 1.000005 qw(
28 ArrayRef
29 Str
30 Bool
31 1 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 ▶use Term::CLI::L10N;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 1 ▶use Moo 1.000001;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use namespace::clean 0.25;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 extends 'Term::CLI::Argument';
39  
40 has 'true_values' => (
41 is => 'rw',
42 isa => ArrayRef[Str],
43 default => sub { [qw( true 1 )] },
44 );
45  
46 has 'false_values' => (
47 is => 'rw',
48 isa => ArrayRef[Str],
49 default => sub { [qw( false 0 )] },
50 );
51  
52 has 'ignore_case' => (
53 is => 'rw',
54 isa => Bool,
55 default => sub { 1 },
56 );
57  
58  
59 sub validate {
60 0 ▶ my $self = shift;
Subroutine validate: not called CC=6 SCAR=37.4
61 0 my $value = shift;
62  
63 0 ▶ defined $self->SUPER::validate($value) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->SUPER::validate($value) or return 0 0 0
64  
65 0 my ($true, $false);
66  
67 0 ▶ if ($self->ignore_case) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ignore_case) { } 0 0 0
68 0 $value = lc $value;
69  
70 0 $true = [ map { lc $_ } @{$self->true_values} ];
71 0 $false = [ map { lc $_ } @{$self->false_values} ];
72 }
73 else {
74 0 $true = $self->true_values;
75 0 $false = $self->false_values;
76 }
77  
78 0 my @true_match = grep { rindex($_, $value, 0) == 0 } @$true;
79 0 my @false_match = grep { rindex($_, $value, 0) == 0 } @$false;
80  
81 0 ▶ if (@true_match) {
Branch: 2 branches
BranchTrueFalseTotal
if (@true_match) { } 0 0 0
elsif (@false_match) { } 0 0 0
82 0 ▶ if (@false_match) {
Branch: 1 branch
BranchTrueFalseTotal
if (@false_match) { } 0 0 0
83 0 return $self->set_error(
84 loc("ambiguous boolean value"
85 ." (matches ~[[_1]~] and ~[[_2]~])",
86 join(", ", @true_match),
87 join(", ", @false_match),
88 )
89 );
90 }
91 else {
92 0 return 1;
93 }
94 }
95 elsif (@false_match) {
96 0 return 0;
97 }
98  
99 0 return $self->set_error(loc("invalid boolean value"));
100 }
101  
102  
103 sub complete {
104 0 ▶ my ($self, $value) = @_;
Subroutine complete: not called CC=3 SCAR=24.8
105  
106 0 my @values = ( @{$self->true_values}, @{$self->false_values} );
107  
108 0 ▶ if (!length $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $value) { } 0 0 0
109 0 return sort @values;
110 }
111 else {
112 0 ▶ if ($self->ignore_case) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ignore_case) { } 0 0 0
113 my @matches
114 = sort
115 grep
116 0 { substr(lc $_, 0, length($value)) eq lc $value }
117 @values;
118  
119 0 return map { $value.substr($_, length($value)) } @matches;
120 }
121 else {
122 return sort
123 0 grep { substr($_, 0, length($value)) eq $value } @values;
124 }
125 }
126 }
127  
128  
129 }
130  
131 1;
132  
133 __END__
« CLI/Argument/String.pm ↑ Summary CLI/Argument/Enum.pm »