Tidy things up a little and fix pod usage.
[catagits/Catalyst-Runtime.git] / lib / Catalyst / ScriptRole.pm
1 package Catalyst::ScriptRole;
2 use Moose::Role;
3 use MooseX::Types::Moose qw/Str Bool/;
4 use Pod::Usage;
5 use MooseX::Getopt;
6 use namespace::autoclean;
7
8 with 'MooseX::Getopt' => {
9     excludes => [qw/
10         _getopt_spec_warnings
11         _getopt_spec_exception
12         _getopt_full_usage
13     /],
14 };
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'],
25     isa           => Bool,
26     is            => 'ro',
27     documentation => 'Display this help and exit',
28     cmd_aliases   => ['?', 'h'],
29 );
30
31 sub _getopt_spec_exception {}
32
33 sub _getopt_spec_warnings {
34     shift;
35     warn @_;
36 }
37
38 sub _getopt_full_usage {
39     my $self = shift;
40     pod2usage();
41     exit 0;
42 }
43
44 before run => sub {
45     my $self = shift;
46     $self->_getopt_full_usage if $self->help;
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;
66
67 =head1 NAME
68
69 Catalyst::ScriptRole - Common functionality for Catalyst scripts.
70
71 =head1 SYNOPSIS
72
73     package MyApp::Script::Foo;
74     use Moose;
75     use namespace::autoclean;
76
77     with 'Catalyst::ScriptRole';
78
79     sub _application_args { ... }
80
81 =head1 DESCRIPTION
82
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>
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