Commit | Line | Data |
d3082fac |
1 | package Catalyst::ScriptRole; |
2 | use Moose::Role; |
3 | use MooseX::Types::Moose qw/Str Bool/; |
4 | use Pod::Usage; |
0e4038c6 |
5 | use MooseX::Getopt; |
d3082fac |
6 | use namespace::autoclean; |
7 | |
19529022 |
8 | with 'MooseX::Getopt' => { |
9 | excludes => [qw/ |
10 | _getopt_spec_warnings |
11 | _getopt_spec_exception |
2b8d5598 |
12 | _getopt_full_usage |
19529022 |
13 | /], |
14 | }; |
d3082fac |
15 | |
16 | has application_name => ( |
17 | traits => ['NoGetopt'], |
18 | isa => Str, |
19 | is => 'ro', |
20 | required => 1, |
21 | ); |
22 | |
23 | has help => ( |
24 | traits => ['Getopt'], |
d3082fac |
25 | isa => Bool, |
26 | is => 'ro', |
27 | documentation => q{Display this help and exit}, |
3a8c155f |
28 | cmd_aliases => ['?', 'h'], |
d3082fac |
29 | ); |
30 | |
19529022 |
31 | sub _getopt_spec_exception {} |
32 | |
33 | sub _getopt_spec_warnings { |
34 | shift; |
35 | warn @_; |
36 | } |
37 | |
2b8d5598 |
38 | sub _getopt_full_usage { |
d3082fac |
39 | my $self = shift; |
40 | pod2usage(); |
41 | exit 0; |
42 | } |
43 | |
44 | before run => sub { |
45 | my $self = shift; |
2b8d5598 |
46 | $self->_getopt_full_usage if $self->help; |
d3082fac |
47 | }; |
48 | |
49 | sub run { |
50 | my $self = shift; |
51 | $self->_run_application; |
52 | } |
53 | |
54 | sub _application_args { |
55 | () |
56 | } |
57 | |
58 | sub _run_application { |
59 | my $self = shift; |
60 | my $app = $self->application_name; |
61 | Class::MOP::load_class($app); |
62 | $app->run($self->_application_args); |
63 | } |
64 | |
65 | 1; |
1628b022 |
66 | |
67 | =head1 NAME |
68 | |
69 | Catalyst::ScriptRole - Common functionality for Catalyst scripts. |
70 | |
71 | =head1 SYNOPSIS |
72 | |
12aa6ca4 |
73 | package MyApp::Script::Foo; |
74 | use Moose; |
75 | use namespace::autoclean; |
111d3c9a |
76 | |
12aa6ca4 |
77 | with 'Catalyst::Script::Role'; |
111d3c9a |
78 | |
12aa6ca4 |
79 | sub _application_args { ... } |
111d3c9a |
80 | |
1628b022 |
81 | =head1 DESCRIPTION |
82 | |
12aa6ca4 |
83 | Role with the common functionality of Catalyst scripts. |
84 | |
85 | =head1 METHODS |
86 | |
87 | =head2 run |
88 | |
89 | The method invoked to run the application. |
90 | |
91 | =head1 ATTRIBUTES |
92 | |
93 | =head2 application_name |
94 | |
95 | The name of the application class, e.g. MyApp |
96 | |
97 | =head1 SEE ALSO |
98 | |
99 | L<Catalyst> |
100 | |
101 | L<MooseX::Getopt> |
1628b022 |
102 | |
103 | =head1 AUTHORS |
104 | |
105 | Catalyst Contributors, see Catalyst.pm |
106 | |
107 | =head1 COPYRIGHT |
108 | |
109 | This library is free software, you can redistribute it and/or modify |
110 | it under the same terms as Perl itself. |
111 | |
112 | =cut |
111d3c9a |
113 | |