line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ============================================================================« |
2
|
|
|
|
|
|
|
package MooseX::App::Simple; |
3
|
|
|
|
|
|
|
# ============================================================================« |
4
|
|
|
|
|
|
|
|
5
|
6
|
|
|
6
|
|
630235
|
use 5.010; |
|
6
|
|
|
|
|
72
|
|
6
|
6
|
|
|
6
|
|
3063
|
use utf8; |
|
6
|
|
|
|
|
74
|
|
|
6
|
|
|
|
|
33
|
|
7
|
6
|
|
|
6
|
|
183
|
use strict; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
113
|
|
8
|
6
|
|
|
6
|
|
29
|
use warnings; |
|
6
|
|
|
|
|
63
|
|
|
6
|
|
|
|
|
343
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:MAROS'; |
11
|
|
|
|
|
|
|
our $VERSION = '1.42'; |
12
|
|
|
|
|
|
|
|
13
|
6
|
|
|
6
|
|
2893
|
use Moose::Exporter; |
|
6
|
|
|
|
|
713345
|
|
|
6
|
|
|
|
|
53
|
|
14
|
6
|
|
|
6
|
|
2931
|
use MooseX::App::Exporter qw(app_usage app_description app_base app_fuzzy app_strict app_prefer_commandline app_permute option parameter command_short_description command_long_description command_usage command_strict); |
|
6
|
|
|
|
|
150
|
|
|
6
|
|
|
|
|
75
|
|
15
|
6
|
|
|
6
|
|
3472
|
use MooseX::App::Meta::Role::Attribute::Option; |
|
6
|
|
|
|
|
22
|
|
|
6
|
|
|
|
|
228
|
|
16
|
6
|
|
|
6
|
|
2661
|
use MooseX::App::Message::Envelope; |
|
6
|
|
|
|
|
22
|
|
|
6
|
|
|
|
|
293
|
|
17
|
6
|
|
|
6
|
|
48
|
use Scalar::Util qw(blessed); |
|
6
|
|
|
|
|
14
|
|
|
6
|
|
|
|
|
3094
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my ($IMPORT,$UNIMPORT,$INIT_META) = Moose::Exporter->build_import_methods( |
20
|
|
|
|
|
|
|
with_meta => [ qw(app_usage app_description app_base app_fuzzy app_strict app_permute option parameter command_short_description command_long_description command_usage command_strict) ], |
21
|
|
|
|
|
|
|
also => [ 'Moose' ], |
22
|
|
|
|
|
|
|
as_is => [ 'new_with_options' ], |
23
|
|
|
|
|
|
|
install => [ 'unimport', 'init_meta' ], |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub import { |
27
|
6
|
|
|
6
|
|
63
|
my ( $class, @plugins ) = @_; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# Get caller |
30
|
6
|
|
|
|
|
29
|
my ($caller_class) = caller(); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# Process plugins |
33
|
6
|
|
|
|
|
175
|
MooseX::App::Exporter->process_plugins($caller_class,@plugins); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Call Moose-Exporter generated importer |
36
|
6
|
|
|
|
|
33
|
return $class->$IMPORT( { into => $caller_class } ); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub init_meta { |
40
|
6
|
|
|
6
|
0
|
650
|
my ($class,%args) = @_; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# Get required roles and metaroles |
43
|
6
|
|
|
|
|
21
|
$args{roles} = ['MooseX::App::Role::Base' ]; |
44
|
|
|
|
|
|
|
$args{metaroles} = { |
45
|
6
|
|
|
|
|
53
|
class => [ |
46
|
|
|
|
|
|
|
'MooseX::App::Meta::Role::Class::Base', |
47
|
|
|
|
|
|
|
'MooseX::App::Meta::Role::Class::Simple', |
48
|
|
|
|
|
|
|
'MooseX::App::Meta::Role::Class::Documentation' |
49
|
|
|
|
|
|
|
], |
50
|
|
|
|
|
|
|
attribute => [ |
51
|
|
|
|
|
|
|
'MooseX::App::Meta::Role::Attribute::Option' |
52
|
|
|
|
|
|
|
], |
53
|
|
|
|
|
|
|
}; |
54
|
6
|
|
|
|
|
57
|
my $meta = MooseX::App::Exporter->process_init_meta(%args); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# Register only one command |
57
|
6
|
|
|
|
|
242
|
$meta->app_commands({ 'self' => $args{for_class} }); |
58
|
|
|
|
|
|
|
|
59
|
6
|
|
|
|
|
58
|
return $meta; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub new_with_options { |
63
|
14
|
|
|
14
|
1
|
5461
|
my ($class,@args) = @_; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Sanity check |
66
|
14
|
50
|
33
|
|
|
109
|
Moose->throw_error('new_with_options is a class method') |
67
|
|
|
|
|
|
|
if ! defined $class || blessed($class); |
68
|
|
|
|
|
|
|
|
69
|
14
|
|
|
|
|
31
|
my %args; |
70
|
14
|
100
|
66
|
|
|
100
|
if (scalar @args == 1 |
|
|
50
|
|
|
|
|
|
71
|
|
|
|
|
|
|
&& ref($args[0]) eq 'HASH' ) { |
72
|
1
|
|
|
|
|
3
|
%args = %{$args[0]}; |
|
1
|
|
|
|
|
6
|
|
73
|
|
|
|
|
|
|
} elsif (scalar @args % 2 == 0) { |
74
|
13
|
|
|
|
|
39
|
%args = @args; |
75
|
|
|
|
|
|
|
} else { |
76
|
0
|
|
|
|
|
0
|
Moose->throw_error('new_with_command got invalid extra arguments'); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
$class->meta->command_check() |
80
|
14
|
50
|
33
|
|
|
192
|
if $ENV{APP_DEVELOPER} || $ENV{HARNESS_ACTIVE}; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Get ARGV |
83
|
13
|
|
|
|
|
43
|
my $argv = delete $args{ARGV}; |
84
|
13
|
|
|
|
|
26
|
my $parsed_argv; |
85
|
13
|
50
|
|
|
|
55
|
if (defined $argv) { |
86
|
0
|
|
|
|
|
0
|
$parsed_argv = MooseX::App::ParsedArgv->new( argv => $argv ); |
87
|
|
|
|
|
|
|
} else { |
88
|
13
|
|
|
|
|
119
|
$parsed_argv = MooseX::App::ParsedArgv->instance(); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
13
|
|
|
|
|
131
|
return $class->initialize_command_class($class,%args); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
6
|
|
|
6
|
|
60
|
no Moose; |
|
6
|
|
|
|
|
26
|
|
|
6
|
|
|
|
|
47
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
__END__ |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=encoding utf8 |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 NAME |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
MooseX::App::Simple - Single command applications |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 SYNOPSIS |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
package MyApp; |
108
|
|
|
|
|
|
|
use MooseX::App::Simple qw(Config Color); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
parameter 'param' => ( |
111
|
|
|
|
|
|
|
is => 'rw', |
112
|
|
|
|
|
|
|
isa => 'Str', |
113
|
|
|
|
|
|
|
documentation => q[First parameter], |
114
|
|
|
|
|
|
|
required => 1, |
115
|
|
|
|
|
|
|
); # Positional parameter |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
option 'my_option' => ( |
118
|
|
|
|
|
|
|
is => 'rw', |
119
|
|
|
|
|
|
|
isa => 'Bool', |
120
|
|
|
|
|
|
|
documentation => q[Enable this to do fancy stuff], |
121
|
|
|
|
|
|
|
); # Option (--my_option) |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
has 'private' => ( |
124
|
|
|
|
|
|
|
is => 'rw', |
125
|
|
|
|
|
|
|
); # not exposed |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub run { |
128
|
|
|
|
|
|
|
my ($self) = @_; |
129
|
|
|
|
|
|
|
# Do something |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
And then in some simple wrapper script: |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
#!/usr/bin/env perl |
135
|
|
|
|
|
|
|
use MyApp; |
136
|
|
|
|
|
|
|
MyApp->new_with_options->run; |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 DESCRIPTION |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
MooseX::App::Simple works basically just as MooseX::App, however it does |
141
|
|
|
|
|
|
|
not search for commands and assumes that you have all options and parameters |
142
|
|
|
|
|
|
|
defined in the current class. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Read the L<Tutorial|MooseX::App::Tutorial> for getting started with a simple |
145
|
|
|
|
|
|
|
MooseX::App command line application. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 METHODS |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head2 new_with_options |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
my $myapp_command = MyApp->new_with_options(); |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
This method reads the command line arguments from the user and tries to create |
154
|
|
|
|
|
|
|
instantiate the current class with the ARGV-input. If it fails it returns a |
155
|
|
|
|
|
|
|
L<MooseX::App::Message::Envelope> object holding an error message. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
You can pass a hash or hashref of default params to new_with_options |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
MyApp->new_with_options( %default ); |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Optionally you can pass a custom ARGV to this constructor |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
my $obj = MyApp->new_with_options( ARGV => \@myARGV ); |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
However, if you do so you must take care of propper @ARGV encoding yourself. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head1 OPTIONS |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
Same as in L<MooseX::App>, however all options regarding command loading such |
170
|
|
|
|
|
|
|
as L<app_namespace> and L<app_exclude> are not available. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 PLUGINS |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Same as in L<MooseX::App>. However plugings adding additional commands |
175
|
|
|
|
|
|
|
(eg. version) will have no effect with MooseX::App::Simple. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head1 SEE ALSO |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Read the L<Tutorial|MooseX::App::Tutorial> for getting started with a simple |
180
|
|
|
|
|
|
|
MooseX::App command line application. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
See L<MooseX::Getopt> and L<MooX::Options> for alternatives |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=cut |