File Coverage

blib/lib/Getopt/Yath.pm
Criterion Covered Total %
statement 64 71 90.1
branch 10 22 45.4
condition 6 16 37.5
subroutine 13 15 86.6
pod n/a
total 93 124 75.0


line stmt bran cond sub pod time code
1             package Getopt::Yath;
2 1     1   198903 use strict;
  1         4  
  1         44  
3 1     1   15 use warnings;
  1         1  
  1         74  
4              
5             our $VERSION = '2.000008';
6              
7 1     1   6 use Carp qw/croak/;
  1         3  
  1         61  
8              
9 1     1   628 use Getopt::Yath::Util qw/mod2file/;
  1         3  
  1         6  
10              
11 1     1   509 use Getopt::Yath::Instance;
  1         2  
  1         30  
12 1     1   6 use Getopt::Yath::Option;
  1         1  
  1         401  
13              
14             sub import {
15 1     1   1000 my $class = shift;
16 1         3 my %params = @_;
17              
18 1         2 my $caller = caller();
19              
20 1   50     6 my $inst_class = $params{inst_class} // 'Getopt::Yath::Instance';
21              
22 1         4 my $instance = $inst_class->new(class => $class);
23 1 50 33     3 $instance->include($class->options) if $params{inherit} && $class->can('options');
24              
25 1         2 my %export;
26             my @common;
27 1     2   4 $export{options} = sub { $instance };
  2         3156  
28              
29             $export{option} = sub {
30 16     16   15144 my $title = shift;
31 16 50       64 my $option = Getopt::Yath::Option->create(trace => [caller()], title => $title, @common ? (%{$common[-1]}) : (), @_);
  16         84  
32 15         65 $instance->_option($option);
33 1         3 };
34              
35             $export{include_options} = sub {
36 0     0   0 while (my $module = shift @_) {
37 0         0 my $file = mod2file($module);
38 0 0       0 require $file unless $INC{$file};
39              
40 0 0       0 croak "Module '$module' does not have an 'options' method"
41             unless $module->can('options');
42              
43 0 0 0     0 my $list = @_ && ref($_[0]) eq 'ARRAY' ? shift(@_) : undef;
44              
45 0         0 $instance->include($module->options, $list);
46             }
47 1         2 };
48              
49             $export{option_post_process} = sub {
50 4     4   1757 my $cb = pop;
51 4   100     9 my $weight = shift // 0;
52 4         5 my ($applicable) = @_;
53              
54 4 50 33     15 $applicable //= $common[-1]->{applicable} if @common;
55              
56 4 50 33     12 croak "You must provide a callback coderef" unless $cb && ref($cb) eq 'CODE';
57              
58 4         13 $instance->_post([caller()], $weight, $applicable, $cb);
59 1         3 };
60              
61             $export{option_group} = sub {
62 2     2   63559 my ($set, $sub) = @_;
63              
64 2 100       8 my $common = {@common ? (%{$common[-1]}) : (), %$set};
  1         6  
65              
66 2 100       10 $common->{module} = caller unless $common->{no_module};
67              
68 2         4 push @common => $common;
69 2         3 my $ok = eval { $sub->(); 1 };
  2         6  
  2         1832  
70 2         5 my $err = $@;
71 2         3 pop @common;
72              
73 2 50       9 die $err unless $ok;
74 1         2 };
75              
76 1     64   2 $export{parse_options} = sub { $instance->process_args(@_) };
  64         4204  
77              
78 1     0   3 $export{category_sort_map} = sub { $instance->set_category_sort_map(@_) };
  0         0  
79              
80 1         2 for my $name (keys %export) {
81 1     1   5 no strict 'refs';
  1         1  
  1         126  
82             croak "$caller already has an '$name' method"
83 7 50       7 if defined(&{"${caller}\::${name}"});
  7         18  
84              
85 7         6 *{"${caller}\::${name}"} = $export{$name};
  7         18  
86             }
87              
88 1         84536 return 1;
89             }
90              
91             1;
92              
93             __END__