Chomp trailing space
[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;
5use namespace::autoclean;
6
d3082fac 7with 'MooseX::Getopt';
8
9has application_name => (
10 traits => ['NoGetopt'],
11 isa => Str,
12 is => 'ro',
13 required => 1,
14);
15
16has help => (
17 traits => ['Getopt'],
d3082fac 18 isa => Bool,
19 is => 'ro',
20 documentation => q{Display this help and exit},
21);
22
4f0612fd 23sub _exit_with_usage {
d3082fac 24 my $self = shift;
25 pod2usage();
26 exit 0;
27}
28
29before run => sub {
30 my $self = shift;
4f0612fd 31 $self->_exit_with_usage if $self->help;
d3082fac 32};
33
34sub run {
35 my $self = shift;
36 $self->_run_application;
37}
38
39sub _application_args {
40 ()
41}
42
43sub _run_application {
44 my $self = shift;
45 my $app = $self->application_name;
46 Class::MOP::load_class($app);
47 $app->run($self->_application_args);
48}
49
4f0612fd 50# GROSS HACK, temporary until MX::Getopt gets some proper refactoring and unfucking..
51around '_parse_argv' => sub {
52 my ($orig, $self, @args) = @_;
53 my %data = eval { $self->$orig(@args) };
54 $self->_exit_with_usage($@) if $@;
55 $data{usage} = Catalyst::ScriptRole::Useage->new(code => sub { shift; $self->_exit_with_usage(@_) });
56 return %data;
57};
58
59# This package is going away.
60package # Hide from PAUSE
61 Catalyst::ScriptRole::Useage;
62use Moose;
63use namespace::autoclean;
64
65has code => ( is => 'ro', required => 1 );
66
67sub die { shift->code->(@_) }
68
d3082fac 691;
1628b022 70
71=head1 NAME
72
73Catalyst::ScriptRole - Common functionality for Catalyst scripts.
74
75=head1 SYNOPSIS
76
12aa6ca4 77 package MyApp::Script::Foo;
78 use Moose;
79 use namespace::autoclean;
111d3c9a 80
12aa6ca4 81 with 'Catalyst::Script::Role';
111d3c9a 82
12aa6ca4 83 sub _application_args { ... }
111d3c9a 84
1628b022 85=head1 DESCRIPTION
86
12aa6ca4 87Role with the common functionality of Catalyst scripts.
88
89=head1 METHODS
90
91=head2 run
92
93The method invoked to run the application.
94
95=head1 ATTRIBUTES
96
97=head2 application_name
98
99The name of the application class, e.g. MyApp
100
101=head1 SEE ALSO
102
103L<Catalyst>
104
105L<MooseX::Getopt>
1628b022 106
107=head1 AUTHORS
108
109Catalyst Contributors, see Catalyst.pm
110
111=head1 COPYRIGHT
112
113This library is free software, you can redistribute it and/or modify
114it under the same terms as Perl itself.
115
116=cut
111d3c9a 117