blib/lib/CLI/Osprey/Descriptive/Usage.pm

filter: Statementstmt 53.3% 88 / 165 Branchbran 35.0% 21 / 60 Conditioncond 15.0% 3 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 55.5% 10 / 18 Podpod 0.0% 0 / 12 totaltotal 42.3% 122 / 288 CC 43 SCAR 31.1
CC
43
Cov
46%
sub_commands_text
56.0
wrap
42.0
describe_opt
27.6
CRAP 22.5
« blib/lib/CLI/Osprey/Role.pm ↑ Summary blib/lib/CLI/Osprey.pm »
1 package CLI::Osprey::Descriptive::Usage;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 use overload (
8 5 q{""} => "text",
9 5 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use Getopt::Long::Descriptive::Usage ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 *option_text = \&Getopt::Long::Descriptive::Usage::option_text;
14  
15 # ABSTRACT: Produce usage information for CLI::Osprey apps
16 5 our $VERSION = '0.08'; # VERSION
17 5 our $AUTHORITY = 'cpan:ARODLAND'; # AUTHORITY
18  
19 5 my %format_doc = (
20 s => { short => "string", long => "string" },
21 i => { short => "int" , long => "integer" },
22 o => { short => "int" , long => "integer (dec/hex/bin/oct)" },
23 f => { short => "num" , long => "number" },
24 );
25  
26 5 has 'options' => (
27 is => 'ro',
28 );
29  
30 5 has 'leader_text' => (
31 is => 'ro',
32 );
33  
34 5 has 'target' => (
35 is => 'ro',
36 predicate => 1,
37 );
38  
39 5 has 'prog_name' => (
40 is => 'ro',
41 predicate => 1,
42 );
43  
44 has 'width' => (
45 is => 'ro',
46 default => sub {
47 16 ▶ return $ENV{CLI_OSPREY_OVERRIDE_WIDTH} if exists $ENV{CLI_OSPREY_OVERRIDE_WIDTH};
Subroutine __ANON__: called CC=3 SCAR=11.7
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{"CLI_OSPREY_OVERRIDE_WIDTH"} 0 16 16
48 16 ▶ return $ENV{COLUMNS} if exists $ENV{COLUMNS};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{"COLUMNS"} 0 16 16
49 16 return 80;
50 },
51 5 );
52  
53 sub wrap {
54 0 ▶ my ($self, $in, $prefix) = @_;
Subroutine wrap: not called CC=6 SCAR=37.4
Pod undocumented
55  
56 0 my $width = $self->width;
57 0 ▶ return $in if $width <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $width <= 0 0 0 0
58  
59 0 my @out;
60 0 my $line = "";
61  
62 0 ▶ while ($in =~ /(\s*)(\S+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /(\s*)(\S+)/g) 0 0 0
63 0 my ($space, $nonspace) = ($1, $2);
64 0 ▶ if (length($line) + length($space) + length($nonspace) <= $width) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($line) + length($space) + length($nonspace) <= $width) { } 0 0 0
65 0 $line .= $space . $nonspace;
66 } else {
67 0 ▶ while (length($nonspace)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $nonspace) 0 0 0
68 0 push @out, $line;
69 0 $line = $prefix;
70 0 $line .= substr($nonspace, 0, $width - length($line), '');
71 }
72 }
73 }
74 0 ▶ push @out, $line if length($line);
Branch: 1 branch
BranchTrueFalseTotal
if length $line 0 0 0
75 0 return @out;
76 }
77  
78 sub maxlen {
79 0 ▶ my $max = 0;
Subroutine maxlen: not called CC=3 SCAR=24.8
Pod undocumented
80 0 for (@_) {
81 0 ▶ $max = length($_) if length($_) > $max;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ > $max 0 0 0
82 }
83 0 return $max;
84 }
85  
86 sub sub_commands_text {
87 0 ▶ my ($self, $length) = @_;
Subroutine sub_commands_text: not called CC=7 SCAR=40.3
Pod undocumented
88  
89 0 ▶ if ($self->has_target && (my %subcommands = $self->target->_osprey_subcommands)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_target and my(%subcommands) = $self->target->_osprey_subcommands) 0 0 0
Condition0%0/3
$self->has_target and my(%subcommands) = $self->target->_osprey_subcommands
!ll&&!rl&&r
000
MC/DC0%0/2
$self->has_target and my(%subcommands) = $self->target->_osprey_subcommands
$self->has_target my(%subcommands) = $self->target->_osprey_subcommands
Truth table0%0/3
A and B
A= $self->has_targetB= my(%subcommands) = $self->target->_osprey_subcommands
ABresult
0X0
100
111
90 0 ▶ if ($length eq 'long') {
Branch: 1 branch
BranchTrueFalseTotal
if ($length eq 'long') { } 0 0 0
91 0 my $maxlen = maxlen(keys %subcommands);
92  
93 0 my @out;
94 0 push @out, "";
95 0 push @out, "Subcommands available:";
96  
97 0 for my $name (sort keys %subcommands) {
98 0 ▶ my $desc = $subcommands{$name}->can('_osprey_subcommand_desc') && $subcommands{$name}->_osprey_subcommand_desc;
Condition0%0/3
$subcommands{$name}->can('_osprey_subcommand_desc') && $subcommands{$name}->_osprey_subcommand_desc
!ll&&!rl&&r
000
MC/DC0%0/2
$subcommands{$name}->can('_osprey_subcommand_desc') && $subcommands{$name}->_osprey_subcommand_desc
$subcommands{$name}->can('_osprey_subcommand_desc') $subcommands{$name}->_osprey_subcommand_desc
Truth table0%0/3
A && B
A= $subcommands{$name}->can('_osprey_subcommand_desc')B= $subcommands{$name}->_osprey_subcommand_desc
ABresult
0X0
100
111
99 0 ▶ if (defined $desc) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $desc) { } 0 0 0
100 push @out, $self->wrap(
101 0 sprintf("%*s %s", -$maxlen, $name, $subcommands{$name}->_osprey_subcommand_desc),
102 " " x ($maxlen + 2)
103 );
104 } else {
105 0 push @out, $name;
106 }
107 }
108 0 push @out, "";
109  
110 0 return @out;
111 } else {
112 0 return "",
113 $self->wrap(
114 "Subcommands available: " . join(" | ", sort keys %subcommands),
115 " " x length("Subcommands available: ")
116 );
117 }
118 }
119 0 return;
120 }
121  
122 sub pod_escape {
123 3 ▶ my ($self, $text) = @_;
Subroutine pod_escape: called CC=1 SCAR=0.1
Pod undocumented
124 3 my %map = (
125 '<' => 'lt',
126 '>' => 'gt',
127 '|' => 'verbar',
128 '/' => 'sol',
129 );
130  
131 3 $text =~ s,([<>|/]),"E<$map{$1}>",eg;
132 3 return $text;
133 }
134  
135 sub describe_opt {
136 4 ▶ my ($self, $opt) = @_;
Subroutine describe_opt: called CC=15 SCAR=33.2
Pod undocumented
137  
138 4 ▶ if ($opt->{desc} eq 'spacer') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"desc"} eq "spacer") 1 3 4
139 1 return { spacer => 1 };
140 }
141  
142 3 my $name = my $attr_name = $opt->{name};
143  
144 3 my $option_attrs;
145  
146 3 ▶ if ($self->has_target) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_target) 3 0 3
147 3 my %options = $self->target->_osprey_options;
148 3 $option_attrs = $options{$attr_name};
149 3 ▶ $name = $option_attrs->{option} if defined $option_attrs->{option};
Branch: 1 branch
BranchTrueFalseTotal
if defined $option_attrs->{"option"} 0 3 3
150 }
151  
152 3 my ($short, $format) = $opt->{spec} =~ /(?:\|(\w))?(?:=(.*?))?$/;
153  
154 3 my $array;
155 3 ▶ if (defined $format && $format =~ s/[\@\+]$//) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $format and $format =~ s/[\@\+]$//) 0 3 3
Condition33%1/3
defined $format and $format =~ s/[\@\+]$//
!ll&&!rl&&r
300
MC/DC0%0/2
defined $format and $format =~ s/[\@\+]$//
defined $format $format =~ s/[\@\+]$//
Truth table33%1/3
A and B
A= defined $formatB= $format =~ s/[\@\+]$//
ABresult
0X0
100
111
156 0 $array = 1;
157 }
158  
159 3 my $format_doc;
160 3 ▶ if (defined $format) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $format) 0 3 3
161 0 ▶ if (defined $option_attrs->{format_doc}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $option_attrs->{'format_doc'}) { } 0 0 0
162 $format_doc = {
163 short => $option_attrs->{format_doc},
164 long => $option_attrs->{format_doc},
165 0 };
166 } else {
167 0 $format_doc = $format_doc{$format};
168 }
169 }
170  
171 3 my $spec;
172  
173 3 ▶ if ($short) {
Branch: 1 branch
BranchTrueFalseTotal
if ($short) 0 3 3
174 0 $spec = "-$short|";
175 }
176  
177 3 ▶ if (length($name) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $name > 1) { } 2 1 3
178 2 $spec .= "--$name";
179 } else {
180 1 $spec .= "-$name";
181 }
182  
183 3 my ($shortspec, $longspec) = ($spec, $spec);
184 3 my ($podshortspec, $podlongspec) = ("B<< $spec >>", "B<< $spec >>");
185  
186 3 ▶ if (defined $format_doc) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $format_doc) 0 3 3
187 0 $shortspec .= " $format_doc->{short}";
188 0 $podshortspec .= " I<< $format_doc->{short} >>";
189 0 $longspec .= " $format_doc->{long}";
190 0 $podlongspec .= " I<< $format_doc->{long} >>";
191 }
192  
193 3 ▶ if ($array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($array) 0 3 3
194 0 $shortspec .= "...";
195 0 $podshortspec .= "...";
196 }
197  
198 3 ▶ if (defined $option_attrs && !$option_attrs->{required}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $option_attrs and not $option_attrs->{"required"}) 3 0 3
Condition33%1/3
defined $option_attrs and not $option_attrs->{"required"}
!ll&&!rl&&r
003
MC/DC0%0/2
defined $option_attrs and not $option_attrs->{"required"}
defined $option_attrs not $option_attrs->{"required"}
Truth table33%1/3
A and not(B)
A= defined $option_attrsB= not $option_attrs->{"required"}
ABresult
0X0
100
111
199 3 $shortspec = "[$shortspec]";
200 3 $podshortspec = "[$podshortspec]";
201 }
202  
203 return {
204 short => $shortspec,
205 long => $longspec,
206 podshort => $podshortspec,
207 podlong => $podlongspec,
208 doc => $opt->{desc},
209 3 ▶ long_doc => defined($option_attrs->{long_doc}) ? $option_attrs->{long_doc} : $self->pod_escape($opt->{desc}),
Branch: 1 branch
BranchTrueFalseTotal
defined $option_attrs->{'long_doc'} ? : 0 3 3
210 };
211 }
212  
213 sub describe_options {
214 1 ▶ my ($self) = @_;
Subroutine describe_options: called CC=1 SCAR=0.0
Pod undocumented
215  
216 1 return map $self->describe_opt($_), @{ $self->options };
217 }
218  
219 sub header {
220 0 ▶ my ($self) = @_;
Subroutine header: not called CC=1 SCAR=6.9
Pod undocumented
221  
222 0 my @descs = $self->describe_options;
223  
224 my $option_text = join "\n", $self->wrap(
225 0 join(" ", map $_->{short}, grep !$_->{spacer}, @descs),
226 " ",
227 );
228  
229 0 my $text = $self->leader_text;
230 0 $text =~ s/\Q[long options...]/$option_text/;
231  
232 0 return $text;
233 }
234  
235 sub text {
236 0 ▶ my ($self) = @_;
Subroutine text: not called CC=1 SCAR=6.9
Pod undocumented
237  
238 0 return join "\n", $self->header, $self->sub_commands_text('short');
239 }
240  
241 sub option_help {
242 0 ▶ my ($self) = @_;
Subroutine option_help: not called CC=3 SCAR=24.8
Pod undocumented
243  
244 0 my @descs = $self->describe_options;
245  
246 0 my $maxlen = maxlen(map $_->{long}, grep !$_->{spacer}, @descs);
247  
248 0 my @out;
249 0 for my $desc (@descs) {
250 0 ▶ if ($desc->{spacer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc->{'spacer'}) { } 0 0 0
251 0 push @out, "";
252 } else {
253 push @out, $self->wrap(
254 0 sprintf("%*s %s", -$maxlen, $desc->{long}, $desc->{doc}),
255 " " x ($maxlen + 2),
256 );
257 }
258 }
259  
260 0 return join("\n", $self->header, $self->sub_commands_text('long'), @out);
261 }
262  
263 sub option_pod {
264 1 ▶ my ($self) = @_;
Subroutine option_pod: called CC=11 SCAR=28.3
Pod undocumented
265  
266 1 my %osprey_config = $self->target->_osprey_config;
267  
268 1 my @descs = $self->describe_options;
269 1 my @pod;
270  
271 1 push @pod, "=encoding UTF-8";
272  
273 1 push @pod, "=head1 NAME";
274 1 ▶ push @pod, $self->{prog_name} . ($osprey_config{desc} ? " - " . $osprey_config{desc} : "" );
Branch: 1 branch
BranchTrueFalseTotal
$osprey_config{'desc'} ? : 0 1 1
275  
276 1 push @pod, "=head1 SYNOPSIS";
277 push @pod, "B<< $self->{prog_name} >> "
278 1 . join(" ", map "S<<< $_->{podshort} >>>", grep !$_->{spacer}, @descs);
279  
280 1 ▶ if ($osprey_config{description_pod}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($osprey_config{"description_pod"}) 1 0 1
281 1 push @pod, "=head1 DESCRIPTION";
282 1 push @pod, $osprey_config{description_pod};
283 }
284  
285 1 ▶ if ($osprey_config{extra_pod}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($osprey_config{"extra_pod"}) 1 0 1
286 1 push @pod, $osprey_config{extra_pod};
287 }
288  
289 1 push @pod, "=head1 OPTIONS";
290 1 push @pod, "=over";
291  
292 1 for my $desc (@descs) {
293 4 ▶ if ($desc->{spacer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc->{'spacer'}) { } 1 3 4
294 1 push @pod, "=back";
295 1 push @pod, "E<32>" x 40;
296 1 push @pod, "=over";
297 } else {
298 3 push @pod, "=item $desc->{podlong}";
299 3 push @pod, $desc->{long_doc};
300 }
301 }
302  
303 1 push @pod, "=back";
304  
305 1 ▶ if ($self->has_target && (my %subcommands = $self->target->_osprey_subcommands)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_target and my(%subcommands) = $self->target->_osprey_subcommands) 0 1 1
Condition33%1/3
$self->has_target and my(%subcommands) = $self->target->_osprey_subcommands
!ll&&!rl&&r
010
MC/DC0%0/2
$self->has_target and my(%subcommands) = $self->target->_osprey_subcommands
$self->has_target my(%subcommands) = $self->target->_osprey_subcommands
Truth table33%1/3
A and B
A= $self->has_targetB= my(%subcommands) = $self->target->_osprey_subcommands
ABresult
0X0
100
111
306 0 push @pod, "=head1 COMMANDS";
307 0 push @pod, "=over";
308  
309 0 for my $name (sort keys %subcommands) {
310 0 ▶ my $desc = $subcommands{$name}->can('_osprey_subcommand_desc') && $subcommands{$name}->_osprey_subcommand_desc;
Condition0%0/3
$subcommands{$name}->can('_osprey_subcommand_desc') && $subcommands{$name}->_osprey_subcommand_desc
!ll&&!rl&&r
000
MC/DC0%0/2
$subcommands{$name}->can('_osprey_subcommand_desc') && $subcommands{$name}->_osprey_subcommand_desc
$subcommands{$name}->can('_osprey_subcommand_desc') $subcommands{$name}->_osprey_subcommand_desc
Truth table0%0/3
A && B
A= $subcommands{$name}->can('_osprey_subcommand_desc')B= $subcommands{$name}->_osprey_subcommand_desc
ABresult
0X0
100
111
311 0 push @pod, "=item B<< $name >>";
312 0 ▶ if ($desc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc) 0 0 0
313 0 push @pod, $desc;
314 }
315 }
316  
317 0 push @pod, "=back";
318 }
319  
320 1 return join("\n\n", @pod);
321 }
322  
323 sub die {
324 0 ▶ my $self = shift;
Subroutine die: not called CC=2 SCAR=17.9
Pod undocumented
325 0 ▶ my $arg = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
326  
327 die(
328 0 join q{}, grep { defined } $arg->{pre_text}, $self->text, $arg->{post_text}
329 0 );
330 }
331  
332 0 ▶sub warn { warn shift->text }
Subroutine warn: not called CC=1 SCAR=6.9
Pod undocumented
333  
334 5 1;
335  
336 __END__
« blib/lib/CLI/Osprey/Role.pm ↑ Summary blib/lib/CLI/Osprey.pm »