File Coverage

blib/lib/App/Cmd/Subdispatch.pm
Criterion Covered Total %
statement 26 37 70.2
branch 4 8 50.0
condition 0 6 0.0
subroutine 8 11 72.7
pod 4 4 100.0
total 42 66 63.6


line stmt bran cond sub pod time code
1             # The "experimental" below is not actually scary. The feature went on to be
2             # de-experimental-ized with no changes and is now on by default in perl v5.24
3             # and later. -- rjbs, 2021-03-14
4 2     2   1818 use 5.020;
  2         7  
5 2     2   10 use warnings;
  2         5  
  2         65  
6 2     2   11 use experimental qw(postderef postderef_qq);
  2         5  
  2         14  
7              
8             package App::Cmd::Subdispatch 0.335;
9              
10 2     2   380 use App::Cmd;
  2         3  
  2         19  
11 2     2   988 use App::Cmd::Command;
  2         5  
  2         90  
12 2     2   951 BEGIN { our @ISA = qw(App::Cmd::Command App::Cmd) }
13              
14             # ABSTRACT: an App::Cmd::Command that is also an App::Cmd
15              
16             #pod =method new
17             #pod
18             #pod A hackish new that allows us to have an Command instance before they normally
19             #pod exist.
20             #pod
21             #pod =cut
22              
23             sub new {
24 2     2 1 5 my ($inv, $fields, @args) = @_;
25 2 50       5 if (ref $inv) {
26 0         0 @{ $inv }{ keys %$fields } = values %$fields;
  0         0  
27 0         0 return $inv;
28             } else {
29 2         9 $inv->SUPER::new($fields, @args);
30             }
31             }
32              
33             #pod =method prepare
34             #pod
35             #pod my $subcmd = $subdispatch->prepare($app, @args);
36             #pod
37             #pod An overridden version of L that performs a new
38             #pod dispatch cycle.
39             #pod
40             #pod =cut
41              
42             sub prepare {
43 2     2 1 5 my ($class, $app, @args) = @_;
44              
45 2         9 my $self = $class->new({ app => $app });
46              
47 2         9 my ($subcommand, $opt, @sub_args) = $self->get_command(@args);
48              
49 2         12 $self->set_global_options($opt);
50              
51 2 100       5 if (defined $subcommand) {
52 1         8 return $self->_prepare_command($subcommand, $opt, @sub_args);
53             } else {
54 1 50       4 if (@args) {
55 0         0 return $self->_bad_command(undef, $opt, @sub_args);
56             } else {
57 1         8 return $self->_prepare_default_command($opt, @sub_args);
58             }
59             }
60             }
61              
62             sub _plugin_prepare {
63 0     0     my ($self, $plugin, @args) = @_;
64 0           return $plugin->prepare($self->choose_parent_app($self->app, $plugin), @args);
65             }
66              
67             #pod =method app
68             #pod
69             #pod $subdispatch->app;
70             #pod
71             #pod This method returns the application that this subdispatch is a command of.
72             #pod
73             #pod =cut
74              
75 0     0 1   sub app { $_[0]{app} }
76              
77             #pod =method choose_parent_app
78             #pod
79             #pod $subcmd->prepare(
80             #pod $subdispatch->choose_parent_app($app, $opt, $plugin),
81             #pod @$args
82             #pod );
83             #pod
84             #pod A method that chooses whether the parent app or the subdispatch is going to be
85             #pod C<< $cmd->app >>.
86             #pod
87             #pod =cut
88              
89             sub choose_parent_app {
90 0     0 1   my ( $self, $app, $plugin ) = @_;
91              
92 0 0 0       if (
      0        
93             $plugin->isa("App::Cmd::Command::commands")
94             or $plugin->isa("App::Cmd::Command::help")
95             or keys $self->global_options->%*
96             ) {
97 0           return $self;
98             } else {
99 0           return $app;
100             }
101             }
102              
103             1;
104              
105             __END__