Remove most of the horrible
[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 => q{Display this help and exit},
28 );
29
30 sub _getopt_spec_exception {}
31
32 sub _getopt_spec_warnings {
33     shift;
34     warn @_;
35 }
36
37 sub _getopt_full_usage {
38     my $self = shift;
39     pod2usage();
40     exit 0;
41 }
42
43 before run => sub {
44     my $self = shift;
45     $self->_getopt_full_usage if $self->help;
46 };
47
48 sub run {
49     my $self = shift;
50     $self->_run_application;
51 }
52
53 sub _application_args {
54     ()
55 }
56
57 sub _run_application {
58     my $self = shift;
59     my $app = $self->application_name;
60     Class::MOP::load_class($app);
61     $app->run($self->_application_args);
62 }
63
64 1;
65
66 =head1 NAME
67
68 Catalyst::ScriptRole - Common functionality for Catalyst scripts.
69
70 =head1 SYNOPSIS
71
72     package MyApp::Script::Foo;
73     use Moose;
74     use namespace::autoclean;
75
76     with 'Catalyst::Script::Role';
77
78      sub _application_args { ... }
79
80 =head1 DESCRIPTION
81
82 Role with the common functionality of Catalyst scripts.
83
84 =head1 METHODS
85
86 =head2 run
87
88 The method invoked to run the application.
89
90 =head1 ATTRIBUTES
91
92 =head2 application_name
93
94 The name of the application class, e.g. MyApp
95
96 =head1 SEE ALSO
97
98 L<Catalyst>
99
100 L<MooseX::Getopt>
101
102 =head1 AUTHORS
103
104 Catalyst Contributors, see Catalyst.pm
105
106 =head1 COPYRIGHT
107
108 This library is free software, you can redistribute it and/or modify
109 it under the same terms as Perl itself.
110
111 =cut
112