line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# You may distribute under the terms of the GNU General Public License |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# (C) Paul Evans, 2008-2014 -- leonerd@leonerd.org.uk |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Circle::Command; |
6
|
|
|
|
|
|
|
|
7
|
4
|
|
|
4
|
|
19
|
use strict; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
108
|
|
8
|
4
|
|
|
4
|
|
16
|
use warnings; |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
104
|
|
9
|
|
|
|
|
|
|
|
10
|
4
|
|
|
4
|
|
16
|
use Attribute::Storage qw( get_subattrs get_subattr find_subs_with_attr ); |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
21
|
|
11
|
|
|
|
|
|
|
require mro; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub _find_commands |
14
|
|
|
|
|
|
|
{ |
15
|
0
|
|
|
0
|
|
|
my ( $obj, $cinv, $containedby ) = @_; |
16
|
|
|
|
|
|
|
|
17
|
0
|
|
|
|
|
|
my @ret; |
18
|
|
|
|
|
|
|
my %commands; |
19
|
|
|
|
|
|
|
|
20
|
0
|
|
|
|
|
|
while( $obj ) { |
21
|
0
|
|
|
|
|
|
my %subs = find_subs_with_attr( mro::get_linear_isa( ref $obj ), "Command_description", |
22
|
|
|
|
|
|
|
matching => qr/^command_/, |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
0
|
|
|
|
|
|
foreach my $name ( keys %subs ) { |
26
|
0
|
|
|
|
|
|
( my $commandname = $name ) =~ s/^command_//; |
27
|
0
|
|
|
|
|
|
my $cv = $subs{$name}; |
28
|
|
|
|
|
|
|
|
29
|
0
|
0
|
|
|
|
|
next if $commands{$commandname}; |
30
|
|
|
|
|
|
|
|
31
|
0
|
|
|
|
|
|
my $subof = get_subattr( $cv, "Command_subof" ); |
32
|
0
|
0
|
0
|
|
|
|
next if $containedby and !$subof or |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
33
|
|
|
|
|
|
|
!$containedby and $subof or |
34
|
|
|
|
|
|
|
$containedby and $subof and $containedby ne $subof; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
my $attrs = get_subattrs( $cv ); |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
$commands{$commandname} = 1; |
39
|
0
|
|
|
|
|
|
push @ret, __PACKAGE__->new( %$attrs, |
40
|
|
|
|
|
|
|
name => $commandname, |
41
|
|
|
|
|
|
|
obj => $obj, |
42
|
|
|
|
|
|
|
cv => $cv, |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# Collect in parent too |
47
|
0
|
|
0
|
|
|
|
$obj = $obj->can( "commandable_parent" ) && $obj->commandable_parent( $cinv ); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
return @ret; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub root_commands |
54
|
|
|
|
|
|
|
{ |
55
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
56
|
0
|
|
|
|
|
|
my ( $cinv ) = @_; |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
return map { $_->name => $_ } _find_commands( $cinv->invocant, $cinv, undef ); |
|
0
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# Object stuff |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub new |
64
|
|
|
|
|
|
|
{ |
65
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
66
|
0
|
|
|
|
|
|
my %attrs = @_; |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
$attrs{name} =~ s/_/ /g; |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
return bless \%attrs, $class; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub sub_commands |
74
|
|
|
|
|
|
|
{ |
75
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
76
|
0
|
|
|
|
|
|
my ( $cinv ) = @_; |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
return map { $_->shortname => $_ } _find_commands( $cinv->invocant, $cinv, $self->name ); |
|
0
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub name |
82
|
|
|
|
|
|
|
{ |
83
|
0
|
|
|
0
|
0
|
|
my $self = shift; return $self->{name}; |
|
0
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub shortname |
87
|
|
|
|
|
|
|
{ |
88
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
89
|
0
|
|
|
|
|
|
( split m/ /, $self->name )[-1]; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub is_default |
93
|
|
|
|
|
|
|
{ |
94
|
0
|
|
|
0
|
0
|
|
my $self = shift; return $self->{Command_default}; |
|
0
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub desc |
98
|
|
|
|
|
|
|
{ |
99
|
0
|
|
0
|
0
|
0
|
|
my $self = shift; return $self->{Command_description}[0] || "[no description]"; |
|
0
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub detail |
103
|
|
|
|
|
|
|
{ |
104
|
0
|
|
|
0
|
0
|
|
my $self = shift; return $self->{Command_description}[1]; |
|
0
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub args |
108
|
|
|
|
|
|
|
{ |
109
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
110
|
0
|
0
|
|
|
|
|
return unless $self->{Command_arg}; |
111
|
0
|
|
|
|
|
|
return @{ $self->{Command_arg} }; |
|
0
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub opts |
115
|
|
|
|
|
|
|
{ |
116
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
117
|
0
|
|
|
|
|
|
return $self->{Command_opt}; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub default_sub |
121
|
|
|
|
|
|
|
{ |
122
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
123
|
0
|
|
|
|
|
|
my ( $cinv ) = @_; |
124
|
|
|
|
|
|
|
|
125
|
0
|
|
|
|
|
|
my %subs = $self->sub_commands( $cinv ); |
126
|
0
|
|
|
|
|
|
my @defaults = grep { $_->is_default } values %subs; |
|
0
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
|
128
|
0
|
0
|
|
|
|
|
return $defaults[0] if @defaults == 1; # Only if it's unique |
129
|
0
|
|
|
|
|
|
return; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub invoke |
133
|
|
|
|
|
|
|
{ |
134
|
0
|
|
|
0
|
0
|
|
my $self = shift; $self->{cv}->( $self->{obj}, @_ ); |
|
0
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
0x55AA; |