add -M option
[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     local $0 = "mx-run ... $app";
17
18     return MooseX::Runnable::Invocation->new(
19         class   => $app,
20         plugins => $plugins || [],
21     )->run(@$argv);
22 }
23
24 sub parse_argv {
25     # we need to parse "incrementally" so we can identify:
26     # - our args (-Ilib, and --help, -h, and -?)
27     # - plugins to load (+Plugin)
28     # - the class name
29     # - the class' args
30     # code that's better than this is welcome!
31
32     my (@include, @plugins, $app);
33
34     while( my $arg = shift @ARGV ){
35         if ($arg =~ /^-I([^-]+)/){ # XXX: handle -I"quoted string" ?
36             push @include, $1;
37         }
38         elsif ($arg =~ /^-M([^-]+)/){
39             my $module = $1;
40             eval "use $module";
41             die $@ if $@;
42         }
43         elsif ($arg =~ /^\+\+?([A-Za-z:_]+)$/){ # second + is for +Foo::Bar
44             push @plugins, $1;
45         }
46         elsif ($arg =~ /^--([^-]+)$/){
47             help();
48         }
49         else {
50             if($arg =~ /^([A-Za-z:_]+)$/){
51                 $app = $arg;
52                 last;
53             }
54             else {
55                 help();
56             }
57         }
58     }
59
60     return \@include, \@plugins, $app, \@ARGV;
61 }
62
63 sub help {
64     print <<'END';
65
66 This is mx-run, a utility for running MooseX::Runnable classes.
67 usage: mx-run <mx-run options> Class::Name <options for Class::Name>
68 mx-run options:
69
70     --help -? -h     Print this message
71     -I<path>         Add <path> to @INC before loading Class::Name
72     -M<module>       use <module> immediately
73     +PluginName      Load PluginName (see MooseX::Runnable::Invocation)
74
75 Note that as soon as +PluginName is seen, all -[IM] options are
76 ignored by mx-run, and are instead processed by PluginName.
77
78 To get help for Class::Name, run:
79
80     mx-run Class::Name --help
81 END
82
83     exit 1;
84 }
85
86 __END__
87
88 =head1 NAME
89
90 mx-run - script to run MooseX::Runnable classes