Factory/Interface/CLI.pm

filter: Statementstmt 77.4% 48 / 62 Branchbran 78.5% 22 / 28 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 45.4% 5 / 11 Podpod 100% 2 / 2 totaltotal 73.5% 78 / 106 CC 18 SCAR 18.7
CC
18
Cov
77%
__ANON__
12.0
consume_control
9.0
render_control
7.1
CRAP 6.5
« Factory/Interface/HTML/Widget/Textarea.pm ↑ Summary Factory/Feature/RequireNoneOrAll.pm »
1 package Form::Factory::Interface::CLI;
2 1 $Form::Factory::Interface::CLI::VERSION = '0.022';
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 with qw( Form::Factory::Interface );
6  
7 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ABSTRACT: Command-line interface builder for form factory
10  
11  
12 has renderer => (
13 is => 'ro',
14 isa => 'CodeRef',
15 required => 1,
16 0 ▶ default => sub { sub { print @_ } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
17 1 );
18  
19  
20 has get_args => (
21 is => 'ro',
22 isa => 'CodeRef',
23 required => 1,
24 0 ▶ default => sub { sub { \@ARGV } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 1 );
26  
27  
28 has get_file => (
29 is => 'ro',
30 isa => 'CodeRef',
31 required => 1,
32 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
34 0 my ($interface, $name) = @_;
35 0 my $fh;
36  
37 0 ▶ if ($name eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq '-') { } 0 0 0
38 0 $fh = \*STDIN;
39 }
40 else {
41 0 ▶ open $fh, '<', $name or Carp::croak("cannot read $name: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $name or Carp::croak("cannot read ${name}: $!\n") 0 0 0
42 }
43  
44 0 do { local $/; <$fh> };
45 }
46 0 },
47 1 );
48  
49  
50 8 ▶sub render_control {
Subroutine render_control: called CC=7 SCAR=19.6
Pod documented
51 8 my ($self, $control, %options) = @_;
52  
53 8 ▶ return if $control->does('Form::Factory::Control::Role::HiddenValue');
Branch: 1 branch
BranchTrueFalseTotal
if $control->does("Form::Factory::Control::Role::HiddenValue") 1 7 8
54  
55 7 my $arg;
56 7 ▶ if ($control->does('Form::Factory::Control::Role::AvailableChoices')) {
Branch: 4 branches
BranchTrueFalseTotal
if ($control->does('Form::Factory::Control::Role::AvailableChoices')) { } 2 5 7
elsif ($control->does('Form::Factory::Control::Role::BooleanValue')) { } 2 3 5
elsif ($control->does('Form::Factory::Control::Role::PresetValue')) { } 0 3 3
elsif ($control->does('Form::Factory::Control::Role::MultiLine')) { } 1 2 3
57 2 my @values = map { $_->value } @{ $control->available_choices };
58 2 $arg = '[ ' . join(' | ', @values) . ' ]';
59 }
60 elsif ($control->does('Form::Factory::Control::Role::BooleanValue')) {
61 2 $arg = ''
62 }
63 elsif ($control->does('Form::Factory::Control::Role::PresetValue')) {
64 0 $arg = '';
65 }
66 elsif ($control->does('Form::Factory::Control::Role::MultiLine')) {
67 1 $arg = 'FILE';
68 }
69 else {
70 2 $arg = 'TEXT';
71 }
72  
73 7 ▶ my $description = $control->documentation || '';
Condition50%1/2
$control->documentation || ''
l!l
70
MC/DC0%0/1
$control->documentation || ''
$control->documentation
Truth table50%1/2
A
A= $control->documentation
Aresult
11
00
74  
75 7 $self->renderer->($self,
76 sprintf(" --%-20s %s\n", $control->name . ' '. $arg, $description)
77 );
78 }
79  
80  
81 8 ▶sub consume_control {
Subroutine consume_control: called CC=9 SCAR=22.0
Pod documented
82 8 my ($self, $control, %options) = @_;
83  
84 8 my @argv = @{ $self->get_args->($self) };
85 8 my ($fetch, @values);
86 8 for my $argv (@argv) {
87 128 ▶ if ($fetch) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fetch) { } 7 121 128
elsif ($argv eq '--' . $control->name) { } 9 112 121
88 7 push @values, $argv;
89 7 undef $fetch;
90 }
91  
92 elsif ($argv eq '--' . $control->name) {
93 9 ▶ if ($control->does('Form::Factory::Control::Role::BooleanValue')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($control->does('Form::Factory::Control::Role::BooleanValue')) { } 2 7 9
94 2 push @values, $control->true_value;
95 }
96 else {
97 7 $fetch++;
98 }
99 }
100 }
101  
102 8 ▶ return {} unless @values > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @values > 0 1 7 8
103  
104 9 ▶ my $get_value = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
105 9 my $value = shift;
106 9 ▶ if ($control->does('Form::Factory::Control::Role::MultiLine')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($control->does('Form::Factory::Control::Role::MultiLine')) { } 1 8 9
107 1 return $self->get_file->($self, $value);
108 }
109 else {
110 8 return $value;
111 }
112 7 };
113  
114 7 ▶ if ($control->does('Form::Factory::Control::Role::ListValue')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($control->does('Form::Factory::Control::Role::ListValue')) { } 1 6 7
115 1 my @result;
116 1 push @result, $get_value->($_) for @values;
117 1 $control->current_values(\@result);
118 }
119  
120 else {
121 6 ▶ Carp::croak(sprintf("the --%s option should be used only once\n", $control->name))
Branch: 1 branch
BranchTrueFalseTotal
if @values > 1 0 6 6
122 if @values > 1;
123
124 6 $control->current_value($get_value->($values[0]));
125 }
126 }
127  
128 1 __PACKAGE__->meta->make_immutable;
129  
130 __END__
« Factory/Interface/HTML/Widget/Textarea.pm ↑ Summary Factory/Feature/RequireNoneOrAll.pm »