| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # ABSTRACT: App::Spec Plugin for help subcommand and options | 
| 2 | 4 |  |  | 4 |  | 1730 | use strict; | 
|  | 4 |  |  |  |  | 9 |  | 
|  | 4 |  |  |  |  | 108 |  | 
| 3 | 4 |  |  | 4 |  | 19 | use warnings; | 
|  | 4 |  |  |  |  | 7 |  | 
|  | 4 |  |  |  |  | 187 |  | 
| 4 |  |  |  |  |  |  | package App::Spec::Plugin::Help; | 
| 5 |  |  |  |  |  |  | our $VERSION = '0.012'; # VERSION | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 4 |  |  | 4 |  | 20 | use Moo; | 
|  | 4 |  |  |  |  | 9 |  | 
|  | 4 |  |  |  |  | 20 |  | 
| 8 |  |  |  |  |  |  | with 'App::Spec::Role::Plugin::Subcommand'; | 
| 9 |  |  |  |  |  |  | with 'App::Spec::Role::Plugin::GlobalOptions'; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | my $yaml; | 
| 12 |  |  |  |  |  |  | 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 |  |  |  |  |  |  | my $options  = [ | 
| 23 |  |  |  |  |  |  | { | 
| 24 |  |  |  |  |  |  | name => 'help', | 
| 25 |  |  |  |  |  |  | summary => 'Show command help', | 
| 26 |  |  |  |  |  |  | type => 'flag', | 
| 27 |  |  |  |  |  |  | aliases => ['h'], | 
| 28 |  |  |  |  |  |  | }, | 
| 29 |  |  |  |  |  |  | ]; | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | sub install_subcommands { | 
| 32 | 31 |  |  | 31 | 1 | 131 | my ($class, %args) = @_; | 
| 33 | 31 |  |  |  |  | 85 | my $parent = $args{spec}; | 
| 34 | 31 |  |  |  |  | 121 | my $appspec = App::Spec::Subcommand->read($cmd); | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 31 |  | 50 |  |  | 212 | my $help_subcmds = $appspec->subcommands || {}; | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 31 |  | 100 |  |  | 153 | my $parent_subcmds = $parent->subcommands || {}; | 
| 39 | 31 |  |  |  |  | 181 | $class->_add_subcommands($help_subcmds, $parent_subcmds, { subcommand_required => 0 }); | 
| 40 | 31 |  |  |  |  | 147 | $appspec->subcommands($help_subcmds); | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 31 |  |  |  |  | 103 | return $appspec; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | sub cmd_help { | 
| 46 | 4 |  |  | 4 | 1 | 13 | my ($self, $run) = @_; | 
| 47 | 4 |  |  |  |  | 10 | my $spec = $run->spec; | 
| 48 | 4 |  |  |  |  | 9 | my $cmds = $run->commands; | 
| 49 | 4 |  |  |  |  | 8 | shift @$cmds; | 
| 50 | 4 |  |  |  |  | 17 | my $help = $spec->usage( | 
| 51 |  |  |  |  |  |  | commands => $cmds, | 
| 52 |  |  |  |  |  |  | colored => $run->colorize_code, | 
| 53 |  |  |  |  |  |  | ); | 
| 54 | 4 |  |  |  |  | 24 | $run->out($help); | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | sub _add_subcommands { | 
| 58 | 383 |  |  | 383 |  | 723 | my ($self, $commands1, $commands2, $ref) = @_; | 
| 59 | 383 | 50 |  |  |  | 526 | for my $name (keys %{ $commands2 || {} }) { | 
|  | 383 |  |  |  |  | 1472 |  | 
| 60 | 352 | 50 |  |  |  | 679 | next if $name eq "help"; | 
| 61 | 352 |  |  |  |  | 612 | my $cmd = $commands2->{ $name }; | 
| 62 | 352 |  |  |  |  | 5308 | $commands1->{ $name } = App::Spec::Subcommand->new( | 
| 63 |  |  |  |  |  |  | name => $name, | 
| 64 |  |  |  |  |  |  | subcommands => {}, | 
| 65 |  |  |  |  |  |  | %$ref, | 
| 66 |  |  |  |  |  |  | ); | 
| 67 | 352 |  | 100 |  |  | 2518 | my $subcmds = $cmd->{subcommands} || {}; | 
| 68 | 352 |  |  |  |  | 922 | $self->_add_subcommands($commands1->{ $name }->{subcommands}, $subcmds, $ref); | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | sub install_options { | 
| 73 | 31 |  |  | 31 | 1 | 108 | my ($class, %args) = @_; | 
| 74 | 31 |  |  |  |  | 87 | return $options; | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | sub init_run { | 
| 78 | 30 |  |  | 30 | 1 | 107 | my ($self, $run) = @_; | 
| 79 | 30 |  |  |  |  | 198 | $run->subscribe( | 
| 80 |  |  |  |  |  |  | global_options => { | 
| 81 |  |  |  |  |  |  | plugin => $self, | 
| 82 |  |  |  |  |  |  | method => "global_options", | 
| 83 |  |  |  |  |  |  | }, | 
| 84 |  |  |  |  |  |  | ); | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | sub global_options { | 
| 88 | 32 |  |  | 32 | 1 | 120 | my ($self, %args) = @_; | 
| 89 | 32 |  |  |  |  | 67 | my $run = $args{run}; | 
| 90 | 32 |  |  |  |  | 79 | my $options = $run->options; | 
| 91 | 32 |  |  |  |  | 59 | my $op; | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 32 | 100 |  |  |  | 136 | if ($run->spec->has_subcommands) { | 
| 94 | 30 | 0 | 0 |  |  | 89 | if ($options->{help} and (not @{ $run->argv } or $run->argv->[0] ne "help")) { | 
|  |  |  | 33 |  |  |  |  | 
| 95 |  |  |  |  |  |  | # call subcommand 'help' | 
| 96 | 0 |  |  |  |  | 0 | unshift @{ $run->argv }, "help"; | 
|  | 0 |  |  |  |  | 0 |  | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  | else { | 
| 100 | 2 | 100 |  |  |  | 9 | if ($options->{help}) { | 
| 101 | 1 |  |  |  |  | 3 | $op = "::cmd_help"; | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 | 32 | 100 |  |  |  | 141 | $run->op("App::Spec::Plugin::Help$op") if $op; | 
| 106 |  |  |  |  |  |  | } | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | 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 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. | 
| 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. | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | See L. | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | =item install_subcommands | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | This is required by L. | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | See L. | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | =item global_options | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | This method is called by L after global options have been read. | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | For apps without subcommands it just sets the method to execute to | 
| 160 |  |  |  |  |  |  | L. | 
| 161 |  |  |  |  |  |  | No further processing is done. | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | For apps with subcommands it inserts C at the beginning of the | 
| 164 |  |  |  |  |  |  | commandline arguments and continues processing. | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | =item init_run | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | See L | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | =back | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | =cut | 
| 173 |  |  |  |  |  |  |  |