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   212442 use strict;
  1         2  
  1         28  
3 1     1   9 use warnings;
  1         1  
  1         129  
4              
5             our $VERSION = '2.000007';
6              
7 1     1   5 use Carp qw/croak/;
  1         1  
  1         97  
8              
9 1     1   374 use Getopt::Yath::Util qw/mod2file/;
  1         4  
  1         38  
10              
11 1     1   617 use Getopt::Yath::Instance;
  1         4  
  1         40  
12 1     1   8 use Getopt::Yath::Option;
  1         2  
  1         512  
13              
14             sub import {
15 1     1   1197 my $class = shift;
16 1         2 my %params = @_;
17              
18 1         3 my $caller = caller();
19              
20 1   50     7 my $inst_class = $params{inst_class} // 'Getopt::Yath::Instance';
21              
22 1         6 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         3833  
28              
29             $export{option} = sub {
30 16     16   20109 my $title = shift;
31 16 50       65 my $option = Getopt::Yath::Option->create(trace => [caller()], title => $title, @common ? (%{$common[-1]}) : (), @_);
  16         116  
32 15         100 $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         4 };
48              
49             $export{option_post_process} = sub {
50 4     4   2438 my $cb = pop;
51 4   100     14 my $weight = shift // 0;
52 4         6 my ($applicable) = @_;
53              
54 4 50 33     17 $applicable //= $common[-1]->{applicable} if @common;
55              
56 4 50 33     15 croak "You must provide a callback coderef" unless $cb && ref($cb) eq 'CODE';
57              
58 4         19 $instance->_post([caller()], $weight, $applicable, $cb);
59 1         2 };
60              
61             $export{option_group} = sub {
62 2     2   78004 my ($set, $sub) = @_;
63              
64 2 100       10 my $common = {@common ? (%{$common[-1]}) : (), %$set};
  1         5  
65              
66 2 100       11 $common->{module} = caller unless $common->{no_module};
67              
68 2         4 push @common => $common;
69 2         4 my $ok = eval { $sub->(); 1 };
  2         6  
  2         2953  
70 2         7 my $err = $@;
71 2         4 pop @common;
72              
73 2 50       15 die $err unless $ok;
74 1         2 };
75              
76 1     64   3 $export{parse_options} = sub { $instance->process_args(@_) };
  64         6047  
77              
78 1     0   2 $export{category_sort_map} = sub { $instance->set_category_sort_map(@_) };
  0         0  
79              
80 1         3 for my $name (keys %export) {
81 1     1   7 no strict 'refs';
  1         2  
  1         171  
82             croak "$caller already has an '$name' method"
83 7 50       6 if defined(&{"${caller}\::${name}"});
  7         17  
84              
85 7         8 *{"${caller}\::${name}"} = $export{$name};
  7         20  
86             }
87              
88 1         103264 return 1;
89             }
90              
91             1;
92              
93             __END__