Console.pm

filter: Statementstmt 27.5% 24 / 87 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 19.2% 5 / 26 Podpod 100% 11 / 11 totaltotal 20.5% 40 / 195 CC 23 SCAR 34.6
CC
23
Cov
17%
ask
90.0
confirm
56.0
choice
42.0
CRAP 31.7
« Console/Output.pm ↑ Summary Console/Input.pm »
1 package Mojo::Console;
2 2 ▶use Mojo::Base 'Mojolicious::Command';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use List::Util qw(any none);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Mojo::Console::Input;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Mojo::Console::Output;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '0.0.8';
11  
12 0 ▶has 'args' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
13 0 my $self = shift;
14  
15 0 my $args = $self->defaults;
16  
17 0 GetOptions($args, @{ $self->options });
18  
19 0 return $args;
20 2 };
21  
22 0 ▶has 'defaults' => sub { {} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
23 0 ▶has 'input' => sub { Mojo::Console::Input->new };
Subroutine __ANON__: not called CC=1 SCAR=1.2
24 2 has 'max_attempts' => 10;
25 0 ▶has 'options' => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
26 0 ▶has 'output' => sub { Mojo::Console::Output->new };
Subroutine __ANON__: not called CC=1 SCAR=1.2
27 2 has '_required' => 0;
28  
29 0 ▶sub arg {
Subroutine arg: not called CC=1 SCAR=6.9
Pod documented
30 0 my ($self, $name) = @_;
31  
32 0 return $self->args->{ $name };
33 }
34  
35 0 ▶sub ask {
Subroutine ask: not called CC=9 SCAR=45.0
Pod documented
36 0 my ($self, $message, $default) = @_;
37  
38 0 my $attempts = $self->max_attempts;
39 0 my $answer = '';
40  
41 0 ▶ while ((($self->_required || $attempts == 10) && !$answer) && $attempts--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_required || $attempts == 10 and not $answer and $attempts--) 0 0 0
Condition0%0/9
$self->_required || $attempts == 10 and not $answer and $attempts--
!ll&&!rl&&r
000
$self->_required || $attempts == 10 and not $answer
!ll&&!rl&&r
000
$self->_required or $attempts == 10
l!l&&r!l&&!r
000
MC/DC0%0/4
$self->_required || $attempts == 10 and not $answer and $attempts--
$self->_required $attempts == 10 not $answer $attempts--
Truth table0%0/7
A or B and not(C) and D
A= $self->_requiredB= $attempts == 10C= not $answerD= $attempts--
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
42 0 $self->line($message . ' ');
43
44 0 ▶ if ($default) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default) 0 0 0
45 0 $self->warn(sprintf('[default=%s] ', $default));
46 }
47  
48 0 ▶ $answer = $self->input->ask || (!$self->_required && $default);
Condition0%0/6
$self->input->ask || !$self->_required && $default
l!l&&r!l&&!r
000
not $self->_required and $default
!ll&&!rl&&r
000
MC/DC0%0/3
$self->input->ask || !$self->_required && $default
$self->input->ask not $self->_required $default
Truth table0%0/4
A || not(B) and C
A= $self->input->askB= not $self->_requiredC= $default
ABCresult
1XX1
0111
00X0
0100
49 }
50  
51 0 ▶ if ($attempts < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attempts < 0) 0 0 0
52 0 $self->error("Please answer the question.\n");
53 }
54  
55 0 $self->required(0);
56  
57 0 return $answer;
58 }
59  
60 0 ▶sub confirm {
Subroutine confirm: not called CC=7 SCAR=40.3
Pod documented
61 0 my ($self, $message, $default) = @_;
62  
63 0 ▶ my $default_yes = (any { lc($default || '') eq $_ } qw/y yes/);
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$default || ''
l!l
00
MC/DC0%0/1
$default || ''
$default
Truth table0%0/2
A
A= $default
Aresult
11
00
64 0 ▶ my $default_no = (any { lc($default || '') eq $_ } qw/n no/);
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$default || ''
l!l
00
MC/DC0%0/1
$default || ''
$default
Truth table0%0/2
A
A= $default
Aresult
11
00
65  
66 0 my $attempts = $self->max_attempts;
67 0 my $answer = '';
68  
69 0 ▶ while ((none { lc($answer) eq $_ } qw/y yes n no/) && $attempts--) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&none(sub { lc $answer eq $_; } , "y", "yes", "n", "no") and $attempts--) 0 0 0
Condition0%0/3
&none(sub { lc $answer eq $_; } , "y", "yes", "n", "no") and $attempts--
!ll&&!rl&&r
000
MC/DC0%0/2
&none(sub { lc $answer eq $_; } , "y", "yes", "n", "no") and $attempts--
&none(sub { lc $answer eq $_; } , "y", "yes", "n", "no") $attempts--
Truth table0%0/3
A and B
A= &none(sub { lc $answer eq $_; } , "y", "yes", "n", "no")B= $attempts--
ABresult
0X0
100
111
70 0 $self->line($message);
71  
72 0 $self->success(' [yes/no] ');
73  
74 0 ▶ if ($default) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default) 0 0 0
75 0 $self->warn(sprintf('[default=%s] ', $default));
76 }
77  
78 0 ▶ $answer = $self->input->ask || $default;
Condition0%0/3
$self->input->ask || $default
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->input->ask || $default
$self->input->ask $default
Truth table0%0/3
A || B
A= $self->input->askB= $default
ABresult
1X1
011
000
79 }
80  
81 0 ▶ if ($attempts < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attempts < 0) 0 0 0
82 0 $self->error("Please answer with [yes/no]\n");
83 }
84  
85 0 ▶ return (any { lc($answer) eq $_ } qw/y yes/) ? 1 : 0;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
&any(sub { lc $answer eq $_; } , 'y', 'yes') ? : 0 0 0
86 }
87  
88 0 ▶sub choice {
Subroutine choice: not called CC=6 SCAR=37.4
Pod documented
89 0 my ($self, $message, $choices, $default) = @_;
90  
91 0 my $attempts = $self->max_attempts;
92 0 my $answer = '';
93  
94 0 ▶ while ((none { $answer eq $_ } @$choices) && $attempts--) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&none(sub { $answer eq $_; } , @$choices) and $attempts--) 0 0 0
Condition0%0/3
&none(sub { $answer eq $_; } , @$choices) and $attempts--
!ll&&!rl&&r
000
MC/DC0%0/2
&none(sub { $answer eq $_; } , @$choices) and $attempts--
&none(sub { $answer eq $_; } , @$choices) $attempts--
Truth table0%0/3
A and B
A= &none(sub { $answer eq $_; } , @$choices)B= $attempts--
ABresult
0X0
100
111
95 0 $self->line($message);
96 0 $self->success(sprintf(' [%s] ', join(', ', @$choices)));
97  
98 0 ▶ if ($default) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default) 0 0 0
99 0 $self->warn(sprintf('[default=%s] ', $default));
100 }
101  
102 0 ▶ $answer = $self->input->ask || $default;
Condition0%0/3
$self->input->ask || $default
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->input->ask || $default
$self->input->ask $default
Truth table0%0/3
A || B
A= $self->input->askB= $default
ABresult
1X1
011
000
103 }
104  
105 0 ▶ if ($attempts < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attempts < 0) 0 0 0
106 0 $self->error(sprintf("Please chose one of the following options: [%s] \n", join(', ', @$choices)));
107 }
108  
109 0 return $answer;
110 }
111  
112 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
113 0 return shift->output->error(@_);
114 }
115  
116 0 ▶sub info {
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
117 0 return shift->output->info(@_);
118 }
119  
120 0 ▶sub line {
Subroutine line: not called CC=1 SCAR=6.9
Pod documented
121 0 return shift->output->line(@_);
122 }
123  
124 0 ▶sub newline {
Subroutine newline: not called CC=1 SCAR=6.9
Pod documented
125 0 return shift->output->newline(@_);
126 }
127  
128 0 ▶sub required {
Subroutine required: not called CC=2 SCAR=17.9
Pod documented
129 0 my $self = shift;
130  
131 0 ▶ $self->_required(shift // 1);
Condition0%0/2
shift() // 1
l!l
00
MC/DC0%0/1
shift() // 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
132  
133 0 return $self;
134 }
135  
136 0 ▶sub success {
Subroutine success: not called CC=1 SCAR=6.9
Pod documented
137 0 return shift->output->success(@_);
138 }
139  
140 0 ▶sub warn {
Subroutine warn: not called CC=1 SCAR=6.9
Pod documented
141 0 return shift->output->warn(@_);
142 }
143  
144 2 1;
145  
146 =encoding utf8
147  
148 =head1 NAME
149  
150 Mojo::Console - Extend Mojolicious::Command to be able to ask for things from command line
151  
152 =head1 SYNOPSIS
153  
154 package MyApp::Command::helloworld;
155 use Mojo::Base 'Mojo::Console';
156  
157 sub run {
158 my $self = shift;
159  
160 my $name = $self->ask('What is your name?');
161 my $gender = $self->choice('Are you a male or a female?', ['male', 'female']);
162 my $bool = $self->confirm("Do you have a cat?");
163  
164 $self->line("Hi $name\n");
165 $self->line("We found out that you are a $gender ");
166  
167 if ($bool) {
168 $self->line("and you have a cat");
169 } else {
170 $self->line("and you don't have a cat");
171 }
172  
173 if ($self->confirm("Would you like an icecream?")) {
174 $self->success("Thanks");
175 } else {
176 $self->error("Oh no!");
177 }
178  
179 $self->info("You got here because you took an icecream");
180 }
181  
182 1;
183  
184 =head1 DESCRIPTION
185  
186 L<Mojo::Console> is an extension of L<Mojolicious::Command>
187  
188 =head1 ATTRIBUTES
189  
190 L<Mojo::Console> inherits all attributes from L<Mojolicious::Command>and implements the
191 following new ones.
192  
193 =head2 args
194  
195 my $args = $console->args;
196  
197 Uses Getopt::Long::GetOptions to parse passed args.
198  
199 =head2 defaults
200  
201 my $defaults = $console->defaults;
202 $console = $console->defaults({ arg1 => 'value', verbose => 1 });
203  
204 Default values for args.
205  
206 =head2 input
207  
208 my $input = $console->input;
209 $console = $console->input(Mojo::Console::Input->new);
210  
211 Input collector interface.
212  
213 =head2 max_attempts
214  
215 my $max_attempts = $console->max_attempts;
216 $console = $console->max_attempts(5);
217  
218 How many times to allow a user to enter wrong value.
219  
220 =head2 options
221  
222 my $options = $console->options;
223 $console = $console->options(['arg1=s', 'confirm']);
224  
225 Args descriptor, see L<GetOpt::Long>, method GetOptions.
226  
227 =head2 output
228  
229 my $output = $console->output;
230 $console = $console->output(Mojo::Console::Output->new);
231  
232 Output interface.
233  
234 =head1 METHODS
235  
236 L<Mojo::Console> inherits all methods from L<Mojolicious::Command> and implements
237 the following new ones.
238  
239 =head2 arg
240  
241 my $value = $self->arg('verbose');
242  
243 Get the value of an argument.
244  
245 =head2 ask
246  
247 my $answer = $self->ask('What is your name?');
248 my $required_answer = $self->required->ask('What is your name?'); # this will ask for an answer maximum 10 times and will exit in case the answer is empty
249  
250 Ask a question.
251  
252 =head2 confirm
253  
254 my $bool = $self->confirm("Are you sure?");
255 my $bool_with_default_answer = $self->confirm("Are you sure?", 'yes');
256  
257 Ask the user to confirm something.
258  
259 =head2 choice
260  
261 my $choice = $self->choice('Are you a male or a female?', ['male', 'female']);
262 my $choice_with_default_answer = $self->choice('Are you a male or a female?', ['male', 'female'], 'male');
263  
264 Ask the user to pick a value.
265  
266 =head2 error
267  
268 $self->error("The program will stop here");
269  
270 Write a message on the output and exit.
271  
272 =head2 info
273  
274 $self->info("This is just an info message");
275  
276 Write an info message to the output.
277  
278 =head2 line
279  
280 $self->line("This message will not have a new line at the end");
281  
282 Write a line to the output.
283  
284 =head2 newline
285  
286 $self->newline("This message will have a new line at the end");
287  
288 Write a line to the output, followed by a newline.
289  
290 =head2 required
291  
292 $self->required->ask('What is your name?');
293  
294 Mark the question as being required.
295  
296 =head2 success
297  
298 $self->success("This is just a success message");
299  
300 Write a success message to the output.
301  
302 =head2 warn
303  
304 $self->success("This is just a warning message");
305  
306 Write a warn message to the output.
307  
308 =head1 SEE ALSO
309  
310 L<Mojolicious::Command>, L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
311  
312 =cut
« Console/Output.pm ↑ Summary Console/Input.pm »