skeleton CLI support
[catagits/Web-Simple.git] / lib / Web / Simple / Application.pm
index c24ac79..3bdbd97 100644 (file)
@@ -27,7 +27,7 @@ use warnings FATAL => 'all';
     my ($self, $env, @args) = @_;
     my $next = $self->_has_match ? $self->next : undef;
     if (my ($env_delta, @match) = $self->_match_against($env)) {
-      if (my ($result) = $self->_execute_with(@args, @match)) {
+      if (my ($result) = $self->_execute_with(@args, @match, $env)) {
         if ($self->_is_dispatcher($result)) {
           $next = $result->set_next($next);
           $env = { %$env, %$env_delta };
@@ -167,14 +167,14 @@ sub _construct_subdispatch {
     my $chain = $class->_build_dispatch_chain(@res);
     return $class->_build_dispatcher({
       call => sub {
-        my ($d, $self, $env) = (shift, shift, shift);
+        my ($d, $self, $env) = (shift, shift, shift); pop; # lose trailing $env
         return $chain->dispatch($env, $self, @_);
       }
     });
   });
   return $class->_build_dispatcher({
     call => sub {
-      my ($d, $self, $env) = (shift, shift, shift);
+      my ($d, $self, $env) = (shift, shift, shift); pop; # lose trailing $env
       my @sub = $disp->dispatch($env, $self, @_);
       return @sub if @sub;
       return unless (my $next = $d->next);
@@ -191,7 +191,7 @@ sub _build_dispatcher_from_spec {
   my $matcher = (
     defined($proto) && length($proto)
       ? $parser->parse_dispatch_specification($proto)
-      : undef
+      : sub { ({}, $_[1]) }
   );
   return $class->_build_dispatcher({
     match => $matcher,
@@ -271,7 +271,11 @@ sub run {
   } elsif ($ENV{GATEWAY_INTERFACE}) {
     return $self->_run_cgi;
   }
-  my $path = shift(@ARGV) or die "No path passed - use $0 / for root";
+  unless (@ARGV && $ARGV[0] =~ m{^/}) {
+    return $self->_run_cli;
+  }
+
+  my $path = shift @ARGV;
 
   require HTTP::Request::Common;
   require Plack::Test;
@@ -283,4 +287,16 @@ sub run {
   print $response->as_string;
 }
 
+sub _run_cli {
+  my $self = shift;
+  die $self->_cli_usage;
+}
+
+sub _cli_usage {
+  "To run this script in CGI test mode, pass a URL path beginning with /:\n".
+  "\n".
+  "  $0 /some/path\n".
+  "  $0 /\n"
+}
+
 1;