add -M option
[gitmo/MooseX-Runnable.git] / bin / mx-run
CommitLineData
c527660e 1#!/usr/bin/env perl
2
3use strict;
4use warnings;
5
441297a7 6use MooseX::Runnable::Run; # incidentally, we don't actually use this...
7
8exit run();
9
10sub run {
11 my ($includes, $plugins, $app, $argv) = parse_argv();
12
13 unshift @INC, $_ for @$includes;
14 help() unless $app;
15
c4e3f06e 16 local $0 = "mx-run ... $app";
17
441297a7 18 return MooseX::Runnable::Invocation->new(
19 class => $app,
20 plugins => $plugins || [],
21 )->run(@$argv);
22}
23
24sub 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 }
6a06028a 38 elsif ($arg =~ /^-M([^-]+)/){
39 my $module = $1;
40 eval "use $module";
41 die $@ if $@;
42 }
441297a7 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
63sub help {
64 print <<'END';
65
66This is mx-run, a utility for running MooseX::Runnable classes.
67usage: mx-run <mx-run options> Class::Name <options for Class::Name>
68mx-run options:
69
70 --help -? -h Print this message
441297a7 71 -I<path> Add <path> to @INC before loading Class::Name
6a06028a 72 -M<module> use <module> immediately
73 +PluginName Load PluginName (see MooseX::Runnable::Invocation)
74
75Note that as soon as +PluginName is seen, all -[IM] options are
76ignored by mx-run, and are instead processed by PluginName.
441297a7 77
78To get help for Class::Name, run:
79
80 mx-run Class::Name --help
81END
82
83 exit 1;
84}
c527660e 85
86__END__
87
88=head1 NAME
89
90mx-run - script to run MooseX::Runnable classes