File Coverage

blib/lib/Rope/Cmd.pm
Criterion Covered Total %
statement 79 83 95.1
branch 10 12 83.3
condition 3 6 50.0
subroutine 18 20 90.0
pod 0 6 0.0
total 110 127 86.6


line stmt bran cond sub pod time code
1             package Rope::Cmd;
2              
3 2     2   234124 use 5.006;
  2         10  
4 2     2   11 use strict;
  2         12  
  2         90  
5 2     2   12 use warnings;
  2         5  
  2         149  
6 2     2   1372 use Term::ANSI::Sprintf qw/sprintf/;
  2         49112  
  2         21  
7 2     2   238 use 5.006; use strict; use warnings; use Rope;
  2     2   7  
  2     2   12  
  2     2   4  
  2         89  
  2         12  
  2         3  
  2         120  
  2         1425  
  2         26894  
  2         14  
8             our $VERSION = '0.08';
9             our (%PRO, %OPTIONS);
10              
11             BEGIN {
12             %PRO = (
13             keyword => sub {
14 2     2   871 no strict;
  2         6  
  2         269  
15 8         18 my ($caller, $meth, $cb) = @_;
16 8     5   25 *{"${caller}::${meth}"} = sub { $cb->($caller, @_) };
  8         45  
  5         375898  
17             }
18 2     2   2653 );
19             }
20              
21             sub import {
22 2     2   31 my ($pkg, @export) = @_;
23              
24 2         7 my $caller = caller();
25              
26 2 50       11 @export = qw/option title abstract colors/ unless @export;
27              
28 2         6 $PRO{keyword}($caller, $_, \&{$_}) for @export;
  8         30  
29              
30 2         6 my $extend = 'Rope::Cmd';
31 2         4 my $isa = '@' . $caller . '::ISA';
32 2         210 eval "push $isa, '$extend'";
33             }
34              
35             sub option {
36 3     3 0 20 my ($pkg, $name, %options) = @_;
37 3   33     15 ! exists $options{$_} && do { $options{$_} = 1 } for qw/enumerable configurable/;
  6         16  
38 3         13 $OPTIONS{$pkg}{options}{$name} = \%options;
39             }
40              
41             sub options {
42 1     1 0 13 my ($pkg) = @_;
43 1         3 return $OPTIONS{$pkg}{options};
44             }
45              
46             sub title {
47 1     1 0 5 my ($pkg, $str) = @_;
48 1         6 $OPTIONS{$pkg}{title} = $str;
49             }
50              
51             sub abstract {
52 1     1 0 5 my ($pkg, $str) = @_;
53 1         18 $OPTIONS{$pkg}{abstract} = $str;
54             }
55              
56             sub colors {
57 0     0 0 0 my ($pkg, %colors) = @_;
58 0         0 $OPTIONS{$pkg}{colors} = \%colors;
59             }
60              
61             sub run {
62 2     2 0 12 my ($pkg, @params) = @_;
63             my $self = Rope->new({ name => $pkg, use => [qw/Rope::Autoload/], properties => {
64 2         23 %{ $OPTIONS{$pkg}{options} },
65             print_color => {
66             writeable => 1,
67             value => sub {
68 0     0   0 my ($self, $color, $text) = @_;
69 0         0 print sprintf('%' . $color, $text);
70             }
71             }
72 2         5 }});
73 2         4438 my %map;
74 2         6 my ($options, $max) = ($OPTIONS{$pkg}{options}, 0);
75 2         4 for my $o (sort keys %{$options}) {
  2         7  
76 6         6 my $cur = length $o;
77 6         7 $map{$o} = $o;
78 6 100       11 if ($options->{$o}{option_alias}) {
79 2         4 $map{$options->{$o}{option_alias}} = $o;
80 2         3 $cur += length($options->{$o}{option_alias}) + 1;
81             }
82 6 100       11 $max = $cur if ($cur > $max);
83             }
84              
85 2         9 my $colors = $self->_default_colors($OPTIONS{$pkg}{colors});
86              
87 2 100 66     13 if (scalar @params == 1 && $params[0] =~ m/^(h|help)$/) {
88 1         35 print sprintf('%' . $colors->{title}, $OPTIONS{$pkg}{title} . "\n\n");
89 1         49 print sprintf('%' . $colors->{abstract}, $OPTIONS{$pkg}{abstract} . "\n\n");
90 1         17 print sprintf('%' . $colors->{options_title}, "Options" . "\n\n");
91 1         7 for my $o (sort keys %{$options}) {
  1         4  
92             print sprintf(
93             "%$colors->{options} %$colors->{options_description}\n",
94             pack("A${max}", ($options->{$o}{option_alias} ? sprintf("%s|", $options->{$o}{option_alias}) : "") . $o),
95             $options->{$o}{description}
96 3 100       53 );
97             }
98 1         11 return;
99             }
100              
101 1         2 for my $param (@params) {
102 3         126 my ($key, $value) = split("\=", $param, 2);
103 3         7 $self->{$map{$key}} = $value;
104             }
105              
106 1         653 $self->callback();
107              
108 1         228 return $self;
109             }
110              
111             sub _default_colors {
112 2     2   6 my ($self, $colors) = @_;
113 2         19 for (qw/title abstract options_title options options_description/) {
114 10 50       21 $colors->{$_} = 's' unless $colors->{$_};
115             }
116 2         4 return $colors;
117             }
118              
119             1;
120              
121             __END__;