Layered/Source/Getopt.pm

filter: Statementstmt 97.3% 36 / 37 Branchbran 91.6% 11 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 1 totaltotal 94.8% 55 / 58 CC 8 SCAR 11.5
CC
8
Cov
96%
get_config
5.0
_is_bool
4.1
BEGIN
1.0
CRAP 3.2
« Layered/Source/ENV.pm ↑ Summary Layered/Source/ConfigAny.pm »
1 package Config::Layered::Source::Getopt;
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Storable qw( dclone );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Scalar::Util qw( looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use base 'Config::Layered::Source';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 42 ▶sub get_config {
Subroutine get_config: called CC=5 SCAR=16.1
Pod undocumented
10 42 my ( $self ) = @_;
11 42 my @want;
12  
13 42 my $struct = dclone($self->layered->default);
14  
15 42 for my $key ( keys %{$struct} ) {
16 109 ▶ if ( ref $struct->{$key} eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $struct->{$key} eq 'ARRAY') { } 11 98 109
elsif (ref $struct->{$key} eq 'HASH') { } 3 95 98
elsif (_is_bool($struct->{$key})) { } 52 43 95
17 11 push @want, "$key=s@";
18 } elsif ( ref $struct->{$key} eq 'HASH' ) {
19 3 push @want, "$key=s%"
20 } elsif ( _is_bool($struct->{$key}) ) {
21 52 push @want, "$key!";
22 } else {
23 43 push @want, "$key=s";
24 }
25 }
26  
27 42 my %config;
28 42 GetOptions( \%config, @want );
29 42 return { %config };
30 }
31  
32 95 ▶sub _is_bool {
Subroutine _is_bool: called CC=4 SCAR=14.1
33 95 my ( $any ) = @_;
34 95 ▶ return 0 unless looks_like_number($any);
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($any) 43 52 95
35 52 ▶ return 1 if $any == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $any == 1 28 24 52
36 24 ▶ return 1 if $any == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $any == 0 24 0 24
37 0 return 0;
38 }
39  
40 3 1;
41  
42 =head1 NAME
43  
44 Config::Layered::Source::Getopt - The Command Line Source
45  
46 =head1 DESCRIPTION
47  
48 The Getopt source provides access to Getopt::Long and will configure
49 it based on the default data structure.
50  
51 The configuration of the Getopt::Long options is done in the following
52 way:
53  
54 =over 4
55  
56 =item * If the default value of a key is 0 or 1, it is treated as
57 a boolean, and the C<key!> directive is used. This will enable C<--no*>
58 options to work as expected.
59  
60 =item * If the value is a hash reference, the C<key%> directive is used,
61 and options are configured as C<--key name=value>. New hash keys will
62 be added, previously used hash keys (i.e. default configuration, previously
63 run sources) will be replaced.
64  
65 =item * If the value if an array reference, the C<key@> directive is used,
66 and options are configured as C<--key value --key value>. An array entered
67 by this source will replace a previously entered array (i.e. default
68 configuration, previous run sources).
69  
70 =item * All other situations will result in a simple string, C<key=s>.
71  
72 =head1 EXAMPLE
73  
74 my $config = Config::Layered->load_config(
75 sources => [ 'ConfigAny' => { file => "/etc/myapp" } ],
76 default => {
77 foo => "bar",
78 blee => 1,
79 size => 20,
80 bax => { chicken => "eggs", }
81 baz => [ wq( foo bar blee ) ]
82 }
83 );
84  
85 The above data structure would create the following Getopt::Long
86 configuration:
87  
88 Getopts( \%config,
89 "foo=s",
90 "blee!",
91 "size",
92 "bax%",
93 "baz@",
94 );
95
96 =head1 SOURCE ARGUMENTS
97  
98 =over 4
99  
100 =item * None
101  
102 =back
103  
104 =cut
« Layered/Source/ENV.pm ↑ Summary Layered/Source/ConfigAny.pm »