sugaryify mx-run
[gitmo/MooseX-Runnable.git] / bin / mx-run
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5
6 use MooseX::Runnable::Run; # incidentally, we don't actually use this...
7
8 exit run();
9
10 sub run {
11     my ($includes, $plugins, $app, $argv) = parse_argv();
12
13     unshift @INC, $_ for @$includes;
14     help() unless $app;
15
16     return MooseX::Runnable::Invocation->new(
17         class   => $app,
18         plugins => $plugins || [],
19     )->run(@$argv);
20 }
21
22 sub parse_argv {
23     # we need to parse "incrementally" so we can identify:
24     # - our args (-Ilib, and --help, -h, and -?)
25     # - plugins to load (+Plugin)
26     # - the class name
27     # - the class' args
28     # code that's better than this is welcome!
29
30     my (@include, @plugins, $app);
31
32     while( my $arg = shift @ARGV ){
33         if ($arg =~ /^-I([^-]+)/){ # XXX: handle -I"quoted string" ?
34             push @include, $1;
35         }
36         elsif ($arg =~ /^\+\+?([A-Za-z:_]+)$/){ # second + is for +Foo::Bar
37             push @plugins, $1;
38         }
39         elsif ($arg =~ /^--([^-]+)$/){
40             help();
41         }
42         else {
43             if($arg =~ /^([A-Za-z:_]+)$/){
44                 $app = $arg;
45                 last;
46             }
47             else {
48                 help();
49             }
50         }
51     }
52
53     return \@include, \@plugins, $app, \@ARGV;
54 }
55
56 sub help {
57     print <<'END';
58
59 This is mx-run, a utility for running MooseX::Runnable classes.
60 usage: mx-run <mx-run options> Class::Name <options for Class::Name>
61 mx-run options:
62
63     --help -? -h     Print this message
64     +PluginName      Load PluginName (see MooseX::Runnable::Invocation)
65     -I<path>         Add <path> to @INC before loading Class::Name
66
67 To get help for Class::Name, run:
68
69     mx-run Class::Name --help
70 END
71
72     exit 1;
73 }
74
75 __END__
76
77 =head1 NAME
78
79 mx-run - script to run MooseX::Runnable classes