line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ============================================================================« |
2
|
|
|
|
|
|
|
package MooseX::App::Simple; |
3
|
|
|
|
|
|
|
# ============================================================================« |
4
|
|
|
|
|
|
|
|
5
|
6
|
|
|
6
|
|
548375
|
use 5.010; |
|
6
|
|
|
|
|
68
|
|
6
|
6
|
|
|
6
|
|
2697
|
use utf8; |
|
6
|
|
|
|
|
71
|
|
|
6
|
|
|
|
|
32
|
|
7
|
6
|
|
|
6
|
|
169
|
use strict; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
104
|
|
8
|
6
|
|
|
6
|
|
27
|
use warnings; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
293
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:MAROS'; |
11
|
|
|
|
|
|
|
our $VERSION = '1.41'; |
12
|
|
|
|
|
|
|
|
13
|
6
|
|
|
6
|
|
2540
|
use Moose::Exporter; |
|
6
|
|
|
|
|
659789
|
|
|
6
|
|
|
|
|
41
|
|
14
|
6
|
|
|
6
|
|
2830
|
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
|
|
|
|
|
112
|
|
|
6
|
|
|
|
|
64
|
|
15
|
6
|
|
|
6
|
|
3186
|
use MooseX::App::Meta::Role::Attribute::Option; |
|
6
|
|
|
|
|
19
|
|
|
6
|
|
|
|
|
219
|
|
16
|
6
|
|
|
6
|
|
2330
|
use MooseX::App::Message::Envelope; |
|
6
|
|
|
|
|
20
|
|
|
6
|
|
|
|
|
250
|
|
17
|
6
|
|
|
6
|
|
50
|
use Scalar::Util qw(blessed); |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
2785
|
|
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
|
|
56
|
my ( $class, @plugins ) = @_; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# Get caller |
30
|
6
|
|
|
|
|
24
|
my ($caller_class) = caller(); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# Process plugins |
33
|
6
|
|
|
|
|
184
|
MooseX::App::Exporter->process_plugins($caller_class,@plugins); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Call Moose-Exporter generated importer |
36
|
6
|
|
|
|
|
27
|
return $class->$IMPORT( { into => $caller_class } ); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub init_meta { |
40
|
6
|
|
|
6
|
0
|
575
|
my ($class,%args) = @_; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# Get required roles and metaroles |
43
|
6
|
|
|
|
|
16
|
$args{roles} = ['MooseX::App::Role::Base' ]; |
44
|
|
|
|
|
|
|
$args{metaroles} = { |
45
|
6
|
|
|
|
|
29
|
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
|
|
|
|
|
50
|
my $meta = MooseX::App::Exporter->process_init_meta(%args); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# Register only one command |
57
|
6
|
|
|
|
|
241
|
$meta->app_commands({ 'self' => $args{for_class} }); |
58
|
|
|
|
|
|
|
|
59
|
6
|
|
|
|
|
56
|
return $meta; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub new_with_options { |
63
|
14
|
|
|
14
|
1
|
5376
|
my ($class,@args) = @_; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Sanity check |
66
|
14
|
50
|
33
|
|
|
100
|
Moose->throw_error('new_with_options is a class method') |
67
|
|
|
|
|
|
|
if ! defined $class || blessed($class); |
68
|
|
|
|
|
|
|
|
69
|
14
|
|
|
|
|
29
|
my %args; |
70
|
14
|
100
|
66
|
|
|
96
|
if (scalar @args == 1 |
|
|
50
|
|
|
|
|
|
71
|
|
|
|
|
|
|
&& ref($args[0]) eq 'HASH' ) { |
72
|
1
|
|
|
|
|
2
|
%args = %{$args[0]}; |
|
1
|
|
|
|
|
4
|
|
73
|
|
|
|
|
|
|
} elsif (scalar @args % 2 == 0) { |
74
|
13
|
|
|
|
|
42
|
%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
|
|
|
140
|
if $ENV{APP_DEVELOPER} || $ENV{HARNESS_ACTIVE}; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Get ARGV |
83
|
13
|
|
|
|
|
43
|
my $argv = delete $args{ARGV}; |
84
|
13
|
|
|
|
|
25
|
my $parsed_argv; |
85
|
13
|
50
|
|
|
|
36
|
if (defined $argv) { |
86
|
0
|
|
|
|
|
0
|
$parsed_argv = MooseX::App::ParsedArgv->new( argv => $argv ); |
87
|
|
|
|
|
|
|
} else { |
88
|
13
|
|
|
|
|
87
|
$parsed_argv = MooseX::App::ParsedArgv->instance(); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
13
|
|
|
|
|
116
|
return $class->initialize_command_class($class,%args); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
6
|
|
|
6
|
|
55
|
no Moose; |
|
6
|
|
|
|
|
17
|
|
|
6
|
|
|
|
|
43
|
|
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> |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head1 PLUGINS |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Same as in L<MooseX::App>. However plugings adding commands (eg. version) |
174
|
|
|
|
|
|
|
will not work with MooseX::App::Simple. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 SEE ALSO |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Read the L<Tutorial|MooseX::App::Tutorial> for getting started with a simple |
179
|
|
|
|
|
|
|
MooseX::App command line application. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
See L<MooseX::Getopt> and L<MooX::Options> for alternatives |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=cut |