App-DocKnot
view release on metacpan or search on metacpan
lib/App/DocKnot/Command.pm view on Meta::CPAN
# The name of the module that implements this command. Its constructor
# (which must be named new) will be passed as its sole argument a
# reference to the hash containing the results of parsing any options.
#
# options
# A reference to an array of Getopt::Long option specifications defining
# the arguments that can be passed to this subcommand.
#
# required
# A reference to an array of required option names (the part before any |
# in the option specification for that option). If any of these options
# are not set, an error will be thrown.
our %COMMANDS = (
dist => {
method => 'make_distribution',
module => 'App::DocKnot::Dist',
options => ['distdir|d=s', 'metadata|m=s', 'pgp-key|p=s'],
maximum => 0,
},
generate => {
method => 'generate_output',
module => 'App::DocKnot::Generate',
options => ['metadata|m=s', 'width|w=i'],
maximum => 2,
minimum => 1,
},
'generate-all' => {
method => 'generate_all',
module => 'App::DocKnot::Generate',
options => ['metadata|m=s', 'width|w=i'],
maximum => 0,
},
release => {
method => 'release',
module => 'App::DocKnot::Release',
options => ['archivedir|a=s', 'distdir|d=s', 'metadata|m=s'],
maximum => 0,
},
spin => {
method => 'spin',
module => 'App::DocKnot::Spin',
options => ['delete|d', 'exclude|e=s@', 'style-url|s=s'],
minimum => 2,
maximum => 2,
},
'spin-rss' => {
method => 'generate',
module => 'App::DocKnot::Spin::RSS',
options => ['base|b=s'],
minimum => 1,
maximum => 1,
},
'spin-text' => {
method => 'spin_text_file',
module => 'App::DocKnot::Spin::Text',
options => [
'modified|m', 'style|s=s', 'title|t=s', 'use-value|u',
],
maximum => 2,
},
'spin-thread' => {
method => 'spin_thread_file',
module => 'App::DocKnot::Spin::Thread',
options => ['style-url|s=s'],
maximum => 2,
},
update => {
method => 'update',
module => 'App::DocKnot::Update',
options => ['metadata|m=s', 'output|o=s'],
maximum => 0,
},
'update-spin' => {
method => 'update_spin',
module => 'App::DocKnot::Update',
maximum => 1,
},
);
##############################################################################
# Option parsing
##############################################################################
# Parse command-line options and do any required error handling.
#
# $command - The command being run or undef for top-level options
# $options_ref - A reference to the options specification
# @args - The arguments to the command
#
# Returns: A list composed of a reference to a hash of options and values,
# followed by a reference to the remaining arguments after options
# have been extracted
# Throws: A text error message if the options are invalid
sub _parse_options {
my ($self, $command, $options_ref, @args) = @_;
# Use the object-oriented syntax to isolate configuration options from the
# rest of the program.
my $parser = Getopt::Long::Parser->new;
$parser->configure(qw(bundling no_ignore_case require_order));
# Parse the options and capture any errors, turning them into exceptions.
# The first letter of the Getopt::Long warning message will be capitalized
# but we want it to be lowercase to follow our error message standard.
my %opts;
{
my $error = 'option parsing failed';
local $SIG{__WARN__} = sub { ($error) = @_ };
if (!$parser->getoptionsfromarray(\@args, \%opts, $options_ref->@*)) {
$error =~ s{ \n+ \z }{}xms;
$error =~ s{ \A (\w) }{ lc($1) }xmse;
if ($command) {
die "$0 $command: $error\n";
} else {
die "$0: $error\n";
}
}
}
# Success. Return the options and the remaining arguments.
return (\%opts, \@args);
}
( run in 2.207 seconds using v1.01-cache-2.11-cpan-c966e8aa7e8 )