Factor stuff out into a script role, clean up all the script code
[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 namespace::autoclean;
6
7 requires 'run';
8
9 with 'MooseX::Getopt';
10
11 has application_name => (
12     traits => ['NoGetopt'],
13     isa => Str,
14     is => 'ro',
15     required => 1,
16 );
17
18 has help => (
19     traits => ['Getopt'],
20     cmd_aliases => 'h',
21     isa => Bool,
22     is => 'ro',
23     documentation => q{Display this help and exit},
24 );
25
26 sub _display_help {
27     my $self = shift;
28     pod2usage();
29     exit 0;
30 }
31
32 before run => sub {
33     my $self = shift;
34     $self->_display_help if $self->help;
35 };
36
37 sub run {
38     my $self = shift;
39     $self->_run_application;
40 }
41
42 sub _application_args {
43     ()
44 }
45
46 sub _run_application {
47     my $self = shift;
48     my $app = $self->application_name;
49     Class::MOP::load_class($app);
50     $app->run($self->_application_args);
51 }
52
53 1;