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