blib/lib/Getopt/AsDocumented.pm

filter: Statementstmt 74.7% 151 / 202 Branchbran 61.8% 73 / 118 Conditioncond 17.8% 5 / 28 MC/DCmcdc 6.2% 1 / 16 Subroutinesub 72.7% 16 / 22 Podpod 100% 8 / 8 totaltotal 64.4% 254 / 394 CC 74 SCAR 29.2
CC
74
Cov
66%
command
38.6
version_handler
30.0
textblock
14.5
CRAP 18.5
↑ Summary
1 package Getopt::AsDocumented;
2 2 $VERSION = v0.0.2;
3  
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use base 'Getopt::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Getopt::AsDocumented - declare options as pod documentation
13  
14 =head1 SYNOPSIS
15  
16 =head1 Options
17 ...
18 =item -w, --what WHAT
19  
20 =cut
21  
22 sub main {
23 my (@args) = @_;
24 my $opt = Getopt::AsDocumented->process(\@args) or return;
25  
26 my $what = $opt->what;
27 ...
28 }
29  
30 =head1 About
31  
32 This module allows you to declare your program's command-line options as
33 pod documentation. It provides syntax to declare types and defaults in
34 a way which is also readable as documentation.
35  
36 Note: This is built on Getopt::Base and some advanced features are still
37 growing. Your help is welcome.
38  
39 =head1 Methods
40  
41 =head2 process
42  
43 Loads the pod from your current file and processes the command-line
44 arguments.
45  
46 my $opt = Getopt::AsDocumented->process(\@args) or return;
47  
48 =cut
49  
50 1 ▶sub process {
Subroutine process: called CC=3 SCAR=11.2
Pod documented
51 1 my $self = shift;
52 1 my $args = shift;
53 1 ▶ (@_ % 2) and croak("odd number of elements in \%settings");
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 and croak("odd number of elements in %settings") 0 1 1
54 1 my %also = @_;
55 1 ▶ $self = $self->new(%also) unless(ref $self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 1 1
56  
57 1 return $self->SUPER::process($args);
58 } # process ############################################################
59  
60 =head1 Option Specification
61  
62 =head2 With/Without Opterands
63  
64 If an option is followed by a word, it requires an opterand.
65  
66 =item --foo FOO
67  
68 Otherwise, it is a simple flag (boolean) option
69  
70 =item --foo
71  
72 =head3 Booleans
73  
74 Each boolean option will automatically generate a '--no-' form which
75 negates it. You may choose to mention this and/or link aliases to it.
76  
77 =item --foo
78  
79 Sets the fooness.
80  
81 =item -x, --ex-foo, --no-foo
82  
83 =head2 Types
84  
85 Non-boolean options may be typed as strings, numbers, or integers. The
86 type is included in parenthesis after the option spec. If the type is not mentioned, it defaults to C<string>.
87  
88 =head3 integer
89  
90 An integer.
91  
92 =item --foo FOO (integer)
93  
94 =head3 number
95  
96 A floating-point number.
97  
98 =item --foo FOO (number)
99  
100 =head3 string
101  
102 A string. This is the default, but may be included for clarity.
103  
104 =item --foo FOO (string)
105  
106 =head2 Scalar/HASH/ARRAY
107  
108 Any non-boolean option can take one of the following forms.
109  
110 =head3 Scalar
111  
112 An option followed by a simple word means that only one value is
113 assigned to it (if the user repeats it, a prior value is overwritten.)
114  
115 =item --foo FOO
116  
117 =head3 HASH
118  
119 A HASH option is followed by something of the form C<\w+=.*>. Each
120 opterand is treated as a $key=$value pair.
121  
122 =item --foo BAR=BAZ
123  
124 =head3 LIST
125  
126 A LIST option is followed by another mention of itself within []
127 brackets with an ellipsis to indicate optional additional elements.
128  
129 =item --foo FOO [--foo ...]
130  
131 If a list option requires an explicit type, this must be included after
132 the bracketed text.
133  
134 =item --foo FOO [--foo ...] (integer)
135  
136 =head2 Defaults
137  
138 An option's default may be set by the string "DEFAULT: " at the
139 beginning of a paragraph. The remainder of that paragraph contains the
140 default value.
141  
142 =item --foo FOO
143  
144 The setting for foo.
145  
146 DEFAULT: bar
147  
148 Any leading whitespace after the ':' is removed.
149  
150 A single leading backslash (if present) will be removed and the rest of
151 the string will be treated as a literal.
152  
153 A boolean default may be "NO". Without a value, a boolean will default
154 to undef. Anything true will be translated to '1'.
155  
156 =item --foo
157  
158 Whether to foo or not.
159  
160 DEFAULT: yes. Use --no-foo to disable this.
161  
162 The strings "no" or "false" may also be used as "0".
163  
164 If the default is enclosed with braces ({}), it is interpreted as a
165 block of code. For literal braces, use a leading backslash.
166  
167 =item --input FILENAME
168  
169 Input file.
170  
171 DEFAULT: {File::Fu->home + 'input.txt'}
172  
173 =head1 Handlers
174  
175 =head2 config_file_handler
176  
177 Loads the user's configuration file. All of the values from the
178 configuration will be loaded into the options object I<before> any
179 options from the command-line are processed.
180  
181 $go->config_file_handler;
182  
183 =cut
184  
185 0 ▶sub config_file_handler {
Subroutine config_file_handler: not called CC=1 SCAR=6.9
Pod documented
186 0 my $self = shift;
187 0 my ($file) = shift;
188 0 $self->load_config_file($file);
189 } # config_file_handler ################################################
190  
191 =head2 load_config_file
192  
193 $self->load_config_file($file);
194  
195 =cut
196  
197 1 ▶sub load_config_file {
Subroutine load_config_file: called CC=2 SCAR=7.0
Pod documented
198 1 my $self = shift;
199 1 my ($file) = @_;
200  
201 1 ▶ my $mod = sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
202 1 foreach my $m (qw(YAML::XS YAML::Syck YAML)) {
203 3 ▶ eval("require $m") and return($m);
Branch: 1 branch
BranchTrueFalseTotal
eval "require $m" and return $m 1 2 3
204 }
205 0 croak("cannot load any yaml module $@");
206 1 }->();
207 1 ▶ croak("what?") unless($mod);
Branch: 1 branch
BranchTrueFalseTotal
unless $mod 0 1 1
208  
209 1 my $loader = $mod->can('LoadFile');
210  
211 1 my ($data) = $loader->($file);
212 1 $self->set_values(%$data);
213 } # load_config_file ###################################################
214  
215 =head2 make_object
216  
217 Wraps the super method in order to load the config file.
218  
219 $obj = $self->make_object;
220  
221 =cut
222  
223 1 ▶sub make_object {
Subroutine make_object: called CC=6 SCAR=22.5
Pod documented
224 1 my $self = shift;
225 1 my $obj = $self->SUPER::make_object(@_);
226  
227 1 ▶ if(my $do = $obj->can('config_file')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $do = $obj->can("config_file")) 1 0 1
228 # XXX this is so wrong
229 0 my %defaults = map({@$_} @{$self->{_defaults}});
230 1 my $lazy = $defaults{config_file};
231  
232 1 ▶ if(my $file = $do->($obj) ||
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = &$do($obj) || $lazy && do { $obj->{"config_file"} = &$lazy() }) 1 0 1
Condition16%1/6
&$do($obj) || $lazy && do { $obj->{'config_file'} = &$lazy() }
l!l&&r!l&&!r
100
$lazy and do { $obj->{"config_file"} = &$lazy() }
!ll&&!rl&&r
000
MC/DC0%0/3
&$do($obj) || $lazy && do { $obj->{'config_file'} = &$lazy() }
&$do($obj) $lazy do { $obj->{"config_file"} = &$lazy() }
Truth table25%1/4
A || B and C
A= &$do($obj)B= $lazyC= do { $obj->{"config_file"} = &$lazy() }
ABCresult
1XX1
0111
00X0
0100
233 0 $lazy && do {$obj->{config_file} = $lazy->()}
234 ) {
235 1 local $self->{object} = $obj; # must have a context
236 1 ▶ $self->load_config_file($file) if(-e $file);
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 1 0 1
237 }
238 }
239  
240 0 return($obj);
241 } # make_object ########################################################
242  
243  
244 =head2 handler
245  
246 Accessor.
247  
248 my $handler = $go->handler;
249  
250 =cut
251  
252 0 ▶sub handler { shift->{handler} }
Subroutine handler: not called CC=1 SCAR=6.9
Pod documented
253  
254 =head2 version_handler
255  
256 Prints the version from your handler/caller()'s package.
257  
258 $go->version_handler;
259  
260 Sets the quit flag.
261  
262 =cut
263  
264 0 ▶sub version_handler {
Subroutine version_handler: not called CC=5 SCAR=34.0
Pod documented
265 0 my $self = shift;
266  
267 0 my $caller = $self->{handler};
268 0 ▶ $caller = ref($caller) || $caller;
Condition0%0/3
ref $caller || $caller
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $caller || $caller
ref $caller $caller
Truth table0%0/3
A || B
A= ref $callerB= $caller
ABresult
1X1
011
000
269 0 eval {require version}; # for stringy VERSION() support (I hope)
270 0 ▶ my $v = $caller->VERSION || main->VERSION || '<undefined>';
Condition0%0/5
$caller->VERSION || 'main'->VERSION || '<undefined>'
l!l
00
$caller->VERSION or "main"->VERSION
l!l&&r!l&&!r
000
MC/DC0%0/2
$caller->VERSION || 'main'->VERSION || '<undefined>'
$caller->VERSION "main"->VERSION
Truth table0%0/3
A or B
A= $caller->VERSIONB= "main"->VERSION
ABresult
1X1
011
000
271 0 ▶ my $name = $self->{program_name} || do {
Condition0%0/3
$self->{'program_name'} || do { require File::Basename; File::Basename::basename($0) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'program_name'} || do { require File::Basename; File::Basename::basename($0) }
$self->{'program_name'} do { require File::Basename; File::Basename::basename($0) }
Truth table0%0/3
A || B
A= $self->{'program_name'}B= do { require File::Basename; File::Basename::basename($0) }
ABresult
1X1
011
000
272 0 require File::Basename;
273 0 File::Basename::basename($0)
274 };
275 0 print "$name version $v\n";
276 0 $self->quit;
277 } # version_handler ####################################################
278  
279 =head2 help_handler
280  
281 Prints a help message based on the USAGE and OPTIONS sections from your
282 pod. Uses the first sentence from each C<=item> section, or
283 alternatively: C<=for help> content found within the C<=item> section.
284  
285 $go->help_handler;
286  
287 Sets the quit flag.
288  
289 =cut
290  
291 0 ▶sub help_handler {
Subroutine help_handler: not called CC=3 SCAR=24.8
Pod documented
292 0 my $self = shift;
293 0 print "Usage:\n", $self->{usage}, "\n\n";
294  
295 my @options = map({
296 0 my $d = $self->{opt_data}{$_};
297 0 my $type = $d->{type};
298 [
299 $self->{help_bits}{$_} .
300 (($type ne 'boolean' and $type ne 'string') ?
301 (' (' . substr($type, 0, 3) . ')') : '')
302 ,
303 0 ▶ $self->{help}{$_}
Branch: 1 branch
BranchTrueFalseTotal
$type ne 'boolean' && $type ne 'string' ? : 0 0 0
Condition0%0/3
$type ne "boolean" and $type ne "string"
!ll&&!rl&&r
000
MC/DC0%0/2
$type ne "boolean" and $type ne "string"
$type ne "boolean" $type ne "string"
Truth table0%0/3
A and B
A= $type ne "boolean"B= $type ne "string"
ABresult
0X0
100
111
304 ]
305 0 } @{$self->{help_order}});
306 0 my ($longest) = sort({$b <=> $a} map({length($_->[0])} @options));
307 0 @options = map({sprintf('%-'.$longest."s %s", @$_)} @options);
308 0 print join("\n ",
309 "Options:", @options
310 ), "\n";
311  
312 0 $self->quit;
313 } # help_handler #######################################################
314  
315 =head1 Other Methods
316  
317 =head2 new
318  
319 my $go = Getopt::AsDocumented->new(%settings);
320  
321 =over
322  
323 =item pod => $string
324  
325 =item from_file => $filename
326  
327 =item handler => $classname
328  
329 =back
330  
331 =cut
332  
333 1 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
Pod documented
334 1 my $class = shift;
335 1 ▶ (@_ % 2) and croak("odd number of elements in \%settings");
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 and croak("odd number of elements in %settings") 0 1 1
336 1 my %setup = @_;
337  
338 1 my %pass;
339 1 foreach my $key (qw(arg_handler)) {
340 1 ▶ $pass{$key} = delete($setup{$key}) if(exists($setup{$key}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $setup{$key} 0 1 1
341 }
342  
343 1 my $self = $class->SUPER::new(%pass);
344  
345 1 $self->_init(%setup);
346  
347 1 return($self);
348 } # new ################################################################
349  
350  
351 =for internal
352 =head2 _init
353  
354 $self->_init(%setup);
355  
356 =cut
357  
358 1 ▶sub _init {
Subroutine _init: called CC=6 SCAR=20.4
359 1 my $self = shift;
360 1 my %setup = @_;
361  
362 1 my $fh;
363 1 ▶ if(my $pod = $setup{pod}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pod = $setup{'pod'}) { } 0 1 1
364 0 ▶ open($fh, '<', \$pod) or croak("cannot open string $!");
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", \$pod or croak("cannot open string $!") 0 0 0
365 }
366 else {
367 1 ▶ my $file = $setup{from_file} || (caller(2))[1];
Condition33%1/3
$setup{'from_file'} || (caller 2)[1]
l!l&&r!l&&!r
100
MC/DC0%0/2
$setup{'from_file'} || (caller 2)[1]
$setup{'from_file'} (caller 2)[1]
Truth table33%1/3
A || B
A= $setup{'from_file'}B= (caller 2)[1]
ABresult
1X1
011
000
368 # TODO allow searching @INC?
369 1 ▶ open($fh, '<', $file) or croak("cannot open '$file' $!");
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or croak("cannot open '${file}' $!") 1 0 1
370 }
371  
372 # TODO check this against the =for getopt_handler ... case
373 1 ▶ $self->{handler} = $setup{handler} || (caller(2))[0];
Condition33%1/3
$setup{'handler'} || (caller 2)[0]
l!l&&r!l&&!r
001
MC/DC0%0/2
$setup{'handler'} || (caller 2)[0]
$setup{'handler'} (caller 2)[0]
Truth table33%1/3
A || B
A= $setup{'handler'}B= (caller 2)[0]
ABresult
1X1
011
000
374  
375 1 $self->{help_order} = [];
376  
377 1 my $parser = Getopt::AsDocumented::PodParser->new;
378 1 $parser->{__go} = $self;
379 1 $parser->{__the_fh} = $fh;
380 1 $parser->parse_from_filehandle($fh);
381 } # _init ##############################################################
382  
383 {
384 package Getopt::AsDocumented::PodParser;
385 2 ▶use base 'Pod::Parser';
Subroutine BEGIN: called CC=1 SCAR=0.0
386  
387 26 ▶sub command {
Subroutine command: called CC=29 SCAR=36.5
388 26 my ($self, $command, $p) = @_;
389  
390 26 $p =~ s/\n+$//;
391  
392 #warn "-- ", $p, "\n";
393 26 ▶ if($command =~ m/^head/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command =~ /^head/) 2 24 26
394 2 ▶ if($self->{__options}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'__options'}) { } 0 2 2
elsif ($p =~ /^options$/i) { } 1 1 2
elsif ($p =~ /^usage$/i) { } 1 0 1
395 # done
396 0 $self->__store_last;
397 0 return seek($self->{__the_fh}, 0, 2);
398 }
399 elsif($p =~ m/^options$/i) {
400 1 $self->{__options} = {};
401 }
402 elsif($p =~ m/^usage$/i) {
403 #warn "usage: $p";
404 1 $self->{__usage} = $p;
405 }
406 2 return;
407 }
408 # hmm, we also need to ditch any directives which aren't in the Usage
409 # or Options sections
410  
411 24 ▶ $self->{__options} or return; # not there yet
Branch: 1 branch
BranchTrueFalseTotal
$self->{"__options"} or return 24 0 24
412  
413 24 ▶ if($command eq 'item') {
Branch: 2 branches
BranchTrueFalseTotal
if ($command eq 'item') { } 14 10 24
elsif ($command eq 'back') { } 1 9 10
414 14 $self->__store_last;
415  
416 14 my %setup;
417  
418 my @opts;
419 14 ▶ while($p =~ s/^([^ ,]+)(,?)(?: |$)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p =~ s/^([^ ,]+)(,?)(?: |$)//) 26 0 26
420 26 ▶ push(@opts, $1); last unless($2);
Branch: 1 branch
BranchTrueFalseTotal
unless $2 14 12 26
421 }
422  
423 # number|integer|string type
424 14 ▶ if($p =~ s/ \(([^ ]+)\)$//) { $setup{type} = $1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($p =~ s/ \(([^ ]+)\)$//) 5 9 14
425  
426 14 ▶ $setup{help_bit} = join(', ', @opts) . ($p ? ' '.$p : '');
Branch: 1 branch
BranchTrueFalseTotal
$p ? : 9 5 14
427  
428 # list/hash form detection
429 14 ▶ if($p =~ s/ \[--[^ ]+ \.\.\.\]$//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p =~ s/ \[--[^ ]+ \.\.\.\]$//) { } 1 13 14
elsif ($p =~ /^\w+=/) { } 1 12 13
430 1 $setup{form} = 'ARRAY';
431 }
432 elsif($p =~ m/^\w+=/) {
433 1 $setup{form} = 'HASH';
434 }
435  
436 # warn " stuff($p)\n" if($p);
437 14 ▶ $setup{example} = $p if($p);
Branch: 1 branch
BranchTrueFalseTotal
if $p 9 5 14
438 14 ▶ if($p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p) { } 9 5 14
439 9 ▶ $setup{type} ||= 'string';
Condition100%2/2
$setup{"type"} ||= "string"
l!l
54
MC/DC100%1/1
$setup{"type"} ||= "string"
$setup{"type"}
Truth table100%2/2
A
A= $setup{"type"}
Aresult
11
00
440 }
441 else {
442 5 $setup{type} = 'boolean';
443 }
444  
445 # parse-out the various short and alias forms
446 # the last one is the canonical form
447 14 my @short;
448 my @long;
449 14 foreach my $opt (@opts) {
450 26 ▶ if($opt =~ s/^--//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt =~ s/^--//) { } 16 10 26
451 16 $opt =~ s/-/_/g;
452 16 push(@long, $opt);
453 }
454 else {
455 10 ▶ $opt =~ s/^-// or Carp::croak("'$opt' must have a leading dash");
Branch: 1 branch
BranchTrueFalseTotal
$opt =~ s/^-// or Carp::croak("'${opt}' must have a leading dash") 10 0 10
456 10 ▶ (length($opt) == 1) or Carp::croak("'$opt' malformed");
Branch: 1 branch
BranchTrueFalseTotal
length $opt == 1 or Carp::croak("'${opt}' malformed") 10 0 10
457 10 push(@short, $opt);
458 }
459 }
460  
461 14 my $canon = pop(@long);
462  
463 14 ▶ if($canon =~ m/^no_(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($canon =~ /^no_(.*)/) 1 13 14
464 1 my $what = $1;
465 1 $setup{opposes} = $what;
466 # implicit 'opposes' -- vs
467 # warn "$canon (@long)- opposes $what\n";
468 #$self->{__go}->add_aliases($canon, \@short, @long);
469 #return;
470 }
471 # warn "canon: $canon\n";
472 # warn "long: @long\n";
473 # warn "short: @short\n";
474 14 $setup{aliases} = \@long;
475 14 $setup{short} = \@short;
476  
477 14 $setup{canon} = $canon;
478 14 $self->{__current} = \%setup;
479 14 return;
480 }
481 elsif($command eq 'back') {
482 1 $self->__store_last;
483 1 return;
484 }
485  
486 9 ▶ if($command eq 'for') {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "for") 8 1 9
487 8 my ($t, @and) = split(/\n=for /, $p);
488  
489 8 my %for_items = map({$_ => 1} qw(positional help isa call opposes));
490 my %for_globals = (
491 0 ▶ handler => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
492 0 my $class = shift;
493 0 ▶ unless($class->can('VERSION')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can("VERSION")) 0 0 0
494 0 eval("require $class");
495 0 ▶ $@ and Carp::croak("cannot load your handler: $@");
Branch: 1 branch
BranchTrueFalseTotal
$@ and Carp::croak("cannot load your handler: $@") 0 0 0
496 }
497 0 $self->{__go}{handler} = $class;
498 },
499 0 ▶ program_name => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
500 0 $self->{__go}{program_name} = shift;
501 },
502 8 );
503  
504  
505 8 my ($thing, $val) = split(/ /, $t, 2);
506  
507 8 ▶ if($for_items{$thing}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($for_items{$thing}) { } 8 0 8
elsif (my $do = $for_globals{$thing}) { } 0 0 0
508 8 ▶ $self->{__current} or Carp::croak("'=for $thing' out of context");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"__current"} or Carp::croak("'=for ${thing}' out of context") 8 0 8
509 8 ▶ $self->{__current}{$thing} = defined($val) ? $val : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 6 2 8
510 }
511 elsif(my $do = $for_globals{$thing}) {
512 0 ▶ $self->{__current} and Carp::croak("'=for $thing' out of context");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"__current"} and Carp::croak("'=for ${thing}' out of context") 0 0 0
513 0 $do->($val);
514 }
515 0 else {warn "unhandled: $t\n"}
516  
517 8 $self->command('for' => $_) for(@and);
518 }
519 }
520 2 ▶sub verbatim {
Subroutine verbatim: called CC=2 SCAR=6.9
521 2 my ($parser, $t) = @_;
522 2 ▶ if(delete($parser->{__usage})) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $parser->{"__usage"}) 1 1 2
523 1 $t =~ s/\n+$//;
524 1 $parser->{__go}->{usage} = $t;
525 }
526 }
527  
528 1 ▶sub end_pod {
Subroutine end_pod: called CC=1 SCAR=0.0
529 1 shift->__store_last;
530 }
531 18 ▶sub textblock {
Subroutine textblock: called CC=11 SCAR=26.8
532 18 my ($self, $p) = @_;
533  
534 18 ▶ my $s = $self->{__current} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $s = $self->{"__current"} or return 18 0 18
535 18 ▶ if($p =~ m/^DEFAULT(?::|\s*=)\s*(.*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p =~ /^DEFAULT(?::|\s*=)\s*(.*)/) { } 4 14 18
elsif (not $s->{'help'}) { } 10 4 14
536 4 my $def = $1;
537  
538 4 ▶ if($def =~ s/^\\//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($def =~ s/^\\//) { } 0 4 4
elsif ($def =~ s/^\{//) { } 0 4 4
539 # everything after that is literal
540 }
541 elsif($def =~ s/^\{//) {
542 0 ▶ $def =~ s/\}$// or croak("DEFAULT must have closing brace");
Branch: 1 branch
BranchTrueFalseTotal
$def =~ s/\}$// or croak("DEFAULT must have closing brace") 0 0 0
543 0 my $sub = eval("sub { $def }");
544 0 ▶ $@ and Carp::croak("error $@\nin DEFAULT block '$def'");
Branch: 1 branch
BranchTrueFalseTotal
$@ and Carp::croak("error $@\nin DEFAULT block '${def}'") 0 0 0
545 0 $def = $sub;
546 }
547 else { # normalize it
548 4 ▶ if($def =~ s/^(["'])//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($def =~ s/^(["'])//) 1 3 4
549 1 $def =~ s/$1$//;
550 }
551 # warn "$s->{canon} $s->{type}\n";
552 4 ▶ if($s->{type} eq 'boolean') {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"type"} eq "boolean") 1 3 4
553 1 $def =~ s/^(no|false)$/0/i;
554 1 ▶ $def = 1 if($def);
Branch: 1 branch
BranchTrueFalseTotal
if $def 0 1 1
555 }
556 }
557  
558 4 $s->{default} = $def;
559 }
560 elsif(not $s->{help}) {
561 # make help from the first sentence
562 10 $p =~ s/\n+$//;
563 10 $p = lcfirst($p);
564 10 $p =~ s/\.(\)?)( *|$).*/$1/s;
565 # TODO some coverage of this - and what to do about parens?
566 #warn "text: $p\n";
567 10 $s->{help} = $p;
568 }
569 }
570  
571 16 ▶sub __store_last {
Subroutine __store_last: called CC=9 SCAR=23.6
572 16 my $parser = shift;
573  
574 16 ▶ my $setup = delete($parser->{__current}) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $setup = delete $parser->{"__current"} or return 14 2 16
575 14 ▶ my $name = delete($setup->{canon}) or die "nothing here";
Branch: 1 branch
BranchTrueFalseTotal
my $name = delete $setup->{"canon"} or die "nothing here" 14 0 14
576 14 my $pos = delete($setup->{positional});
577  
578 14 my $self = $parser->{__go};
579  
580 14 my %auto_actions = map({$_ => 1}
581 qw(help version config_file));
582 14 ▶ if(my $call = $setup->{call}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $call = $setup->{'call'}) { } 0 14 14
elsif ($auto_actions{$name}) { } 3 11 14
583 0 ▶ my $handler = $call =~ s/^(.*)::// ? $1 : $self->{handler};
Branch: 1 branch
BranchTrueFalseTotal
$call =~ s/^(.*)::// ? : 0 0 0
584 # TODO caller should be able to pass handler as an object?
585 0 ▶ $setup->{call} = $handler->can($call) or
Branch: 1 branch
BranchTrueFalseTotal
$setup->{"call"} = $handler->can($call) or Carp::croak("'${handler}' cannot '$call()'") 0 0 0
586 Carp::croak("'$handler' cannot '$call()'");
587 }
588 elsif($auto_actions{$name}) {
589 3 ▶ $setup->{call} = $self->can($name . '_handler') or
Branch: 1 branch
BranchTrueFalseTotal
$setup->{"call"} = $self->can($name . '_handler') or Carp::croak("no handler defined for $name") 3 0 3
590 Carp::croak("no handler defined for $name");
591 }
592  
593 14 push(@{$self->{help_order}}, $name);
594 # TODO ^-- does not work with =for opposes $something
595  
596 14 $self->{help_bits}{$name} = delete($setup->{help_bit});
597 14 $self->{help}{$name} = delete($setup->{help});
598 14 $self->add_option($name, %$setup);
599 14 ▶ $self->add_positionals($name) if($pos);
Branch: 1 branch
BranchTrueFalseTotal
if $pos 2 12 14
600 }
601  
602 }
603 ########################################################################
604  
605  
606  
607  
608  
609  
610 =head1 AUTHOR
611  
612 Eric Wilhelm @ <ewilhelm at cpan dot org>
613  
614 http://scratchcomputing.com/
615  
616 =head1 BUGS
617  
618 If you found this module on CPAN, please report any bugs or feature
619 requests through the web interface at L<http://rt.cpan.org>. I will be
620 notified, and then you'll automatically be notified of progress on your
621 bug as I make changes.
622  
623 If you pulled this development version from my /svn/, please contact me
624 directly.
625  
626 =head1 COPYRIGHT
627  
628 Copyright (C) 2009 Eric L. Wilhelm, All Rights Reserved.
629  
630 =head1 NO WARRANTY
631  
632 Absolutely, positively NO WARRANTY, neither express or implied, is
633 offered with this software. You use this software at your own risk. In
634 case of loss, no person or entity owes you anything whatsoever. You
635 have been warned.
636  
637 =head1 LICENSE
638  
639 This program is free software; you can redistribute it and/or modify it
640 under the same terms as Perl itself.
641  
642 =cut
643  
644 # vi:ts=2:sw=2:et:sta
645 2 1;
↑ Summary