Factor restarter arg assembly out into it's own routine for ease of testing. Use...
[catagits/Catalyst-Runtime.git] / lib / Catalyst / ScriptRole.pm
CommitLineData
d3082fac 1package Catalyst::ScriptRole;
2use Moose::Role;
3use MooseX::Types::Moose qw/Str Bool/;
4use Pod::Usage;
0e4038c6 5use MooseX::Getopt;
d3082fac 6use namespace::autoclean;
7
19529022 8with 'MooseX::Getopt' => {
9 excludes => [qw/
10 _getopt_spec_warnings
11 _getopt_spec_exception
2b8d5598 12 _getopt_full_usage
19529022 13 /],
14};
d3082fac 15
16has application_name => (
17 traits => ['NoGetopt'],
18 isa => Str,
19 is => 'ro',
20 required => 1,
21);
22
23has 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 31sub _getopt_spec_exception {}
32
33sub _getopt_spec_warnings {
34 shift;
35 warn @_;
36}
37
2b8d5598 38sub _getopt_full_usage {
d3082fac 39 my $self = shift;
40 pod2usage();
41 exit 0;
42}
43
44before run => sub {
45 my $self = shift;
2b8d5598 46 $self->_getopt_full_usage if $self->help;
d3082fac 47};
48
49sub run {
50 my $self = shift;
51 $self->_run_application;
52}
53
54sub _application_args {
55 ()
56}
57
58sub _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
651;
1628b022 66
67=head1 NAME
68
69Catalyst::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 83Role with the common functionality of Catalyst scripts.
84
85=head1 METHODS
86
87=head2 run
88
89The method invoked to run the application.
90
91=head1 ATTRIBUTES
92
93=head2 application_name
94
95The name of the application class, e.g. MyApp
96
97=head1 SEE ALSO
98
99L<Catalyst>
100
101L<MooseX::Getopt>
1628b022 102
103=head1 AUTHORS
104
105Catalyst Contributors, see Catalyst.pm
106
107=head1 COPYRIGHT
108
109This library is free software, you can redistribute it and/or modify
110it under the same terms as Perl itself.
111
112=cut
111d3c9a 113