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