blib/lib/App/karr/Role/ExitCodes.pm

filter: Statementstmt 10.6% 19 / 178 Branchbran 1.8% 2 / 108 Conditioncond 1.7% 1 / 58 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 29.4% 5 / 17 Podpod 100% 1 / 1 totaltotal 7.0% 28 / 396 CC 84 SCAR 48.0
CC
84
Cov
6%
_render_help_long
380.0
_option_hint
90.0
_usage_error_last
90.0
CRAP 121.7
« blib/lib/App/karr/Cmd/Dashboard.pm ↑ Summary blib/lib/App/karr/Cmd/Sync.pm »
1 # ABSTRACT: Normalize MooX::Options option-parse errors to exit code 2 (ADR 0002)
2  
3 package App::karr::Role::ExitCodes;
4 12 our $VERSION = '0.602';
5 12 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # Both loaded without importing, and every call below is qualified: a Moo::Role
7 # composes every sub in its package into its consumers, imported ones included
8 # (#38), and these two export names a command class wants for itself.
9 12 ▶use App::karr::Encoding ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use App::karr::Error ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 0 ▶sub usage_error {
Subroutine usage_error: not called CC=3 SCAR=24.8
Pod documented
14 0 my ($self, $message) = @_;
15 0 ▶ chomp $message if defined $message;
Branch: 1 branch
BranchTrueFalseTotal
if defined $message 0 0 0
16 0 ▶ die "Usage error: " . ( defined $message ? $message : 'invalid invocation' ) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $message ? : 0 0 0
17 }
18  
19 #### The answer goes last (ticket k263)
20  
21 # MooX::Options prints its diagnostic before the usage block, and neither writer
22 # is reachable from a method modifier -- they sit in two different places and
23 # neither hands the text on:
24 #
25 # * Getopt::Long warns "Unknown option: bogus" / "Option claim requires an
26 # argument" from inside describe_options, after which MooX::Options::Role
27 # calls options_usage(1, $usage) (4.103, parse_options line 372).
28 # Getopt::Long::Descriptive installs no $SIG{__WARN__} of its own -- it says
29 # why at line 306, "too heavy a hammer" -- so the field is free, but a
30 # handler would catch only this first writer.
31 # * an option declared `required => 1` and not given fails in $class->new, and
32 # new_with_options reports it with a bare `print STDERR "$1 is missing"`
33 # (4.103, lines 293-315) before calling options_usage. That one no warn
34 # handler ever sees, which is why STDERR itself is captured rather than
35 # warnings.
36 #
37 # Both writers are inside new_with_options, so STDERR is buffered for exactly
38 # the length of that call: option parsing and the constructor, never a command
39 # body. Nothing is dropped. Whatever was buffered is printed on every path --
40 # reordered when the failure lands in options_usage or _print_help, verbatim
41 # when new_with_options returns or dies, and from the END block below when
42 # something else exits first (`--man` leaves through options_man).
43 #
44 # This can go when MooX::Options prints its diagnostic after the usage block, or
45 # passes it to options_usage as a message instead of writing it out itself: both
46 # writers are unconditional in 4.103, which is the current release.
47 12 my $CAPTURE;
48  
49 0 ▶sub _start_option_capture {
Subroutine _start_option_capture: not called CC=3 SCAR=24.8
50 0 ▶ return if $CAPTURE;
Branch: 1 branch
BranchTrueFalseTotal
if $CAPTURE 0 0 0
51 0 my $octets = '';
52 0 ▶ open my $handle, '>', \$octets or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, ">", \$octets or return 0 0 0
53 # The in-memory handle is an octet edge like every other one karr has, so it
54 # is crossed the way App::karr::Encoding crosses them: :utf8 keeps a wide
55 # character out of "Wide character in warn" and stores its UTF-8, and
56 # _end_option_capture decodes it back before anything is printed. Only
57 # STDERR's IO slot is swapped, so the encoding layer F<bin/karr> put on the
58 # real handle is still there when it is swapped back.
59 0 binmode $handle, ':utf8';
60 0 $CAPTURE = { handle => $handle, octets => \$octets, stderr => *STDERR{IO} };
61 0 *STDERR = *$handle{IO};
62 0 return;
63 }
64  
65 12 ▶sub _end_option_capture {
Subroutine _end_option_capture: called CC=2 SCAR=11.6
66 12 my $capture = $CAPTURE;
67 12 ▶ return unless $capture;
Branch: 1 branch
BranchTrueFalseTotal
unless $capture 12 0 12
68 0 undef $CAPTURE;
69 0 *STDERR = $capture->{stderr};
70 0 close $capture->{handle}; # the :utf8 layer flushes into the buffer on close
71 0 return App::karr::Encoding::from_octets( ${ $capture->{octets} } );
72 }
73  
74 # Nothing is swallowed on a path that leaves without reaching either caller of
75 # _usage_error_last -- `--man` and `--help` exit from inside MooX::Options. END
76 # runs late enough to be the backstop and restores STDERR itself, so a buffer
77 # nobody claimed is still printed.
78 12 ▶END {
Subroutine END: called CC=3 SCAR=13.1
79 12 my $left = _end_option_capture();
80 12 ▶ print STDERR $left if defined $left && length $left;
Branch: 1 branch
BranchTrueFalseTotal
if defined $left and length $left 0 12 12
Condition33%1/3
defined $left and length $left
!ll&&!rl&&r
1200
MC/DC0%0/2
defined $left and length $left
defined $left length $left
Truth table33%1/3
A and B
A= defined $leftB= length $left
ABresult
0X0
100
111
81 }
82  
83 # The suggestion line for an option-parse diagnostic, or nothing. Three shapes
84 # reach here, and they are the three MooX::Options 4.103 produces:
85 #
86 # Unknown option: bogus_opt Getopt::Long, via describe_options
87 # Option claim requires an argument Getopt::Long, same place
88 # claim is missing MooX::Options::Role, for `required => 1`
89 #
90 # Only the last two get a suggestion. Anything else -- an unrecognised shape,
91 # no recorded argv -- keeps the reordering and loses the suggestion, which is
92 # the right way round: the diagnostic is MooX::Options' to word, the suggestion
93 # is karr's to offer, and karr offers one only where it has one.
94 0 ▶sub _option_hint {
Subroutine _option_hint: not called CC=9 SCAR=45.0
95 0 my ( $self, $diagnostic ) = @_;
96  
97 0 my $argv = App::karr::Error::original_argv();
98 0 ▶ return unless defined $argv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $argv 0 0 0
99  
100 # [ token, is it the caller's own word? ] -- the second half is k263's rule
101 # that a line carrying nothing anybody typed is not printed at all.
102 0 my @tokens = map { [ $_, 1 ] } @$argv;
103 0 my $edited = 0;
104  
105 0 for my $line ( split /\n/, $diagnostic ) {
106 # An unknown option is the one shape with no honest suggestion behind
107 # it, so it gets none -- and therefore no colon either. The other two
108 # are missing a VALUE and the value is what gets filled in, so the
109 # caller's intent survives whole. Here the only line that would run is
110 # the caller's own word thrown away: `karr move 1 todo --bogus` would
111 # be answered with `karr move 1 todo`, a card being moved that nobody
112 # asked to move, handed to an agent reading `tail -1`. What the caller
113 # needs is the list of names that do exist, and the usage block
114 # directly above is that list. It suppresses the whole suggestion and
115 # not just its own edit: a line built around an option karr could not
116 # read is not one karr can stand behind either.
117 0 ▶ return if $line =~ /\AUnknown option: /;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\AUnknown option: / 0 0 0
118  
119 0 ▶ if ( $line =~ /\AOption (\S+) requires an argument\z/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /\AOption (\S+) requires an argument\z/) { } 0 0 0
elsif ($line =~ /\A(\w+) is missing\z/) { } 0 0 0
120 0 my $name = $1;
121 0 my $at = _flag_position( \@tokens, $name );
122 0 my $slot = [ $self->_usage_placeholder($name), 0 ];
123 0 ▶ if ( defined $at ) { splice @tokens, $at + 1, 0, $slot }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $at) { } 0 0 0
124 0 else { push @tokens, [ '--' . $name, 0 ], $slot }
125 0 $edited = 1;
126 }
127 elsif ( $line =~ /\A(\w+) is missing\z/ ) {
128 # The option is not in argv at all, so it is appended to what is:
129 # `karr handoff 1` -> `karr handoff 1 --claim NAME`.
130 0 push @tokens, [ '--' . $1, 0 ], [ $self->_usage_placeholder($1), 0 ];
131 0 $edited = 1;
132 }
133 }
134  
135 0 ▶ return unless $edited;
Branch: 1 branch
BranchTrueFalseTotal
unless $edited 0 0 0
136 0 ▶ return unless grep { $_->[1] } @tokens;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_->[1];} @tokens 0 0 0
137 0 return App::karr::Error::command_hint( map { $_->[0] } @tokens );
138 }
139  
140 # The word a command uses for an option's value in its own usage_string, and
141 # the option name upper-cased only where it names none.
142 #
143 # The commands write their placeholder down already -- Move's usage_string is
144 # `karr move ID[,ID,...] STATUS [--claim NAME] [--next|--prev]`, Handoff's names
145 # NAME and TEXT -- and karr's other k263 messages quote those same words back
146 # (`Status 'in-progress' requires a claim:` offers `--claim NAME`). Reading the
147 # string rather than inventing a second vocabulary is what keeps the two
148 # messages about one option saying one word, and it means a command that
149 # renames its placeholder drags this line along with it.
150 0 ▶sub _usage_placeholder {
Subroutine _usage_placeholder: not called CC=5 SCAR=34.0
151 0 my ( $self, $name ) = @_;
152  
153 0 ▶ my %config = $self->can('_options_config') ? $self->_options_config : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->can('_options_config') ? : 0 0 0
154 0 ▶ if ( defined $config{usage_string} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config{"usage_string"}) 0 0 0
155 # The usage_string spells an option the way a user types it, so the
156 # dashed spelling is tried as well as the underscored name Getopt::Long
157 # reports (#256). `=` is part of the placeholder, not a separator after
158 # it: `--board NAME=PATH` is one word.
159 0 ( my $dashed = $name ) =~ tr/_/-/;
160 0 for my $spelling ( $name, $dashed ) {
161 return $1
162 0 ▶ if $config{usage_string} =~ /--\Q$spelling\E[= ]([A-Z][A-Z0-9_=-]*)/;
Branch: 1 branch
BranchTrueFalseTotal
if $config{"usage_string"} =~ /--\Q$spelling\E[= ]([A-Z][A-Z0-9_=-]*)/ 0 0 0
163 }
164 }
165 0 return uc $name;
166 }
167  
168 # Where the caller's own spelling of an option stands, last occurrence first.
169 # The name in the diagnostic is the one in the generated specification --
170 # underscores, because MooX::Options folded the dashes away before Getopt::Long
171 # saw it (#256) -- so the typed token is folded the same way before comparing.
172 # An abbreviation is answered too: `--cla` is reported as `claim`, and appending
173 # a second, full --claim would put a word in the caller's mouth twice.
174 0 ▶sub _flag_position {
Subroutine _flag_position: not called CC=6 SCAR=37.4
175 0 my ( $tokens, $name ) = @_;
176 0 my $abbreviated;
177 0 for my $at ( reverse 0 .. $#$tokens ) {
178 0 ▶ next unless $tokens->[$at][0] =~ /\A--?([^=]+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $tokens->[$at][0] =~ /\A--?([^=]+)/ 0 0 0
179 0 ( my $typed = $1 ) =~ tr/-/_/;
180 0 ▶ return $at if $typed eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $typed eq $name 0 0 0
181 0 ▶ $abbreviated = $at if !defined $abbreviated && $name =~ /\A\Q$typed\E/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $abbreviated and $name =~ /\A\Q$typed\E/ 0 0 0
Condition0%0/3
not defined $abbreviated and $name =~ /\A\Q$typed\E/
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $abbreviated and $name =~ /\A\Q$typed\E/
not defined $abbreviated $name =~ /\A\Q$typed\E/
Truth table0%0/3
not(A) and B
A= not defined $abbreviatedB= $name =~ /\A\Q$typed\E/
ABresult
0X0
100
111
182 }
183 0 return $abbreviated;
184 }
185  
186 # Take the buffered diagnostic and put it back after $usage_text, with the
187 # suggestion under it, then exit. Returns 0 without exiting when there is
188 # nothing to reorder -- no diagnostic, no usage text, or a code that is not an
189 # error -- having printed whatever was buffered unchanged, so that every caller
190 # can put this in front of the printing it already did.
191 0 ▶sub _usage_error_last {
Subroutine _usage_error_last: not called CC=9 SCAR=45.0
192 0 my ( $self, $usage_text, $code ) = @_;
193  
194 0 my $diagnostic = _end_option_capture();
195 0 ▶ $diagnostic = '' unless defined $diagnostic;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $diagnostic 0 0 0
196  
197 0 ▶ unless ( length $diagnostic && defined $usage_text && defined $code && $code > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $diagnostic and defined $usage_text and defined $code and $code > 0) 0 0 0
Condition0%0/9
length $diagnostic and defined $usage_text and defined $code and $code > 0
!ll&&!rl&&r
000
length $diagnostic and defined $usage_text and defined $code
!ll&&!rl&&r
000
length $diagnostic and defined $usage_text
!ll&&!rl&&r
000
MC/DC0%0/4
length $diagnostic and defined $usage_text and defined $code and $code > 0
length $diagnostic defined $usage_text defined $code $code > 0
Truth table0%0/5
A and B and C and D
A= length $diagnosticB= defined $usage_textC= defined $codeD= $code > 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
198 0 ▶ print STDERR $diagnostic if length $diagnostic;
Branch: 1 branch
BranchTrueFalseTotal
if length $diagnostic 0 0 0
199 0 return 0;
200 }
201  
202 # One newline between the block and the answer. MooX::Options warns the
203 # usage object plus a newline of its own, which leaves a blank line -- and a
204 # blank line is one of the three an agent reading `tail -3` has.
205 0 $usage_text =~ s/\n*\z/\n/;
206 0 $diagnostic =~ s/\n+\z//;
207  
208 0 my $hint = $self->_option_hint($diagnostic);
209 0 ▶ $diagnostic .= ':' if defined $hint;
Branch: 1 branch
BranchTrueFalseTotal
if defined $hint 0 0 0
210  
211 0 print STDERR $usage_text, $diagnostic, "\n";
212 0 ▶ print STDERR $hint, "\n" if defined $hint;
Branch: 1 branch
BranchTrueFalseTotal
if defined $hint 0 0 0
213 0 exit $code;
214 }
215  
216 0 ▶around new_with_options => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
217 0 my ( $orig, $class, @args ) = @_;
218  
219 0 _start_option_capture();
220 0 my @created;
221 0 my $ok = eval { @created = $orig->( $class, @args ); 1 };
222 0 my $err = $@;
223  
224 # Only reached when MooX::Options neither exited nor took over: the command
225 # object was built, or the constructor died of something that is not an
226 # option-parse failure. Either way the buffer goes out as it was written.
227 0 my $left = _end_option_capture();
228 0 ▶ print STDERR $left if defined $left && length $left;
Branch: 1 branch
BranchTrueFalseTotal
if defined $left and length $left 0 0 0
Condition0%0/3
defined $left and length $left
!ll&&!rl&&r
000
MC/DC0%0/2
defined $left and length $left
defined $left length $left
Truth table0%0/3
A and B
A= defined $leftB= length $left
ABresult
0X0
100
111
229  
230 0 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
231 0 ▶ return wantarray ? @created : $created[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
232 12 };
233  
234 # The per-command help block (k276).
235 #
236 # MooX::Options' own text() rendered underscored names, listed the four
237 # built-ins (--usage/-h/--help/--man), and tacked on a blank-line divider the
238 # USAGE line on the previous page contradicted. Building it ourselves off
239 # _options_data (which carries none of the built-ins) lets us name options the
240 # way the command line spells them, drop the four built-ins for the same
241 # reason, and drop any option a command marks `hidden => 1` -- --dir lives on
242 # every board command and --quiet on every syncing one, so hiding them on
243 # per-command pages is what lets a page list only what differs between
244 # commands.
245 0 ▶sub _render_help_long {
Subroutine _render_help_long: not called CC=19 SCAR=59.4
246 0 my ($self) = @_;
247  
248 0 my %data = $self->_options_data;
249 0 my %config = $self->_options_config;
250  
251 # Width budget. The terminal reports columns through a layer of env vars
252 # and tty detection, all of which are unreliable in the test runner and
253 # under the agent harness; default to 76 columns and let a TEST_FORCE_*
254 # override pin it (the help-column-alignment test uses one).
255 my $cols = $ENV{TEST_FORCE_COLUMN_SIZE}
256 0 ▶ || ( eval {
Condition0%0/5
$ENV{'TEST_FORCE_COLUMN_SIZE'} || eval { do { require Term::Size::Any; Term::Size::Any::chars() or 0 } } || 76
l!l
00
$ENV{"TEST_FORCE_COLUMN_SIZE"} or eval { do { require Term::Size::Any; Term::Size::Any::chars() or 0 } }
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'TEST_FORCE_COLUMN_SIZE'} || eval { do { require Term::Size::Any; Term::Size::Any::chars() or 0 } } || 76
$ENV{"TEST_FORCE_COLUMN_SIZE"} eval { do { require Term::Size::Any; Term::Size::Any::chars() or 0 } }
Truth table0%0/3
A or B
A= $ENV{"TEST_FORCE_COLUMN_SIZE"}B= eval { do { require Term::Size::Any; Term::Size::Any::chars() or 0 } }
ABresult
1X1
011
000
257 0 require Term::Size::Any;
258 0 ▶ Term::Size::Any::chars() || 0;
Condition0%0/2
Term::Size::Any::chars() || 0
l!l
00
MC/DC0%0/1
Term::Size::Any::chars() || 0
Term::Size::Any::chars()
Truth table0%0/2
A
A= Term::Size::Any::chars()
Aresult
11
00
259 } ) || 76;
260 0 ▶ $cols = 76 if !defined $cols || $cols < 40;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cols or $cols < 40 0 0 0
Condition0%0/3
not defined $cols or $cols < 40
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $cols or $cols < 40
not defined $cols $cols < 40
Truth table0%0/3
not(A) or B
A= not defined $colsB= $cols < 40
ABresult
1X1
011
000
261  
262 # The USAGE line is the command's own usage_string, which already starts
263 # with `USAGE: karr <cmd> ...` and may itself spell options out. Two cases:
264 # one where it does, where the renderer keeps the command's wording
265 # verbatim and just adds the option list underneath; one where it does not
266 # and we are the first place the option list appears at all. Both are
267 # correct.
268 0 ▶ my $text = ( defined $config{usage_string} ? $config{usage_string} : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $config{'usage_string'} ? : 0 0 0
269 0 ▶ $text .= "\n" if length $text && substr( $text, -1 ) ne "\n";
Branch: 1 branch
BranchTrueFalseTotal
if length $text and substr($text, -1) ne "\n" 0 0 0
Condition0%0/3
length $text and substr($text, -1) ne "\n"
!ll&&!rl&&r
000
MC/DC0%0/2
length $text and substr($text, -1) ne "\n"
length $text substr($text, -1) ne "\n"
Truth table0%0/3
A and B
A= length $textB= substr($text, -1) ne "\n"
ABresult
0X0
100
111
270  
271 0 my @rows;
272 0 for my $name ( sort keys %data ) {
273 0 my $opt = $data{$name};
274 0 ▶ next if $opt->{hidden};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"hidden"} 0 0 0
275 push @rows, [
276 $self->_option_spec_for_help( $name, $opt ),
277 0 ▶ $opt->{doc} // '',
Condition0%0/2
$opt->{"doc"} // ""
l!l
00
MC/DC0%0/1
$opt->{"doc"} // ""
$opt->{"doc"}
Truth table0%0/2
A
A= $opt->{"doc"}
Aresult
11
00
278 ];
279 }
280  
281 # Column width: longest option spec in the table, with at least four
282 # spaces between the spec and the description. The 4 leading spaces are a
283 # deliberate indent -- matching the one --help already used -- so the help
284 # block and the USAGE line share their left margin.
285 0 my $max_spec = 0;
286 $max_spec = length $_->[0] > $max_spec ? length $_->[0] : $max_spec
287 0 ▶ for @rows;
Branch: 1 branch
BranchTrueFalseTotal
length $_->[0] > $max_spec ? : 0 0 0
288 0 ▶ $max_spec = 0 if !defined $max_spec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max_spec 0 0 0
289  
290 0 my $pad_indent = ' ' x 4;
291 0 my $gutter = $max_spec + 4;
292 0 my $doc_width = $cols - length($pad_indent) - $gutter;
293 0 ▶ $doc_width = 20 if $doc_width < 20;
Branch: 1 branch
BranchTrueFalseTotal
if $doc_width < 20 0 0 0
294  
295 0 for my $row (@rows) {
296 0 my ( $spec, $doc ) = @$row;
297 0 my $between = ' ' x ( $gutter - length $spec );
298 0 my @wrapped = $self->_wrap_help_doc( $doc, $doc_width );
299 0 $text .= $pad_indent . $spec . $between . $wrapped[0] . "\n";
300 0 ▶ if ( @wrapped > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@wrapped > 1) 0 0 0
301 0 my $cont = $pad_indent . ' ' x $gutter;
302 0 $text .= $cont . $_ . "\n" for @wrapped[ 1 .. $#wrapped ];
303 }
304 }
305 0 return $text;
306 }
307  
308 # The one-line spelling of an option for the help table.
309 #
310 # MooX::Options' Usage object builds this same string from the Getopt::Long
311 # spec it hands describe_options; the difference here is that we have the
312 # raw option declaration in hand (the underscored name, the short alias, the
313 # type), so we can hyphenate without losing the case and we can keep the
314 # `=String`/`=Int` suffix that matches what Getopt::Long prints on the
315 # option-parse error line -- a `--claimed_by=String` in the help and a
316 # `--claimed_by requires an argument` in the error are two forms of the same
317 # word, and the help's spelling is the one the user has to fix.
318 0 ▶sub _option_spec_for_help {
Subroutine _option_spec_for_help: not called CC=6 SCAR=37.4
319 0 my ( undef, $name, $opt ) = @_;
320  
321 0 my $display = $name;
322 0 $display =~ tr/_/-/;
323  
324 0 my $spec = '--' . $display;
325 0 ▶ if ( defined $opt->{short} && length $opt->{short} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->{"short"} and length $opt->{"short"}) 0 0 0
Condition0%0/3
defined $opt->{"short"} and length $opt->{"short"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opt->{"short"} and length $opt->{"short"}
defined $opt->{"short"} length $opt->{"short"}
Truth table0%0/3
A and B
A= defined $opt->{"short"}B= length $opt->{"short"}
ABresult
0X0
100
111
326 0 $spec = '-' . $opt->{short} . ' ' . $spec;
327 }
328 0 ▶ if ( defined $opt->{format} && length $opt->{format} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->{"format"} and length $opt->{"format"}) 0 0 0
Condition0%0/3
defined $opt->{"format"} and length $opt->{"format"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opt->{"format"} and length $opt->{"format"}
defined $opt->{"format"} length $opt->{"format"}
Truth table0%0/3
A and B
A= defined $opt->{"format"}B= length $opt->{"format"}
ABresult
0X0
100
111
329 0 my $fmt = $opt->{format};
330 0 $fmt =~ s/@\z//;
331 0 my %type = (
332 s => 'String',
333 i => 'Int',
334 o => 'Ext. Int',
335 f => 'Real',
336 );
337 0 ▶ $spec .= '=' . $type{$fmt} if defined $type{$fmt};
Branch: 1 branch
BranchTrueFalseTotal
if defined $type{$fmt} 0 0 0
338 }
339 0 return $spec;
340 }
341  
342 # Wrap $text to fit $width columns, splitting on whitespace. The input is one
343 # paragraph; the output is one or more lines of equal-or-less width, the
344 # caller is in charge of indenting. A word longer than the width is kept whole
345 # rather than split mid-word -- the help block is short, the descriptions
346 # short, and a word like `--claude-skill` would only ever break the layout
347 # for a few columns.
348 0 ▶sub _wrap_help_doc {
Subroutine _wrap_help_doc: not called CC=9 SCAR=45.0
349 0 my ( undef, $text, $width ) = @_;
350  
351 0 ▶ return ('') unless defined $text && length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 0 0 0
Condition0%0/3
defined $text and length $text
!ll&&!rl&&r
000
MC/DC0%0/2
defined $text and length $text
defined $text length $text
Truth table0%0/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
352 0 ▶ return ($text) if length $text <= $width;
Branch: 1 branch
BranchTrueFalseTotal
if length $text <= $width 0 0 0
353  
354 0 my @lines;
355 0 my $current = '';
356 0 for my $word ( split /\s+/, $text ) {
357 0 ▶ next unless length $word;
Branch: 1 branch
BranchTrueFalseTotal
unless length $word 0 0 0
358 0 ▶ if ( length $current == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $current == 0) { } 0 0 0
elsif (length($current) + 1 + length($word) <= $width) { } 0 0 0
359 0 $current = $word;
360 }
361 elsif ( length($current) + 1 + length($word) <= $width ) {
362 0 $current .= ' ' . $word;
363 }
364 else {
365 0 push @lines, $current;
366 0 $current = $word;
367 }
368 }
369 0 ▶ push @lines, $current if length $current;
Branch: 1 branch
BranchTrueFalseTotal
if length $current 0 0 0
370 0 return @lines;
371 }
372  
373 0 ▶around options_usage => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
374 0 my ( $orig, $self, $code, @rest ) = @_;
375 0 ▶ $code = 2 if defined $code && $code > 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $code and $code > 0 0 0 0
Condition0%0/3
defined $code and $code > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $code and $code > 0
defined $code $code > 0
Truth table0%0/3
A and B
A= defined $codeB= $code > 0
ABresult
0X0
100
111
376 0 ▶ $code //= 0;
Condition0%0/2
$code //= 0
l!l
00
MC/DC0%0/1
$code //= 0
$code
Truth table0%0/2
A
A= $code
Aresult
11
00
377  
378 # Anything else -- extra messages to print in front, no usage object at all
379 # -- is none of this role's business and goes to the original untouched
380 # (4.103, options_usage line 456 stringifies the Usage object as the whole
381 # message).
382 0 ▶ unless ( @rest == 1 && ref $rest[0] eq 'MooX::Options::Descriptive::Usage' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@rest == 1 and ref $rest[0] eq "MooX::Options::Descriptive::Usage") 0 0 0
Condition0%0/3
@rest == 1 and ref $rest[0] eq "MooX::Options::Descriptive::Usage"
!ll&&!rl&&r
000
MC/DC0%0/2
@rest == 1 and ref $rest[0] eq "MooX::Options::Descriptive::Usage"
@rest == 1 ref $rest[0] eq "MooX::Options::Descriptive::Usage"
Truth table0%0/3
A and B
A= @rest == 1B= ref $rest[0] eq "MooX::Options::Descriptive::Usage"
ABresult
0X0
100
111
383 0 return $orig->( $self, $code, @rest );
384 }
385  
386 # Build karr's own help block: hyphenated names, one option per line, the
387 # built-in --usage/-h/--help/--man and any `hidden => 1` option (--dir,
388 # --quiet) suppressed. MooX::Options' own long format spelled every option
389 # with underscores, listed the four built-ins, and inserted two blank lines
390 # before them, all of which contradicted the README and the USAGE line a
391 # screen apart (ticket k276).
392 0 my $text = $self->_render_help_long;
393  
394 # k263's reorder goes first: on the error path the buffered diagnostic is
395 # moved after $text and the process exits; on the success path the capture
396 # is ended and whatever was buffered is printed verbatim, and we then print
397 # $text below. STDERR is the real handle at this point either way.
398 0 my $done = $self->_usage_error_last( $text, $code );
399 0 ▶ return if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 0 0 0
400  
401 0 ▶ if ( $code > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code > 0) { } 0 0 0
402 0 CORE::warn $text;
403 }
404 else {
405 0 print $text;
406 }
407 0 ▶ exit $code if $code >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $code >= 0 0 0 0
408 0 return;
409 12 };
410  
411 # options_help is the method MooX::Options calls for --help (options_usage is
412 # what -h reaches). The two were two different renderings of the same options
413 # table -- long form for --help, compact for -h -- and the ticket asked for one
414 # rendering, the compact one, on both (#k276). options_short_usage (--usage)
415 # stays untouched: that one prints a one-line summary that has never been the
416 # "help" page.
417 0 ▶around options_help => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
418 0 my ( $orig, $self, $code, @rest ) = @_;
419 0 ▶ $code = 2 if defined $code && $code > 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $code and $code > 0 0 0 0
Condition0%0/3
defined $code and $code > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $code and $code > 0
defined $code $code > 0
Truth table0%0/3
A and B
A= defined $codeB= $code > 0
ABresult
0X0
100
111
420 0 ▶ $code //= 0;
Condition0%0/2
$code //= 0
l!l
00
MC/DC0%0/1
$code //= 0
$code
Truth table0%0/2
A
A= $code
Aresult
11
00
421  
422 0 ▶ unless ( @rest == 1 && ref $rest[0] eq 'MooX::Options::Descriptive::Usage' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@rest == 1 and ref $rest[0] eq "MooX::Options::Descriptive::Usage") 0 0 0
Condition0%0/3
@rest == 1 and ref $rest[0] eq "MooX::Options::Descriptive::Usage"
!ll&&!rl&&r
000
MC/DC0%0/2
@rest == 1 and ref $rest[0] eq "MooX::Options::Descriptive::Usage"
@rest == 1 ref $rest[0] eq "MooX::Options::Descriptive::Usage"
Truth table0%0/3
A and B
A= @rest == 1B= ref $rest[0] eq "MooX::Options::Descriptive::Usage"
ABresult
0X0
100
111
423 0 return $orig->( $self, $code, @rest );
424 }
425  
426 0 my $text = $self->_render_help_long;
427 0 my $done = $self->_usage_error_last( $text, $code );
428 0 ▶ return if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 0 0 0
429  
430 0 ▶ if ( $code > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code > 0) { } 0 0 0
431 0 CORE::warn $text;
432 }
433 else {
434 0 print $text;
435 }
436 0 ▶ exit $code if $code >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $code >= 0 0 0 0
437 0 return;
438 12 };
439  
440 12 1;
441  
442 __END__
« blib/lib/App/karr/Cmd/Dashboard.pm ↑ Summary blib/lib/App/karr/Cmd/Sync.pm »