Catmandu/Cmd/help.pm

filter: Statementstmt 33.3% 23 / 69 Branchbran 12.5% 2 / 16 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 53.3% 8 / 15 Podpod 60.0% 3 / 5 totaltotal 33.6% 37 / 110 CC 13 SCAR 30.5
CC
13
Cov
30%
help_about
56.0
execute
22.7
__ANON__
2.0
CRAP 21.1
« Catmandu/Fix/Condition/in.pm ↑ Summary Catmandu/Util.pm »
1 package Catmandu::Cmd::help;
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 'Catmandu::Cmd';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use App::Cmd::Command::help;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use Catmandu::Util qw(require_package pod_section);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶sub usage_desc {
Subroutine usage_desc: called CC=1 SCAR=0.0
Pod documented
13 5 '%c help [ <command> | ( export | import | store | fix ) <name> ]';
14 }
15  
16 60 ▶sub command_names {qw/help --help -h -?/}
Subroutine command_names: called CC=1 SCAR=0.0
Pod documented
17  
18 14 my %MODULES = (
19 Exporter => {
20 re => qr/^export(er)?$/i,
21 usage => [
22 "catmandu convert ... to %n [options]",
23 "catmandu export ... to %n [options]",
24 ],
25 },
26 Importer => {
27 re => qr/^import(er)?$/i,
28 usage => [
29 "catmandu convert %n [options] to ...",
30 "catmandu import %n [options] to ...",
31 ],
32 },
33 Store => {
34 re => qr/^(store|copy)$/i,
35 usage => [
36 "catmandu import ... to %n [options]",
37 "catmandu copy ... to %n [options]",
38 "catmandu export %n [options] ...",
39 "catmandu copy %n [options] ...",
40 ]
41 },
42 Fix => {re => qr/^fix$/i, usage => ["%n( [options] )"]},
43 'Fix::Bind' =>
44 {re => qr/^bind$/i, usage => ["do %n( [options] ) ... end"]},
45 'Fix::Condition' =>
46 {re => qr/^condition$/i, usage => ["if %n( [options] ) ... end"]},
47 );
48  
49 5 ▶sub execute {
Subroutine execute: called CC=7 SCAR=31.2
Pod documented
50 5 my ($self, $opts, $args) = @_;
51  
52 # TODO: list available Importer/Exporters/Stores/Fixes...
53  
54 5 ▶ if (@$args == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$args == 2) { } 0 5 5
elsif (@$args == 1 and $args->[0] =~ qr/^fix(es)?$/) { } 0 10 10
55  
56 # detect many forms such as:
57 # export JSON, exporter JSON, JSON export, JSON exporter
58 0 foreach (0, 1) {
59 0 foreach my $type (keys %MODULES) {
60 0 ▶ if ($args->[$_] =~ $MODULES{$type}->{re}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->[$_] =~ /$MODULES{$type}{"re"}/) 0 0 0
61 0 $self->help_about($type, $args->[($_ + 1) % 2]);
62 0 return;
63 }
64 }
65 }
66 }
67 ▶ elsif (@$args == 1 && $args->[0] =~ qr/^fix(es)?$/) {
Condition33%1/3
@$args == 1 and $args->[0] =~ qr/^fix(es)?$/
!ll&&!rl&&r
500
MC/DC0%0/2
@$args == 1 and $args->[0] =~ qr/^fix(es)?$/
@$args == 1 $args->[0] =~ qr/^fix(es)?$/
Truth table33%1/3
A and B
A= @$args == 1B= $args->[0] =~ qr/^fix(es)?$/
ABresult
0X0
100
111
68 0 $self->help_fixes;
69 0 return;
70 }
71  
72 5 App::Cmd::Command::help::execute(@_);
73 }
74  
75 0 ▶sub help_about {
Subroutine help_about: not called CC=7 SCAR=40.3
Pod undocumented
76 0 my ($self, $type, $name) = @_;
77  
78 0 my $class;
79 0 ▶ if ($type eq 'Fix') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "Fix") 0 0 0
80 0 foreach ('Fix', 'Fix::Bind', 'Fix::Condition') {
81 0 $type = $_;
82 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
83 0 require_package($name, "Catmandu::$type");
84 0 $class = "Catmandu::${type}::$name";
85 }
86 0 ▶ catch { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 ▶ last if $class;
Branch: 1 branch
BranchTrueFalseTotal
if $class 0 0 0
88 }
89 0 ▶ unless ($class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class) 0 0 0
90 0 Catmandu::NoSuchFixPackage->throw(
91 {
92 message => "No such fix package: $name",
93 package_name =>
94 "Catmandu::Fix::(Bind::|Condition::)?$name",
95 fix_name => $name,
96 }
97 );
98 }
99 }
100  
101 0 $class = "Catmandu::${type}::$name";
102 0 require_package($class);
103  
104 0 my $about = pod_section($class, "name");
105 0 $about =~ s/\n/ /mg;
106 0 say ucfirst($about);
107  
108 0 say "\nUsage:";
109 0 print join "", map {s/%n/$name/g; " $_\n"} @{$MODULES{$type}->{usage}};
110  
111 0 my $descr = pod_section($class, "description");
112 0 chomp $descr;
113 0 ▶ say "\n$descr" if $descr;
Branch: 1 branch
BranchTrueFalseTotal
if $descr 0 0 0
114  
115 # TODO: include examples?
116  
117 0 my $options = pod_section($class, "configuration");
118 0 ▶ if ($options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) 0 0 0
119 0 $options =~ s/^([a-z0-9_-]+)\s*\n?/--$1, /mgi;
120 0 $options
121 0 =~ s/^(--[a-z0-9_-]+(,\s*--[a-z0-9_-]+)*),\s*([^-])/" $1\n $3"/emgi;
122 0 print "\nOptions:\n$options";
123 }
124 }
125  
126 0 ▶sub help_fixes {
Subroutine help_fixes: not called CC=1 SCAR=6.9
Pod undocumented
127 0 my ($self) = @_;
128  
129 my $fixes = Catmandu->importer(
130 'Modules',
131 namespace => 'Catmandu::Fix',
132 primary => 1
133 )->select(name => qr/::[a-z][^:]*$/)->map(
134 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 0 $_[0]->{name} =~ s/.*:://;
136 0 $_[0];
137 }
138 0 );
139  
140 0 ▶ my $len = $fixes->max(sub {length $_[0]->{name}});
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 $fixes->sorted('name')->each(
142 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
143 0 say sprintf "%-${len}s %s", $_[0]->{name}, $_[0]->{about};
144 }
145 0 );
146  
147 0 say "\nGet additional help with: catmandu help fix <NAME>";
148 }
149  
150 14 1;
151  
152 __END__
« Catmandu/Fix/Condition/in.pm ↑ Summary Catmandu/Util.pm »