From: Jonathan Rockway Date: Tue, 31 Mar 2009 09:22:24 +0000 (-0500) Subject: sugaryify mx-run X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=441297a77c7b57b20a2948b040786d684236890d;p=gitmo%2FMooseX-Runnable.git sugaryify mx-run --- diff --git a/bin/mx-run b/bin/mx-run index d841427..3bad429 100644 --- a/bin/mx-run +++ b/bin/mx-run @@ -3,8 +3,74 @@ use strict; use warnings; -use MooseX::Runnable::Run; -&run_application(@ARGV); # the prototype is ($app, @args), but that's what ARGV is +use MooseX::Runnable::Run; # incidentally, we don't actually use this... + +exit run(); + +sub run { + my ($includes, $plugins, $app, $argv) = parse_argv(); + + unshift @INC, $_ for @$includes; + help() unless $app; + + return MooseX::Runnable::Invocation->new( + class => $app, + plugins => $plugins || [], + )->run(@$argv); +} + +sub parse_argv { + # we need to parse "incrementally" so we can identify: + # - our args (-Ilib, and --help, -h, and -?) + # - plugins to load (+Plugin) + # - the class name + # - the class' args + # code that's better than this is welcome! + + my (@include, @plugins, $app); + + while( my $arg = shift @ARGV ){ + if ($arg =~ /^-I([^-]+)/){ # XXX: handle -I"quoted string" ? + push @include, $1; + } + elsif ($arg =~ /^\+\+?([A-Za-z:_]+)$/){ # second + is for +Foo::Bar + push @plugins, $1; + } + elsif ($arg =~ /^--([^-]+)$/){ + help(); + } + else { + if($arg =~ /^([A-Za-z:_]+)$/){ + $app = $arg; + last; + } + else { + help(); + } + } + } + + return \@include, \@plugins, $app, \@ARGV; +} + +sub help { + print <<'END'; + +This is mx-run, a utility for running MooseX::Runnable classes. +usage: mx-run Class::Name +mx-run options: + + --help -? -h Print this message + +PluginName Load PluginName (see MooseX::Runnable::Invocation) + -I Add to @INC before loading Class::Name + +To get help for Class::Name, run: + + mx-run Class::Name --help +END + + exit 1; +} __END__