DocKnot/Command.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« DocKnot.pm ↑ Summary DocKnot/Dist.pm »
1 # Dispatch code for the DocKnot application.
2 #
3 # DocKnot provides various commands for generating documentation, web pages,
4 # and software releases. This module provides command-line parsing and
5 # dispatch of commands to the various App::DocKnot modules.
6 #
7 # SPDX-License-Identifier: MIT
8  
9 ##############################################################################
10 # Modules and declarations
11 ##############################################################################
12  
13 package App::DocKnot::Command v8.0.1;
14  
15 4 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 0 ▶use App::DocKnot::Dist;
Subroutine BEGIN: called CC=1 SCAR=2.6
20 use App::DocKnot::Generate;
21 use App::DocKnot::Release;
22 use App::DocKnot::Spin;
23 use App::DocKnot::Spin::RSS;
24 use App::DocKnot::Spin::Thread;
25 use App::DocKnot::Update;
26 use Getopt::Long;
27 use Pod::Usage qw(pod2usage);
28  
29 # Defines the subcommands, their options, and the module and method that
30 # implements them. The keys are the names of the commands. Each value is a
31 # hash with one or more of the following keys:
32 #
33 # code
34 # A reference to a function to call to implement this command. If set,
35 # overrides method and module. The function will be passed a reference to
36 # the hash resulting from option parsing as its first argument and any
37 # other command-line arguments as its remaining arguments.
38 #
39 # maximum
40 # The maximum number of positional arguments this command takes.
41 #
42 # method
43 # The name of the method to run to implement this command. It is passed
44 # as arguments any remaining command-line arguments after option parsing.
45 #
46 # minimum
47 # The minimum number of positional arguments this command takes.
48 #
49 # module
50 # The name of the module that implements this command. Its constructor
51 # (which must be named new) will be passed as its sole argument a
52 # reference to the hash containing the results of parsing any options.
53 #
54 # options
55 # A reference to an array of Getopt::Long option specifications defining
56 # the arguments that can be passed to this subcommand.
57 #
58 # required
59 # A reference to an array of required option names (the part before any |
60 # in the option specification for that option). If any of these options
61 # are not set, an error will be thrown.
62 our %COMMANDS = (
63 dist => {
64 method => 'make_distribution',
65 module => 'App::DocKnot::Dist',
66 options => ['distdir|d=s', 'metadata|m=s', 'pgp-key|p=s'],
67 maximum => 0,
68 },
69 generate => {
70 method => 'generate_output',
71 module => 'App::DocKnot::Generate',
72 options => ['metadata|m=s', 'width|w=i'],
73 maximum => 2,
74 minimum => 1,
75 },
76 'generate-all' => {
77 method => 'generate_all',
78 module => 'App::DocKnot::Generate',
79 options => ['metadata|m=s', 'width|w=i'],
80 maximum => 0,
81 },
82 release => {
83 method => 'release',
84 module => 'App::DocKnot::Release',
85 options => ['archivedir|a=s', 'distdir|d=s', 'metadata|m=s'],
86 maximum => 0,
87 },
88 spin => {
89 method => 'spin',
90 module => 'App::DocKnot::Spin',
91 options => ['delete|d', 'exclude|e=s@', 'style-url|s=s'],
92 minimum => 2,
93 maximum => 2,
94 },
95 'spin-rss' => {
96 method => 'generate',
97 module => 'App::DocKnot::Spin::RSS',
98 options => ['base|b=s'],
99 minimum => 1,
100 maximum => 1,
101 },
102 'spin-text' => {
103 method => 'spin_text_file',
104 module => 'App::DocKnot::Spin::Text',
105 options => [
106 'modified|m', 'style|s=s', 'title|t=s', 'use-value|u',
107 ],
108 maximum => 2,
109 },
110 'spin-thread' => {
111 method => 'spin_thread_file',
112 module => 'App::DocKnot::Spin::Thread',
113 options => ['style-url|s=s'],
114 maximum => 2,
115 },
116 update => {
117 method => 'update',
118 module => 'App::DocKnot::Update',
119 options => ['metadata|m=s', 'output|o=s'],
120 maximum => 0,
121 },
122 'update-spin' => {
123 method => 'update_spin',
124 module => 'App::DocKnot::Update',
125 maximum => 1,
126 },
127 );
128  
129 ##############################################################################
130 # Option parsing
131 ##############################################################################
132  
133 # Parse command-line options and do any required error handling.
134 #
135 # $command - The command being run or undef for top-level options
136 # $options_ref - A reference to the options specification
137 # @args - The arguments to the command
138 #
139 # Returns: A list composed of a reference to a hash of options and values,
140 # followed by a reference to the remaining arguments after options
141 # have been extracted
142 # Throws: A text error message if the options are invalid
143 sub _parse_options {
144 my ($self, $command, $options_ref, @args) = @_;
145  
146 # Use the object-oriented syntax to isolate configuration options from the
147 # rest of the program.
148 my $parser = Getopt::Long::Parser->new;
149 $parser->configure(qw(bundling no_ignore_case require_order));
150  
151 # Parse the options and capture any errors, turning them into exceptions.
152 # The first letter of the Getopt::Long warning message will be capitalized
153 # but we want it to be lowercase to follow our error message standard.
154 my %opts;
155 {
156 my $error = 'option parsing failed';
157 local $SIG{__WARN__} = sub { ($error) = @_ };
158 if (!$parser->getoptionsfromarray(\@args, \%opts, $options_ref->@*)) {
159 $error =~ s{ \n+ \z }{}xms;
160 $error =~ s{ \A (\w) }{ lc($1) }xmse;
161 if ($command) {
162 die "$0 $command: $error\n";
163 } else {
164 die "$0: $error\n";
165 }
166 }
167 }
168  
169 # Success. Return the options and the remaining arguments.
170 return (\%opts, \@args);
171 }
172  
173 # Parse command-line options for a given command.
174 #
175 # $command - The command being run
176 # @args - The arguments to the command
177 #
178 # Returns: A list composed of a reference to a hash of options and values,
179 # followed by a reference to the remaining arguments after options
180 # have been extracted
181 # Throws: A text error message if the options are invalid
182 sub _parse_command {
183 my ($self, $command, @args) = @_;
184 my $options_ref = $COMMANDS{$command}{options};
185 return $self->_parse_options($command, $options_ref, @args);
186 }
187  
188 ##############################################################################
189 # Error handling
190 ##############################################################################
191  
192 # Reformat an error message (from warn or die) to prepend the command run and
193 # to strip the file and line information from Perl.
194 #
195 # $command - Invoked command
196 # $error - Error to reformat
197 #
198 # Return: Reformatted error suitable for passing to warn or die, with no
199 # trailing newline (the caller should add it)
200 sub _reformat_error {
201 my ($self, $command, $error) = @_;
202 chomp($error);
203 $error =~ s{ \s+ at \s+ \S+ \s+ line \s+ \d+ [.]? \z }{}xms;
204 if ($error =~ m{ \S+ : \d+ : \s+ \S }xms) {
205 return "$0 $command:$error";
206 } else {
207 return "$0 $command: $error";
208 }
209 }
210  
211 ##############################################################################
212 # Public interface
213 ##############################################################################
214  
215 # Create a new App::DocKnot::Command object.
216 #
217 # Returns: Newly created object
218 sub new {
219 my ($class) = @_;
220 my $self = {};
221 bless($self, $class);
222 return $self;
223 }
224  
225 # Parse command-line options to determine which command to run, and then
226 # dispatch that command.
227 #
228 # @args - Command-line arguments (optional, default: @ARGV)
229 #
230 # Returns: undef
231 # Throws: A text error message for invalid arguments
232 sub run {
233 my ($self, @args) = @_;
234 if (!@args) {
235 @args = @ARGV;
236 }
237  
238 # Parse the initial options and extract the subcommand to run, preserving
239 # any options after the subcommand.
240 my $spec = ['help|h'];
241 my ($opts_ref, $args_ref) = $self->_parse_options(undef, $spec, @args);
242 if ($opts_ref->{help}) {
243 pod2usage(0);
244 }
245 if (!$args_ref->@*) {
246 die "$0: no subcommand given\n";
247 }
248 my $command = shift($args_ref->@*);
249 if (!$COMMANDS{$command}) {
250 die "$0: unknown command $command\n";
251 }
252  
253 # Parse the arguments for the command and check for required arguments.
254 ($opts_ref, $args_ref) = $self->_parse_command($command, $args_ref->@*);
255 if (exists($COMMANDS{$command}{required})) {
256 for my $required ($COMMANDS{$command}{required}->@*) {
257 if (!exists($opts_ref->{$required})) {
258 die "$0 $command: missing required option --$required\n";
259 }
260 }
261 }
262  
263 # Check that we have the correct number of remaining arguments.
264 if (exists($COMMANDS{$command}{maximum})) {
265 if (scalar($args_ref->@*) > $COMMANDS{$command}{maximum}) {
266 die "$0 $command: too many arguments\n";
267 }
268 }
269 if (exists($COMMANDS{$command}{minimum})) {
270 if (scalar($args_ref->@*) < $COMMANDS{$command}{minimum}) {
271 die "$0 $command: too few arguments\n";
272 }
273 }
274  
275 # Dispatch the command and turn exceptions into error messages. Also
276 # capture warnings and perform the same transformation on those.
277 local $SIG{__WARN__} = sub {
278 my ($error) = @_;
279 $error = $self->_reformat_error($command, $error);
280 warn "$error\n";
281 };
282 eval {
283 my $object = $COMMANDS{$command}{module}->new($opts_ref);
284 my $method = $COMMANDS{$command}{method};
285 $object->$method($args_ref->@*);
286 };
287 if ($@) {
288 my $error = $self->_reformat_error($command, $@);
289 die "$error\n";
290 }
291 return;
292 }
293  
294 ##############################################################################
295 # Module return value and documentation
296 ##############################################################################
297  
298 1;
299 __END__
« DocKnot.pm ↑ Summary DocKnot/Dist.pm »