fix usage line in MX::Getopt scripts
[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 =~ /^\+\+?([A-Za-z:_]+)$/){ # second + is for +Foo::Bar
39             push @plugins, $1;
40         }
41         elsif ($arg =~ /^--([^-]+)$/){
42             help();
43         }
44         else {
45             if($arg =~ /^([A-Za-z:_]+)$/){
46                 $app = $arg;
47                 last;
48             }
49             else {
50                 help();
51             }
52         }
53     }
54
55     return \@include, \@plugins, $app, \@ARGV;
56 }
57
58 sub help {
59     print <<'END';
60
61 This is mx-run, a utility for running MooseX::Runnable classes.
62 usage: mx-run <mx-run options> Class::Name <options for Class::Name>
63 mx-run options:
64
65     --help -? -h     Print this message
66     +PluginName      Load PluginName (see MooseX::Runnable::Invocation)
67     -I<path>         Add <path> to @INC before loading Class::Name
68
69 To get help for Class::Name, run:
70
71     mx-run Class::Name --help
72 END
73
74     exit 1;
75 }
76
77 __END__
78
79 =head1 NAME
80
81 mx-run - script to run MooseX::Runnable classes