From: Jonathan Rockway Date: Thu, 25 Jun 2009 21:41:31 +0000 (-0700) Subject: add ability to generate commandline from parsed arg object X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2f7a7ff66e024179f4e0eb1a37376030dac352c2;hp=86c248d88a17d0e3a2a4a492231a5315a44ddbbb;p=gitmo%2FMooseX-Runnable.git add ability to generate commandline from parsed arg object --- diff --git a/lib/MooseX/Runnable/Util/ArgParser.pm b/lib/MooseX/Runnable/Util/ArgParser.pm index 1835103..833f983 100644 --- a/lib/MooseX/Runnable/Util/ArgParser.pm +++ b/lib/MooseX/Runnable/Util/ArgParser.pm @@ -4,6 +4,8 @@ use MooseX::Types::Moose qw(HashRef ArrayRef Str Bool); use MooseX::Types::Path::Class qw(Dir); use List::MoreUtils qw(first_index); +use FindBin; + use namespace::autoclean -also => ['_look_for_dash_something', '_delete_first']; has 'argv' => ( @@ -220,6 +222,34 @@ sub _build_app_args { return [@args]; } +# XXX: bad +sub guess_cmdline { + my ($self, %opts) = @_; + + confess 'Parser is help' if $self->is_help; + + my @perl_flags = @{$opts{perl_flags} || []}; + my @without_plugins = @{$opts{without_plugins} || []}; + + # invoke mx-run + my @cmdline = ($^X, @perl_flags, $FindBin::Bin.'/'.$FindBin::Script); + push @cmdline, map { "-I$_" } $self->include_paths; + push @cmdline, map { "-M$_" } $self->modules; + + p: + for my $plugin (keys %{$self->plugins}){ + for my $without (@without_plugins) { + next p if $without eq $plugin; + } + push @cmdline, "+$plugin", @{$self->plugins->{$plugin} || []}; + } + push @cmdline, '--'; + push @cmdline, $self->class_name; + push @cmdline, $self->app_args; + + return @cmdline; +} + 1; __END__ diff --git a/t/reverse-args.t b/t/reverse-args.t new file mode 100644 index 0000000..4b0086e --- /dev/null +++ b/t/reverse-args.t @@ -0,0 +1,23 @@ +use strict; +use warnings; +use Test::More tests => 1; + +use MooseX::Runnable::Util::ArgParser; + +my $str = '-MFoo -Ilib -MBar +Plugout +Plugin --with-args -- MyApp --with args'; + +my $args = MooseX::Runnable::Util::ArgParser->new( + argv => [split ' ', $str], +); + +local $^X = '/path/to/perl'; +local $FindBin::Bin = '/path/to'; +local $FindBin::Script = 'mx-run'; +my @cmdline = $args->guess_cmdline( + perl_flags => ['--X--'], + without_plugins => ['Plugout'], +); + +is join(' ', @cmdline), + "/path/to/perl --X-- /path/to/mx-run -Ilib -MFoo -MBar +Plugin --with-args -- MyApp --with args", + 'cmdline reverses reasonably';