lib/CLI/Driver/Option.pm

filter: Statementstmt 87.7% 107 / 122 Branchbran 56.2% 27 / 48 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 95.4% 21 / 22 Podpod n/a totaltotal 80.7% 155 / 192 CC 25 SCAR 18.4
CC
25
Cov
79%
get_val
18.5
is_optional
6.0
is_flag
3.1
CRAP 6.3
« lib/CLI/Driver/Help.pm ↑ Summary lib/CLI/Driver/Exec.pm »
1 package CLI::Driver::Option;
2  
3 15 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Kavorka '-all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Data::Printer alias => 'pdump';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 15 ▶use Getopt::Long 'GetOptionsFromArray';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 Getopt::Long::Configure('no_ignore_case');
11 15 Getopt::Long::Configure('pass_through');
12  
13 15 with 'CLI::Driver::CommonRole';
14  
15 ###############################
16 ###### PUBLIC ATTRIBUTES ######
17 ###############################
18  
19 15 has class => (
20 is => 'rw',
21 isa => 'Str',
22 );
23  
24 15 has cli_arg => (
25 is => 'rw',
26 isa => 'Str'
27 );
28  
29 15 has method_arg => (
30 is => 'rw',
31 isa => 'Str|Undef'
32 );
33  
34 15 has required => (
35 is => 'rw',
36 isa => 'Bool'
37 );
38  
39 15 has hard => (
40 is => 'rw',
41 isa => 'Bool',
42 );
43  
44 15 has flag => (
45 is => 'rw',
46 isa => 'Bool',
47 );
48  
49 15 has is_array => (
50 is => 'rw',
51 isa => 'Bool',
52 );
53  
54 ###############################################
55  
56 15 ▶method get_signature {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine get_signature: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 63 0 63
57  
58 63 my %sig;
59 63 my $val = $self->get_val;
60  
61 63 ▶ if ( defined $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 43 20 63
62 43 $sig{ $self->method_arg } = $val;
63 }
64  
65 63 return %sig;
66 }
67  
68 15 ▶method is_required {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine is_required: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 34 0 34
69
70 34 ▶ if ($self->required) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->required) 19 15 34
71 19 return 1;
72 }
73
74 15 return 0;
75 }
76  
77 15 ▶method is_flag {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine is_flag: called CC=3 SCAR=11.3
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 59 0 59
78
79 59 ▶ if ($self->flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->flag) 0 59 59
80 0 return 1;
81 }
82
83 59 return 0;
84 }
85  
86 15 ▶method is_optional {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine is_optional: not called CC=3 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
87
88 0 ▶ if (!$self->required) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->required) 0 0 0
89 0 return 1;
90 }
91
92 0 return 0;
93 }
94  
95 15 ▶method is_hard {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine is_hard: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 9 0 9
96
97 9 ▶ if ($self->hard) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hard) 5 4 9
98 5 return 1;
99 }
100
101 4 return 0;
102 }
103  
104 15 ▶method is_soft {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine is_soft: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 15 0 15
105
106 15 ▶ if (!$self->hard) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->hard) 2 13 15
107 2 return 1;
108 }
109
110 13 return 0;
111 }
112  
113 15 ▶method get_val {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine get_val: called CC=11 SCAR=29.2
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 63 0 63
114  
115 63 my $arg = $self->cli_arg;
116 63 my $val;
117  
118 63 ▶ if ( $self->is_boolean ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_boolean) { } 4 59 63
elsif ($self->is_flag) { } 0 59 59
119 #
120 # deprecated in favor of self->is_flag
121 #
122 4 my $success = GetOptionsFromArray( \@ARGV, "$arg" => \$val, );
123  
124 4 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) 4 0 4
125 4 ▶ my $val = $val ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 2 2 4
126 4 return $val;
127 }
128  
129 0 confess "something went sideways?";
130 }
131 elsif ($self->is_flag) {
132 # - just a cli switch
133 # - never required from cmdline
134
135 0 my $success = GetOptionsFromArray( \@ARGV, "$arg" => \$val, );
136  
137 0 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) 0 0 0
138 0 ▶ my $val = $val ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 0 0 0
139 0 return $val;
140 }
141  
142 0 confess "something went sideways?";
143 }
144 else {
145  
146 # get "-arg <val>" from cmdline if exists
147 59 my $arg_type = "s";
148
149 59 ▶ if( $self->is_array ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_array) 18 41 59
150 18 $arg_type='s@';
151 }
152  
153 59 my $success = GetOptionsFromArray( \@ARGV, "$arg=$arg_type" => \$val, );
154 59 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) 59 0 59
155 59 return $val;
156 }
157  
158 # we didn't find it in @ARGV
159 0 ▶ if ( $self->required ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->required) 0 0 0
160 0 $self->fatal("failed to get arg from argv: $arg");
161 }
162 }
163  
164 0 return;
165 }
166  
167 15 ▶method is_boolean {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine is_boolean: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 63 0 63
168  
169 63 ▶ if (length $self->cli_arg > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->cli_arg > 1) 4 59 63
170 4 return 1;
171 }
172
173 59 return 0;
174 }
175  
176 15 1;
« lib/CLI/Driver/Help.pm ↑ Summary lib/CLI/Driver/Exec.pm »