blib/lib/Config/Cmd.pm

filter: Statementstmt 90.3% 75 / 83 Branchbran 87.5% 28 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 91.6% 11 / 12 Podpod 100% 3 / 3 totaltotal 90.0% 117 / 130 CC 18 SCAR 16.8
CC
18
Cov
90%
set_silent
10.0
_get_file
5.2
get
4.0
CRAP 5.3
↑ Summary
1 package Config::Cmd;
2 3 ▶use Mo qw(default);
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use YAML qw'DumpFile LoadFile';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # ABSTRACT: Command line to config file two way interface
8 3 our $VERSION = '0.002'; # VERSION
9  
10 3 ▶use constant EXT => '_conf.yaml';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 has section => ();
13 3 has filename => ();
14 1 ▶has quote => ( default => sub { q(') } );
Subroutine __ANON__: called CC=1 SCAR=0.0
15  
16 0 ▶sub set {
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
17 0 my $self = shift;
18 0 my $args = shift;
19  
20 0 say STDERR "# writing into ". $self->_set_file;
21 0 say STDERR "# options: ". "@$args";
22  
23 0 $self->set_silent($args);
24 }
25  
26 10 ▶sub set_silent {
Subroutine set_silent: called CC=10 SCAR=23.0
Pod documented
27 10 my $self = shift;
28 10 my $args = shift;
29  
30 # process arguments
31 10 my $section = $self->section;
32 10 ▶ croak "Set section before writing into a file" unless $section;
Branch: 1 branch
BranchTrueFalseTotal
unless $section 1 9 10
33 9 my $config->{$section} = [];
34  
35 9 my $key = '';
36 9 ▶ while (my $e = shift @$args) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = shift @$args) 37 9 46
37 37 ▶ if ($e eq '=') {
Branch: 3 branches
BranchTrueFalseTotal
if ($e eq '=') { } 2 35 37
elsif ($e =~ /(-.+)=(.+)/u) { } 2 33 35
elsif ($e =~ /^-/u) { } 20 13 33
38 2 next;
39 }
40 elsif ($e =~ /(-.+)=(.+)/) {
41 2 ▶ if ($key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) 1 1 2
42 1 my $tuple->{key} = $key ;
43 1 push @{$config->{$section}}, $tuple;
44 1 $key = '';
45 }
46 2 my $tuple->{key} = $1 ;
47 2 $tuple->{value} = $2 ;
48 2 push @{$config->{$section}}, $tuple;
49 }
50 elsif ($e =~ /^-/) {
51 20 ▶ if ($key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) 5 15 20
52 5 my $tuple->{key} = $key ;
53 5 push @{$config->{$section}}, $tuple;
54 }
55 20 $key = $e;
56 } else {
57 13 ▶ if ($key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) 12 1 13
58 12 my $tuple->{key} = $key ;
59 12 $tuple->{value} = $e ;
60 12 push @{$config->{$section}}, $tuple;
61 }
62 13 $key = '';
63 }
64  
65 }
66 9 ▶ if ($key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) 2 7 9
67 2 my $tuple->{key} = $key;
68 2 push @{$config->{$section}}, $tuple;
69 }
70  
71 9 DumpFile $self->_set_file, $config;
72 }
73  
74 9 ▶sub get {
Subroutine get: called CC=4 SCAR=13.9
Pod documented
75 9 my $self = shift;
76 9 my $section = $self->section;
77  
78 9 my $config = LoadFile($self->_get_file);
79  
80 9 my @list;
81 9 for my $tuple (@{$config->{$section}}) {
82 22 ▶ if (defined $tuple->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tuple->{'value'}) { } 14 8 22
83 14 my $value = $tuple->{value};
84 14 ▶ $value = $self->quote. $value. $self->quote if $value =~ /\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\s/u 1 13 14
85 14 push @list, $tuple->{key}, $value;
86 } else {
87 8 push @list, $tuple->{key};
88 }
89 }
90 9 return join ' ', @list;
91 }
92  
93 # internal methods
94  
95 24 ▶sub _default_filename {
Subroutine _default_filename: called CC=1 SCAR=0.0
96 24 return shift->section. EXT;
97 }
98  
99 9 ▶sub _set_file {
Subroutine _set_file: called CC=3 SCAR=11.4
100 9 my $self = shift;
101  
102 9 ▶ return $self->filename if defined $self->filename;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->filename 1 8 9
103 8 ▶ return $self->_default_filename if $self->section;
Branch: 1 branch
BranchTrueFalseTotal
if $self->section 8 0 8
104 0 return; # if no matching files were found
105 }
106  
107  
108 9 ▶sub _get_file {
Subroutine _get_file: called CC=4 SCAR=16.4
109 9 my $self = shift;
110  
111 9 ▶ return $self->filename if defined $self->filename;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->filename 1 8 9
112 8 ▶ if (-e $self->_default_filename) {
Branch: 2 branches
BranchTrueFalseTotal
if (-e $self->_default_filename) { } 8 0 8
elsif (-e $ENV{'HOME'} . '.' . $self->_default_filename) { } 0 0 0
113 8 return $self->_default_filename;
114 } elsif (-e $ENV{HOME}. '.'. $self->_default_filename) { #check home directory
115 0 return $ENV{HOME}. '.'. $self->_default_filename;
116 }
117 0 return; # if no matching files were found
118 }
119  
120 3 1;
121  
122 __END__
↑ Summary