| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package CLI::Framework::Command::Tree; | 
| 2 | 1 |  |  | 1 |  | 6 | use base qw( CLI::Framework::Command::Meta ); | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 460 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 1 |  |  | 1 |  | 7 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 36 |  | 
| 5 | 1 |  |  | 1 |  | 6 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 258 |  | 
| 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 | 2 | my ($self, $opts, @args) = @_; | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 1 |  |  |  |  | 8 | my $app = $self->get_app(); # metacommand is app-aware | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 1 |  |  |  |  | 6 | my $tree = command_tree( $app ); | 
| 23 | 1 |  |  |  |  | 27 | $tree =~ s/^/\t/gm; | 
| 24 | 1 |  |  |  |  | 4 | return $tree; | 
| 25 |  |  |  |  |  |  | } | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | #------- | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | sub command_tree { | 
| 30 | 10 |  |  | 10 | 0 | 15 | my ($app, $root, $indent, $tree) = @_; | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 10 |  | 66 |  |  | 23 | $root   ||= $app; | 
| 33 | 10 |  | 100 |  |  | 24 | $indent ||= 0; | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | # (output object) | 
| 36 | 10 | 100 |  |  |  | 30 | $tree = { text => '' } unless ref $tree; | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 10 | 100 |  |  |  | 41 | $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 |  |  |  |  | 11 | my $registered_command_names_accessor = 'registered_command_names'; | 
| 46 | 10 |  |  |  |  | 12 | my $registered_command_obj_accessor = 'registered_command_object'; | 
| 47 | 10 | 100 |  |  |  | 38 | if( $root->isa('CLI::Framework::Command') ) { | 
| 48 | 9 |  |  |  |  | 11 | $registered_command_names_accessor = 'registered_subcommand_names'; | 
| 49 | 9 |  |  |  |  | 12 | $registered_command_obj_accessor = 'registered_subcommand_object'; | 
| 50 |  |  |  |  |  |  | } | 
| 51 | 10 |  |  |  |  | 11 | my @command_names; | 
| 52 | 1 |  |  | 1 |  | 7 | {   no strict 'refs'; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 58 |  | 
|  | 10 |  |  |  |  | 8 |  | 
| 53 | 10 |  |  |  |  | 75 | @command_names = $root->$registered_command_names_accessor; | 
| 54 |  |  |  |  |  |  | } | 
| 55 | 10 |  |  |  |  | 20 | 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 |  |  |  |  | 10 | my $command_obj; | 
| 60 | 1 |  |  | 1 |  | 5 | {   no strict 'refs'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 198 |  | 
|  | 9 |  |  |  |  | 9 |  | 
| 61 | 9 |  |  |  |  | 28 | $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 |  |  |  |  | 35 | my $source = Class::Inspector->loaded_filename( ref $command_obj ); | 
| 67 | 9 |  | 100 |  |  | 225 | $source ||= 'defined inline'; | 
| 68 | 9 |  |  |  |  | 24 | my $x = ref ($command_obj) . " ($source)"; | 
| 69 | 9 |  |  |  |  | 35 | $tree->{text} .= ' 'x$indent . $x . "\n"; | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | # Recursive call (NOTE: passing output object reference which will act | 
| 72 |  |  |  |  |  |  | # as an accumulator)... | 
| 73 | 9 |  |  |  |  | 26 | command_tree( $app, $command_obj, $indent, $tree ); | 
| 74 |  |  |  |  |  |  | } | 
| 75 | 10 |  |  |  |  | 43 | return $tree->{text} . "\n"; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | #------- | 
| 79 |  |  |  |  |  |  | 1; | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | __END__ |