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