File Coverage

blib/lib/MooseX/App/Cmd/Command.pm
Criterion Covered Total %
statement 10 10 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 14 100.0


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