CLI/Argument/Bool.pm

filter: Statementstmt 90.1% 64 / 71 Branchbran 83.3% 15 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 10 / 12 Podpod 100% 2 / 2 totaltotal 88.3% 91 / 103 CC 10 SCAR 12.9
CC
10
Cov
89%
validate
6.0
complete
5.7
__ANON__
2.0
CRAP 3.6
« CLI/Argument/Tree.pm ↑ Summary CLI/Argument/String.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), <cpanman@monkey-mind.net>
8 # Created: 22/01/18
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::Bool 0.061001;
22  
23 7 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 7 ▶use Types::Standard 1.000005 qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
27 ArrayRef
28 Str
29 Bool
30 7 );
31  
32 7 ▶use Term::CLI::L10N qw( loc );
Subroutine BEGIN: called CC=1 SCAR=0.0
33 7 ▶use Term::CLI::Util qw( is_prefix_str );
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 7 ▶use Moo 1.000001;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 7 ▶use namespace::clean 0.25;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 7 extends 'Term::CLI::Argument';
39  
40 has 'true_values' => (
41 is => 'rw',
42 isa => ArrayRef [Str],
43 0 ▶ default => sub { [qw( true 1 )] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 7 );
45  
46 has 'false_values' => (
47 is => 'rw',
48 isa => ArrayRef [Str],
49 0 ▶ default => sub { [qw( false 0 )] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 7 );
51  
52 has 'ignore_case' => (
53 is => 'rw',
54 isa => Bool,
55 1 ▶ default => sub {1},
Subroutine __ANON__: called CC=1 SCAR=0.0
56 7 );
57  
58 8 ▶sub validate {
Subroutine validate: called CC=6 SCAR=17.9
Pod documented
59 8 my ($self, $value) = @_;
60  
61 8 ▶ defined $self->SUPER::validate($value) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->SUPER::validate($value) or return 6 2 8
62  
63 6 my ( $true, $false );
64  
65 6 ▶ if ( $self->ignore_case ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ignore_case) { } 4 2 6
66 4 $value = lc $value;
67  
68 4 $true = [ map {lc} @{ $self->true_values } ];
69 4 $false = [ map {lc} @{ $self->false_values } ];
70 }
71 else {
72 2 $true = $self->true_values;
73 2 $false = $self->false_values;
74 }
75  
76 6 my @true_match = grep { is_prefix_str( $value, $_ ) } @{$true};
77 6 my @false_match = grep { is_prefix_str( $value, $_ ) } @{$false};
78  
79 6 ▶ if (@true_match) {
Branch: 1 branch
BranchTrueFalseTotal
if (@true_match) 3 3 6
80 3 ▶ if (@false_match) {
Branch: 1 branch
BranchTrueFalseTotal
if (@false_match) 2 1 3
81 2 return $self->set_error(
82 loc('ambiguous boolean value'
83 . ' (matches ~[[_1]~] and ~[[_2]~])',
84 join( ', ', @true_match ),
85 join( ', ', @false_match ),
86 )
87 );
88 }
89 1 return 1;
90 }
91  
92 3 ▶ return 0 if @false_match;
Branch: 1 branch
BranchTrueFalseTotal
if @false_match 1 2 3
93  
94 2 return $self->set_error( loc('invalid boolean value') );
95 }
96  
97 6 ▶sub complete {
Subroutine complete: called CC=5 SCAR=17.5
Pod documented
98 6 my ( $self, $value ) = @_;
99  
100 6 my @values = ( @{ $self->true_values }, @{ $self->false_values } );
101  
102 6 ▶ return ( sort @values ) if $value eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "" 1 5 6
103  
104 5 ▶ if ( $self->ignore_case ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ignore_case) 3 2 5
105 3 my @matches = grep { is_prefix_str( lc $value, lc $_ ) } @values;
106  
107 # Cannot just return @matches because of potential case differences.
108 3 ▶ if ( $value =~ m{ \p{Lu}\z }xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ / \p{Lu}\z /msux) 3 0 3
109 3 return ( sort map { $value . uc substr $_, length $value } @matches );
110 }
111 0 ▶ if ( $value =~ m{ \p{Ll}\z }xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ / \p{Ll}\z /msux) 0 0 0
112 0 return ( sort map { $value . lc substr $_, length $value } @matches );
113 }
114 0 return ( sort map { $value . substr $_, length $value } @matches );
115 }
116 2 return ( sort grep { is_prefix_str( $value, $_ ) } @values );
117 }
118  
119 7 1;
120  
121 __END__
« CLI/Argument/Tree.pm ↑ Summary CLI/Argument/String.pm »