Spec/Plugin/Help.pm

filter: Statementstmt 94.4% 51 / 54 Branchbran 90.0% 9 / 10 Conditioncond 50.0% 7 / 14 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 9 / 9 Podpod 100% 5 / 5 totaltotal 83.8% 83 / 99 CC 13 SCAR 15.9
CC
13
Cov
86%
global_options
9.0
_add_subcommands
5.0
install_subcommands
3.0
CRAP 4.9
« Spec/Role/Command.pm ↑ Summary Spec/Run/Response.pm »
1 # ABSTRACT: App::Spec Plugin for help subcommand and options
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package App::Spec::Plugin::Help;
5 7 our $VERSION = 'v0.15.1'; # TRIAL VERSION
6  
7 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 with 'App::Spec::Role::Plugin::Subcommand';
9 7 with 'App::Spec::Role::Plugin::GlobalOptions';
10  
11 7 my $yaml;
12 7 my $cmd = {
13 name => 'help',
14 summary => 'Show command help',
15 class => 'App::Spec::Plugin::Help',
16 op => 'cmd_help',
17 subcommand_required => 0,
18 options => [
19 { spec => 'all' },
20 ],
21 };
22 7 my $options = [
23 {
24 name => 'help',
25 summary => 'Show command help',
26 type => 'flag',
27 aliases => ['h'],
28 },
29 ];
30  
31 44 ▶sub install_subcommands {
Subroutine install_subcommands: called CC=3 SCAR=11.0
Pod documented
32 44 my ($class, %args) = @_;
33 44 my $parent = $args{spec};
34 44 my $appspec = App::Spec::Subcommand->read($cmd);
35  
36 44 ▶ my $help_subcmds = $appspec->subcommands || {};
Condition50%1/2
$appspec->subcommands || {}
l!l
044
MC/DC0%0/1
$appspec->subcommands || {}
$appspec->subcommands
Truth table50%1/2
A
A= $appspec->subcommands
Aresult
11
00
37  
38 44 ▶ my $parent_subcmds = $parent->subcommands || {};
Condition100%2/2
$parent->subcommands || {}
l!l
386
MC/DC100%1/1
$parent->subcommands || {}
$parent->subcommands
Truth table100%2/2
A
A= $parent->subcommands
Aresult
11
00
39 44 $class->_add_subcommands($help_subcmds, $parent_subcmds, { subcommand_required => 0 });
40 44 $appspec->subcommands($help_subcmds);
41  
42 44 return $appspec;
43 }
44  
45 7 ▶sub cmd_help {
Subroutine cmd_help: called CC=1 SCAR=0.0
Pod documented
46 7 my ($self, $run) = @_;
47 7 my $spec = $run->spec;
48 7 my $cmds = $run->commands;
49 7 shift @$cmds;
50 7 my $help = $spec->usage(
51 commands => $cmds,
52 colored => $run->colorize_code,
53 );
54 7 $run->out($help);
55 }
56  
57 492 ▶sub _add_subcommands {
Subroutine _add_subcommands: called CC=5 SCAR=16.1
58 492 my ($self, $commands1, $commands2, $ref) = @_;
59 492 ▶ for my $name (keys %{ $commands2 || {} }) {
Condition50%1/2
$commands2 || {}
l!l
4920
MC/DC0%0/1
$commands2 || {}
$commands2
Truth table50%1/2
A
A= $commands2
Aresult
11
00
60 449 ▶ next if $name eq "help";
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "help" 1 448 449
61 448 my $cmd = $commands2->{ $name };
62 448 $commands1->{ $name } = App::Spec::Subcommand->new(
63 name => $name,
64 subcommands => {},
65 %$ref,
66 );
67 448 ▶ my $subcmds = $cmd->{subcommands} || {};
Condition100%2/2
$cmd->{'subcommands'} || {}
l!l
122326
MC/DC100%1/1
$cmd->{'subcommands'} || {}
$cmd->{'subcommands'}
Truth table100%2/2
A
A= $cmd->{'subcommands'}
Aresult
11
00
68 448 $self->_add_subcommands($commands1->{ $name }->{subcommands}, $subcmds, $ref);
69 }
70 }
71  
72 44 ▶sub install_options {
Subroutine install_options: called CC=1 SCAR=0.0
Pod documented
73 44 my ($class, %args) = @_;
74 44 return $options;
75 }
76  
77 34 ▶sub init_run {
Subroutine init_run: called CC=1 SCAR=0.0
Pod documented
78 34 my ($self, $run) = @_;
79 34 $run->subscribe(
80 global_options => {
81 plugin => $self,
82 method => "global_options",
83 },
84 );
85 }
86  
87 36 ▶sub global_options {
Subroutine global_options: called CC=7 SCAR=22.0
Pod documented
88 36 my ($self, %args) = @_;
89 36 my $run = $args{run};
90 36 my $options = $run->options;
91 36 my $op;
92  
93 36 ▶ if ($run->spec->has_subcommands) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run->spec->has_subcommands) { } 34 2 36
94 34 ▶ if ($options->{help} and (not @{ $run->argv } or $run->argv->[0] ne "help")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"help"} and !@{$run->argv;} || $run->argv->[0] ne "help") 0 34 34
Condition16%1/6
$options->{"help"} and !@{$run->argv;} || $run->argv->[0] ne "help"
!ll&&!rl&&r
3400
not @{$run->argv;} or $run->argv->[0] ne "help"
l!l&&r!l&&!r
000
MC/DC0%0/3
$options->{"help"} and !@{$run->argv;} || $run->argv->[0] ne "help"
$options->{"help"} not @{$run->argv;} $run->argv->[0] ne "help"
Truth table25%1/4
A and not(B) or C
A= $options->{"help"}B= not @{$run->argv;}C= $run->argv->[0] ne "help"
ABCresult
0XX0
1000
11X1
1011
95 # call subcommand 'help'
96 0 unshift @{ $run->argv }, "help";
97 }
98 }
99 else {
100 2 ▶ if ($options->{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"help"}) 1 1 2
101 1 $op = "::cmd_help";
102 }
103 }
104  
105 36 ▶ $run->op("App::Spec::Plugin::Help$op") if $op;
Branch: 1 branch
BranchTrueFalseTotal
if $op 1 35 36
106 }
107  
108  
109 7 1;
110  
111 =pod
112  
113 =head1 NAME
114  
115 App::Spec::Plugin::Help - App::Spec Plugin for help subcommand and options
116  
117 =head1 DESCRIPTION
118  
119 This plugin is enabled in L<App::Spec> by default.
120  
121 This is a plugin which adds C<-h|--help> options to your app.
122 Also for apps with subcommands it adds a subcommand C<help>.
123  
124 The help command can then be called with all existing subcommands, like this:
125  
126 % app cmd1
127 % app cmd2
128 % app cmd2 cmd2a
129 % app help
130 % app help cmd1
131 % app help cmd2
132 % app help cmd2 cmd2a
133  
134 =head1 METHODS
135  
136 =over 4
137  
138 =item cmd_help
139  
140 This is the code which is executed when using C<-h|--help> or the subcommand
141 help.
142  
143 =item install_options
144  
145 This method is required by L<App::Spec::Role::Plugin::GlobalOptions>.
146  
147 See L<App::Spec::Role::Plugin::GlobalOptions#install_options>.
148  
149 =item install_subcommands
150  
151 This is required by L<App::Spec::Role::Plugin::Subcommand>.
152  
153 See L<App::Spec::Role::Plugin::Subcommand#install_subcommands>.
154  
155 =item global_options
156  
157 This method is called by L<App::Spec::Run> after global options have been read.
158  
159 For apps without subcommands it just sets the method to execute to
160 L<App::Spec::Plugin::Help::cmd_help>.
161 No further processing is done.
162  
163 For apps with subcommands it inserts C<help> at the beginning of the
164 commandline arguments and continues processing.
165  
166 =item init_run
167  
168 See L<App::Spec::Plugin>
169  
170 =back
171  
172 =cut
173  
« Spec/Role/Command.pm ↑ Summary Spec/Run/Response.pm »