| 1 | package Getopt::Yath::Option::Map; | ||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 1 | our $VERSION = '2.000007'; | |||||||||||
| 6 | |||||||||||||
| 7 | 1 | ▶ | use Getopt::Yath::Util qw/decode_json/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 1 | ▶ | use parent 'Getopt::Yath::Option'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 1 | ▶ | use Getopt::Yath::HashBase qw/<split_on <key_on/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 4 | ▶ | sub allows_list { 1 } | ||||||||||
|
Subroutine
allows_list: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 13 | 1 | ▶ | sub allows_default { 1 } | ||||||||||
|
Subroutine
allows_default: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 14 | 6 | ▶ | sub allows_arg { 1 } | ||||||||||
|
Subroutine
allows_arg: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 15 | 5 | ▶ | sub requires_arg { 1 } | ||||||||||
|
Subroutine
requires_arg: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 16 | 0 | ▶ | sub allows_autofill { 0 } | ||||||||||
|
Subroutine
allows_autofill: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 17 | 1 | ▶ | sub requires_autofill { 0 } | ||||||||||
|
Subroutine
requires_autofill: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 18 | |||||||||||||
| 19 | 4 | ▶ | sub notes { (shift->SUPER::notes(), 'Can be specified multiple times') } | ||||||||||
|
Subroutine
notes: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 20 | |||||||||||||
| 21 | sub _example_append { | ||||||||||||
| 22 | 4 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_example_append: called CC=5 SCAR=16.1
| |||||||||||||
| 23 | 4 | my ($params, @prefixes) = @_; | |||||||||||
| 24 | |||||||||||||
| 25 | 4 | ▶ | return unless $self->allows_list; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 26 | |||||||||||||
| 27 | 4 | ▶ | my $groups = $params->{groups} // {}; | ||||||||||
|
Condition100%2/2
$params->{"groups"} // {}
MC/DC100%1/1
$params->{"groups"} // {}
$params->{"groups"}
Truth table100%2/2
A
A= $params->{"groups"}
| |||||||||||||
| 28 | |||||||||||||
| 29 | 4 | my @out; | |||||||||||
| 30 | |||||||||||||
| 31 | 4 | for my $prefix (@prefixes) { | |||||||||||
| 32 | 10 | for my $group (sort keys %$groups) { | |||||||||||
| 33 | 5 | push @out => "${prefix}${group} KEY1 VAL KEY2 ${group} VAL1 VAL2 ... $groups->{$group} ... $groups->{$group}"; | |||||||||||
| 34 | } | ||||||||||||
| 35 | } | ||||||||||||
| 36 | |||||||||||||
| 37 | 4 | return @out; | |||||||||||
| 38 | } | ||||||||||||
| 39 | |||||||||||||
| 40 | sub default_long_examples { | ||||||||||||
| 41 | 2 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
default_long_examples: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 42 | 2 | my %params = @_; | |||||||||||
| 43 | |||||||||||||
| 44 | 2 | my @append = $self->_example_append(\%params, ' ', '='); | |||||||||||
| 45 | |||||||||||||
| 46 | 2 | return [' key=val', '=key=val', qq[ '{"json":"hash"}'], qq[='{"json":"hash"}'], @append]; | |||||||||||
| 47 | } | ||||||||||||
| 48 | |||||||||||||
| 49 | sub default_short_examples { | ||||||||||||
| 50 | 2 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
default_short_examples: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 51 | 2 | my %params = @_; | |||||||||||
| 52 | |||||||||||||
| 53 | 2 | my @append = $self->_example_append(\%params, '', ' ', '='); | |||||||||||
| 54 | |||||||||||||
| 55 | 2 | return [' key=val', 'key=value', '=key=val', qq[ '{"json":"hash"}'], qq[='{"json":"hash"}'], @append]; | |||||||||||
| 56 | } | ||||||||||||
| 57 | |||||||||||||
| 58 | sub init { | ||||||||||||
| 59 | 2 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
init: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||
| 60 | |||||||||||||
| 61 | 2 | $self->SUPER::init(); | |||||||||||
| 62 | |||||||||||||
| 63 | 2 | ▶ | $self->{+KEY_ON} //= '='; | ||||||||||
|
Condition50%1/2
$self->{"key_on"} //= "="
MC/DC0%0/1
$self->{"key_on"} //= "="
$self->{"key_on"}
Truth table50%1/2
A
A= $self->{"key_on"}
| |||||||||||||
| 64 | } | ||||||||||||
| 65 | |||||||||||||
| 66 | 48 | ▶ | sub is_populated { ${$_[1]} && keys %{${$_[1]}} } | ||||||||||
|
Subroutine
is_populated: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||
| 67 | |||||||||||||
| 68 | sub get_initial_value { | ||||||||||||
| 69 | 54 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
get_initial_value: called CC=7 SCAR=22.5
Pod
undocumented
| |||||||||||||
| 70 | |||||||||||||
| 71 | 54 | my %val; | |||||||||||
| 72 | |||||||||||||
| 73 | 54 | my $env = $self->from_env_vars; | |||||||||||
| 74 | 54 | ▶ | for my $name (@{$env || []}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | 0 | ▶ | $val{$name} = $ENV{$name} if defined $ENV{$name}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | } | ||||||||||||
| 77 | |||||||||||||
| 78 | 54 | ▶ | return \%val if keys %val; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | |||||||||||||
| 80 | 54 | ▶ | return undef if $self->{+MAYBE}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | |||||||||||||
| 82 | 54 | ▶ | return $self->_get___value(INITIALIZE()) // {}; | ||||||||||
|
Condition50%1/2
$self->_get___value("initialize") // {}
MC/DC0%0/1
$self->_get___value("initialize") // {}
$self->_get___value("initialize")
Truth table50%1/2
A
A= $self->_get___value("initialize")
| |||||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | sub get_clear_value { | ||||||||||||
| 86 | 2 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
get_clear_value: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||
| 87 | 2 | ▶ | return $self->_get___value(CLEAR(), @_) // {}; | ||||||||||
|
Condition50%1/2
$self->_get___value("clear", @_) // {}
MC/DC0%0/1
$self->_get___value("clear", @_) // {}
$self->_get___value("clear", @_)
Truth table50%1/2
A
A= $self->_get___value("clear", @_)
| |||||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | sub add_value { | ||||||||||||
| 91 | 105 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
add_value: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 92 | 105 | my ($ref, %vals) = @_; | |||||||||||
| 93 | |||||||||||||
| 94 | 105 | ▶ | return unless keys %vals; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | |||||||||||||
| 96 | 51 | ▶ | $$ref //= {}; | ||||||||||
|
Condition100%2/2
$$ref //= {}
MC/DC100%1/1
$$ref //= {}
$$ref
Truth table100%2/2
A
A= $$ref
| |||||||||||||
| 97 | |||||||||||||
| 98 | 51 | %{$$ref} = ( | |||||||||||
| 99 | 51 | %{$$ref}, | |||||||||||
| 100 | %vals, | ||||||||||||
| 101 | ); | ||||||||||||
| 102 | } | ||||||||||||
| 103 | |||||||||||||
| 104 | sub normalize_value { | ||||||||||||
| 105 | 8 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
normalize_value: called CC=6 SCAR=23.2
Pod
undocumented
| |||||||||||||
| 106 | 8 | my (@input) = @_; | |||||||||||
| 107 | |||||||||||||
| 108 | 8 | ▶ | return $self->SUPER::normalize_value(@input) if @input > 1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 109 | |||||||||||||
| 110 | 8 | ▶ | if ($input[0] =~ m/^\s*\{.*\}\s*$/s) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 111 | 0 | my $out; | |||||||||||
| 112 | 0 | local $@; | |||||||||||
| 113 | 0 | ▶ | unless (eval { local $SIG{__DIE__}; $out = decode_json($input[0]); 1 }) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 114 | 0 | my ($err) = split /[\n\r]+/, $@; | |||||||||||
| 115 | 0 | $err =~ s{at \Q$INC{'Getopt/Yath/Util/JSON.pm'}\E line \d+\..*$}{}; | |||||||||||
| 116 | 0 | die "Could not decode JSON string: $err\n====\n$input[0]\n====\n"; | |||||||||||
| 117 | } | ||||||||||||
| 118 | 0 | return %$out; | |||||||||||
| 119 | } | ||||||||||||
| 120 | |||||||||||||
| 121 | 8 | my @split; | |||||||||||
| 122 | 8 | ▶ | if (my $on = $self->split_on) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 123 | 8 | @split = grep { length($_) } map { split($on, $_) } @input; | |||||||||||
| 124 | } | ||||||||||||
| 125 | else { | ||||||||||||
| 126 | 0 | @split = @input; | |||||||||||
| 127 | } | ||||||||||||
| 128 | |||||||||||||
| 129 | 8 | ▶ | my $key_on = $self->key_on // '='; | ||||||||||
|
Condition50%1/2
$self->key_on // "="
MC/DC0%0/1
$self->key_on // "="
$self->key_on
Truth table50%1/2
A
A= $self->key_on
| |||||||||||||
| 130 | 8 | my %output = map { my ($k, $v) = split($key_on, $_, 2); $self->SUPER::normalize_value($k, $v) } @split; | |||||||||||
| 131 | |||||||||||||
| 132 | 8 | return %output; | |||||||||||
| 133 | } | ||||||||||||
| 134 | |||||||||||||
| 135 | 1 | 1; | |||||||||||
| 136 | |||||||||||||
| 137 | __END__ | ||||||||||||