Catmandu/Cmd.pm

filter: Statementstmt 75.6% 56 / 74 Branchbran 61.1% 11 / 18 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 81.2% 13 / 16 Podpod 100% 7 / 7 totaltotal 74.5% 88 / 118 CC 12 SCAR 13.3
CC
12
Cov
72%
_parse_options
7.8
description
6.0
execute
4.1
CRAP 3.8
« Catmandu/Cmd/delete.pm ↑ Summary Catmandu/Importer/TSV.pm »
1 package Catmandu::Cmd;
2  
3 14 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 14 our $VERSION = '1.2025';
6  
7 14 ▶use parent qw(App::Cmd::Command);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use Catmandu::Util qw(is_array_ref pod_section);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use Catmandu::Fix;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Log::Any ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 0 ▶sub log {
Subroutine log: not called CC=1 SCAR=6.9
Pod documented
15 0 Log::Any->get_logger(category => ref($_[0]));
16 }
17  
18 # Internal required by App::Cmd;
19 39 ▶sub prepare {
Subroutine prepare: called CC=1 SCAR=0.0
Pod documented
20 39 my ($self, $app, @args) = @_;
21  
22 # not always available
23 39 eval {
24 39 require I18N::Langinfo;
25 39 I18N::Langinfo->import(qw(langinfo CODESET));
26 39 my $codeset = langinfo(CODESET());
27 39 @args = map {decode $codeset, $_} @args;
28 };
29 39 $self->SUPER::prepare($app, @args);
30 }
31  
32 # Internal required by App::Cmd
33 39 ▶sub opt_spec {
Subroutine opt_spec: called CC=1 SCAR=0.0
Pod documented
34 39 my ($class, $cli) = @_;
35 (
36 39 ['help|h|?', "print this usage screen"],
37 $cli->global_opt_spec, $class->command_opt_spec($cli),
38 );
39 }
40  
41 # Internal required by App::Cmd
42 34 ▶sub execute {
Subroutine execute: called CC=3 SCAR=14.2
Pod documented
43 34 my ($self, $opts, $args) = @_;
44  
45 34 ▶ if ($opts->{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"version"}) 0 34 34
46 0 print $Catmandu::VERSION;
47 0 exit;
48 }
49 34 ▶ if ($opts->{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"help"}) 0 34 34
50 0 print $self->usage->text;
51 0 exit;
52 }
53  
54 34 $self->command($opts, $args);
55 }
56  
57 # show examples, if available in POD
58 0 ▶sub description {
Subroutine description: not called CC=2 SCAR=17.9
Pod documented
59 0 my $class = ref shift;
60  
61 0 my $s = pod_section($class, "name");
62 0 $s =~ s/.*\s+-\s+//;
63 0 $s = ucfirst($s);
64 0 $s .= "\n";
65  
66 0 for (pod_section($class, "examples")) {
67 0 $s .= "Examples:\n\n$_";
68 }
69  
70 0 "$s\nOptions:";
71 }
72  
73 # These should be implemented by the Catmandu::Cmd's
74 5 ▶sub command_opt_spec { }
Subroutine command_opt_spec: called CC=1 SCAR=0.0
Pod documented
75 0 ▶sub command { }
Subroutine command: not called CC=1 SCAR=0.0
Pod documented
76  
77 # helpers
78 32 ▶sub _parse_options {
Subroutine _parse_options: called CC=7 SCAR=20.5
79 32 my ($self, $args, %opts) = @_;
80  
81 32 ▶ $opts{separator} //= 'to';
Condition50%1/2
$opts{"separator"} //= "to"
l!l
032
MC/DC0%0/1
$opts{"separator"} //= "to"
$opts{"separator"}
Truth table50%1/2
A
A= $opts{"separator"}
Aresult
11
00
82  
83 32 my $a = my $lft_args = [];
84 32 my $o = my $lft_opts = {};
85 32 my $rgt_args = [];
86 32 my $rgt_opts = {};
87  
88 32 ▶ for (my $i = 0; $i < @$args; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$args) 81 32 113
89 81 my $arg = $args->[$i];
90 81 ▶ if ($arg eq $opts{separator}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg eq $opts{'separator'}) { } 22 59 81
elsif ($arg =~ s/^-+//) { } 15 44 59
91 22 $a = $rgt_args;
92 22 $o = $rgt_opts;
93 }
94 elsif ($arg =~ s/^-+//) {
95 15 $arg =~ s/-/_/g;
96 15 ▶ if (exists $o->{$arg}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $o->{$arg}) { } 0 15 15
97 0 ▶ if (is_array_ref($o->{$arg})) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array_ref($o->{$arg})) { } 0 0 0
98 0 push @{$o->{$arg}}, $args->[++$i];
99 }
100 else {
101 0 $o->{$arg} = [$o->{$arg}, $args->[++$i]];
102 }
103 }
104 else {
105 15 $o->{$arg} = $args->[++$i];
106 }
107 }
108 else {
109 44 push @$a, $arg;
110 }
111 }
112  
113 32 return $lft_args, $lft_opts, $rgt_args, $rgt_opts;
114 }
115  
116 9 ▶sub _build_fixer {
Subroutine _build_fixer: called CC=3 SCAR=12.5
117 9 my ($self, $opts) = @_;
118 9 ▶ if ($opts->var) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->var) 0 9 9
119 0 return Catmandu::Fix->new(
120 preprocess => 1,
121 fixes => $opts->fix,
122 variables => $opts->var,
123 );
124 }
125 Catmandu::Fix->new(
126 9 ▶ preprocess => $opts->preprocess ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$opts->preprocess ? : 0 9 9
127 fixes => $opts->fix,
128 );
129 }
130  
131 14 1;
132  
133 __END__
« Catmandu/Cmd/delete.pm ↑ Summary Catmandu/Importer/TSV.pm »