File Coverage

lib/CLI/Framework/Command/Tree.pm
Criterion Covered Total %
statement 43 44 97.7
branch 6 6 100.0
condition 6 7 85.7
subroutine 7 8 87.5
pod 2 3 66.6
total 64 68 94.1


line stmt bran cond sub pod time code
1             package CLI::Framework::Command::Tree;
2 1     1   4 use base qw( CLI::Framework::Command::Meta );
  1         0  
  1         270  
3              
4 1     1   4 use strict;
  1         1  
  1         18  
5 1     1   2 use warnings;
  1         2  
  1         161  
6              
7             our $VERSION = 0.01;
8              
9             #-------
10              
11             sub usage_text {
12 0     0 1 0 q{
13             tree: tree view of the names of only those commands that are currently registered in the application
14             }
15             }
16              
17             sub run {
18 1     1 1 1 my ($self, $opts, @args) = @_;
19            
20 1         4 my $app = $self->get_app(); # metacommand is app-aware
21              
22 1         2 my $tree = command_tree( $app );
23 1         9 $tree =~ s/^/\t/gm;
24 1         3 return $tree;
25             }
26              
27             #-------
28              
29             sub command_tree {
30 10     10 0 9 my ($app, $root, $indent, $tree) = @_;
31              
32 10   66     15 $root ||= $app;
33 10   100     18 $indent ||= 0;
34              
35             # (output object)
36 10 100       20 $tree = { text => '' } unless ref $tree;
37              
38 10 100       23 $indent += 4 if( $root->isa( 'CLI::Framework::Command' ) );
39              
40             # For every command registered into the root object (either a CLIF
41             # Application or a CLIF Command), append its tree representation to the
42             # output object...
43              
44             # Use proper accessors for object type...
45 10         5 my $registered_command_names_accessor = 'registered_command_names';
46 10         8 my $registered_command_obj_accessor = 'registered_command_object';
47 10 100       20 if( $root->isa('CLI::Framework::Command') ) {
48 9         4 $registered_command_names_accessor = 'registered_subcommand_names';
49 9         7 $registered_command_obj_accessor = 'registered_subcommand_object';
50             }
51 10         6 my @command_names;
52 1     1   3 { no strict 'refs';
  1         1  
  1         47  
  10         6  
53 10         35 @command_names = $root->$registered_command_names_accessor;
54             }
55 10         12 for my $command_name (@command_names) {
56             #XXX-ALTERNATIVE: show a tree of command names
57             # $tree->{text} .= ' 'x$indent . $command_name . "\n";
58              
59 9         6 my $command_obj;
60 1     1   3 { no strict 'refs';
  1         1  
  1         126  
  9         6  
61 9         16 $command_obj = $root->$registered_command_obj_accessor( $command_name );
62             }
63              
64             #XXX-ALTERNATIVE: show a tree of Perl package names defining the commands (including
65             # source files they were defined in):
66 9         19 my $source = Class::Inspector->loaded_filename( ref $command_obj );
67 9   100     144 $source ||= 'defined inline';
68 9         16 my $x = ref ($command_obj) . " ($source)";
69 9         22 $tree->{text} .= ' 'x$indent . $x . "\n";
70              
71             # Recursive call (NOTE: passing output object reference which will act
72             # as an accumulator)...
73 9         15 command_tree( $app, $command_obj, $indent, $tree );
74             }
75 10         21 return $tree->{text} . "\n";
76             }
77              
78             #-------
79             1;
80              
81             __END__