1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
package MooseX::App::Cmd::Command;
our $VERSION = '0.34';
use Moose;
use Getopt::Long::Descriptive ();
use namespace::autoclean;
extends 'Moose::Object', 'App::Cmd::Command';
with 'MooseX::Getopt';
has usage => (
is => 'ro',
required => 1,
metaclass => 'NoGetopt',
isa => 'Object',
);
has app => (
is => 'ro',
required => 1,
metaclass => 'NoGetopt',
isa => 'MooseX::App::Cmd',
);
override _process_args => sub {
my ($class, $args) = @_;
local @ARGV = @{$args};
my $config_from_file;
if ($class->meta->does_role('MooseX::ConfigFromFile')) {
local @ARGV = @ARGV;
my $configfile;
my $opt_parser;
{
## no critic (Modules::RequireExplicitInclusion)
$opt_parser
= Getopt::Long::Parser->new( config => ['pass_through'] );
}
$opt_parser->getoptions( 'configfile=s' => \$configfile );
if (not defined $configfile
and $class->can('_get_default_configfile'))
{
$configfile = $class->_get_default_configfile();
}
if (defined $configfile) {
$config_from_file = $class->get_config_from_file($configfile);
}
}
my %processed = $class->_parse_argv(
params => { argv => \@ARGV },
options => [ $class->_attrs_to_options($config_from_file) ],
);
return (
$processed{params},
$processed{argv},
usage => $processed{usage},
# params from CLI are also fields in MooseX::Getopt
$config_from_file
? (%$config_from_file, %{ $processed{params} })
: %{ $processed{params} },
);
};
sub _usage_format { ## no critic (ProhibitUnusedPrivateSubroutines)
return shift->usage_desc;
}
## no critic (Modules::RequireExplicitInclusion)
__PACKAGE__->meta->make_immutable();
1;
# ABSTRACT: Base class for MooseX::Getopt based App::Cmd::Commands
__END__
=pod
=encoding UTF-8
=head1 NAME
MooseX::App::Cmd::Command - Base class for MooseX::Getopt based App::Cmd::Commands
=head1 VERSION
version 0.34
=head1 SYNOPSIS
use Moose;
extends qw(MooseX::App::Cmd::Command);
# no need to set opt_spec
# see MooseX::Getopt for documentation on how to specify options
has option_field => (
isa => 'Str',
is => 'rw',
required => 1,
);
sub execute {
my ( $self, $opts, $args ) = @_;
print $self->option_field; # also available in $opts->{option_field}
}
=head1 DESCRIPTION
This is a replacement base class for L<App::Cmd::Command|App::Cmd::Command>
classes that includes
L<MooseX::Getopt|MooseX::Getopt> and the glue to combine the two.
=head1 METHODS
=head2 _process_args
Replaces L<App::Cmd::Command|App::Cmd::Command>'s argument processing in favor
of L<MooseX::Getopt|MooseX::Getopt> based processing.
If your class does the L<MooseX::ConfigFromFile|MooseX::ConfigFromFile> role
(or any of its consuming roles like
L<MooseX::SimpleConfig|MooseX::SimpleConfig>), this will provide an additional
C<--configfile> command line option for loading options from a configuration
file.
=head1 SUPPORT
Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-App-Cmd>
(or L<bug-MooseX-App-Cmd@rt.cpan.org|mailto:bug-MooseX-App-Cmd@rt.cpan.org>).
There is also a mailing list available for users of this distribution, at
L<http://lists.perl.org/list/moose.html>.
There is also an irc channel available for users of this distribution, at
L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
=head1 AUTHOR
יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2008 by Infinity Interactive, Inc.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|