[Bps-public-commit] Prophet branch, config-gitlike, updated. 8defa13a5d4162cb9f95bf9046b570f4659cab7c

spang at bestpractical.com spang at bestpractical.com
Fri Jun 26 16:57:30 EDT 2009


The branch, config-gitlike has been updated
       via  8defa13a5d4162cb9f95bf9046b570f4659cab7c (commit)
      from  4c7aba772f3f9d30ac6fe43569ceaed190aa709a (commit)

Summary of changes:
 lib/Prophet/CLI/Dispatcher.pm |   30 +++++++++++++++-----------
 lib/Prophet/CLIContext.pm     |   46 ++++++++++++++++++++++++++++++----------
 t/cli-arg-parsing.t           |   11 +--------
 t/cli.t                       |   19 +++++++++++++++-
 4 files changed, 70 insertions(+), 36 deletions(-)

- Log -----------------------------------------------------------------
commit 8defa13a5d4162cb9f95bf9046b570f4659cab7c
Author: Christine Spang <spang at mit.edu>
Date:   Fri Jun 26 22:32:17 2009 +0300

    Only pull a record ID from the end of primary_commands for specific commands, not all of them.
    
    Otherwise, commands that sometimes want the last part of their command
    string to look like an id/uuid but don't actually want the
    turn-this-into-my-command-uuid magic will hate you.

diff --git a/lib/Prophet/CLI/Dispatcher.pm b/lib/Prophet/CLI/Dispatcher.pm
index fedc6b3..e16cabf 100644
--- a/lib/Prophet/CLI/Dispatcher.pm
+++ b/lib/Prophet/CLI/Dispatcher.pm
@@ -1,19 +1,13 @@
 package Prophet::CLI::Dispatcher;
 use Path::Dispatcher::Declarative -base;
 use Any::Moose;
+require Prophet::CLIContext;
 
 with 'Prophet::CLI::Parameters';
 
 our @PREFIXES = qw(Prophet::CLI::Command);
 sub add_command_prefix { unshift @PREFIXES, @_ }
 
-# "ticket display $ID" -> "ticket display --id=$ID"
-on qr{^ (.*) \s+ ( \d+ | [0-9a-zA-Z\-\_]{22} | [A-Z0-9]{36} ) $ }x => sub {
-    my $self = shift;
-    $self->context->set_arg(id => $2);
-    run($1, $self, @_);
-};
-
 on '' => sub {
     my $self = shift;
     if ($self->context->has_arg('version')) { run_command("Version")->($self) }
@@ -36,16 +30,27 @@ on qr{^(clone|pull) (\S+)$} => sub {
 };
 
 # log range => log --range range
-on qr{log\s*([0-9LATEST.~]+)} => sub {
+on qr{log\s+([0-9LATEST.~]+)} => sub {
     my $self = shift;
     $self->context->set_arg(range => $1);
     run('log', $self);
 };
 
+on [ qr/^(update|edit|show|display|delete|del|rm|history)$/,
+     qr/^$Prophet::CLIContext::ID_REGEX$/ ] => sub {
+    my $self = shift;
+    $self->context->set_id_from_primary_commands;
+    run($1, $self, @_);
+};
+
+on [ [ 'update', 'edit' ] ]      => run_command("Update");
+on [ [ 'show', 'display' ] ]     => run_command("Show");
+on [ [ 'delete', 'del', 'rm' ] ] => run_command("Delete");
+on history                       => run_command("History");
+
 on [ ['create', 'new'] ]         => run_command("Create");
-on [ ['show', 'display'] ]       => run_command("Show");
-on [ ['update', 'edit'] ]        => run_command("Update");
-on [ ['delete', 'del', 'rm'] ]   => run_command("Delete");
+on [ ['search', 'list', 'ls' ] ] => run_command("Search");
+on [ ['aliases', 'alias'] ]      => run_command('Aliases');
 on [ ['search', 'list', 'ls' ] ] => run_command("Search");
 on [ ['aliases', 'alias'] ]      => run_command('Aliases');
 
@@ -63,7 +68,6 @@ on log      => run_command("Log");
 on shell    => run_command("Shell");
 on export   => run_command('Export');
 on info     => run_command('Info');
-on history  => run_command('History');
 
 on push => sub {
     my $self = shift;
@@ -108,7 +112,7 @@ on qr/^(alias(?:es)?|config)?\s+(.*)/ => sub {
     # alternate syntax (preferred):
     # prophet alias "foo bar" "bar baz", prophet alias foo "bar baz",
     # prophet alias foo bar, etc.
-    elsif ( $arg =~ /^(?:"([^"]+)"|([^"\s]+))(?:\s+(?:"([^"]+)"|([^"\s]+)))?/ ) {
+    elsif ( $arg =~ /^(?:add |set )?\s*(?:"([^"]+)"|([^"\s]+))(?:\s+(?:"([^"]+)"|([^"\s]+)))?/ ) {
         my ($orig, $new) = grep { defined } ($1, $2, $3, $4);
         $orig = "'$orig'" if $cmd =~ /alias/ && $orig =~ /\./;
         if ( $new ) {
diff --git a/lib/Prophet/CLIContext.pm b/lib/Prophet/CLIContext.pm
index 0b4d8d9..fc89d14 100644
--- a/lib/Prophet/CLIContext.pm
+++ b/lib/Prophet/CLIContext.pm
@@ -143,7 +143,7 @@ The regex to use for matching the id argument (luid / uuid).
 
 =cut
 
-our $ID_REGEX = '^(?:\d+|[A-Za-z0-9\-\_]{22}|[0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12})$';
+our $ID_REGEX = '(?:\d+|[A-Za-z0-9\-\_]{22}|[0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12})';
 
 =head2 setup_from_args
 
@@ -172,14 +172,10 @@ sub require_uuid {
     my $self    = shift;
 
     if (!$self->has_uuid) {
-        my $type = $self->type;
         die "This command requires a luid or uuid (use --id to specify).\n";
     }
 }
 
-
-
-
 =head2 parse_args @args
 
 This routine pulls arguments (specified by --key=value or --key
@@ -201,10 +197,6 @@ sub parse_args {
     my @primary;
     push @primary, shift @args while ( $args[0] && $args[0] !~ /^-/ );
 
-    # "ticket show 4" should DWIM and "ticket show --id=4"
-    my $id_re = $ID_REGEX;
-    $self->set_arg( id => pop @primary ) if @primary && $primary[-1] =~ /$id_re/i;
-
     my $collecting_props = 0;
 
     $self->primary_commands( \@primary );
@@ -274,29 +266,59 @@ on the command-line, if possible. Being unable to figure out a uuid is fatal.
 sub set_type_and_uuid {
     my $self = shift;
 
+    $self->set_uuid;
+    $self->set_type;
+}
+
+sub set_uuid {
+    my $self = shift;
+
     if ( my $id = $self->delete_arg('id') ) {
         if ( $id =~ /^(\d+)$/ ) {
             $self->set_arg( luid => $id );
-        } else {
+        }
+        else {
             $self->set_arg( uuid => $id );
         }
     }
 
     if ( my $uuid = $self->delete_arg('uuid') ) {
         $self->uuid($uuid);
-    } elsif ( my $luid = $self->delete_arg('luid') ) {
+    }
+    elsif ( my $luid = $self->delete_arg('luid') ) {
         my $uuid = $self->handle->find_uuid_by_luid( luid => $luid );
         die "I have no UUID mapped to the local id '$luid'\n"
             if !defined($uuid);
         $self->uuid($uuid);
     }
+}
+
+sub set_type {
+    my $self = shift;
+
     if ( my $type = $self->delete_arg('type') ) {
         $self->type($type);
-    } elsif ( $self->primary_commands->[-2] ) {
+    }
+    # allowance for things like ticket show 77, where 'ticket' is the type
+    elsif (
+        $self->primary_commands->[-1] =~ qr/^$Prophet::CLIContext::ID_REGEX$/
+            && $self->primary_commands->[-3] ) {
+        $self->type( $self->primary_commands->[-3] );
+    }
+    elsif ( $self->primary_commands->[-2] ) {
         $self->type( $self->primary_commands->[-2] );
     }
 }
 
+sub set_id_from_primary_commands {
+    my $self = shift;
+
+    if ( (my $id = pop @{$self->primary_commands}) =~ $ID_REGEX ) {
+        $self->set_arg( id => $id );
+        $self->set_uuid;
+    }
+}
+
 __PACKAGE__->meta->make_immutable;
 no Any::Moose;
 
diff --git a/t/cli-arg-parsing.t b/t/cli-arg-parsing.t
index fe7a735..29f8c1d 100644
--- a/t/cli-arg-parsing.t
+++ b/t/cli-arg-parsing.t
@@ -1,6 +1,6 @@
 use warnings;
 use strict;
-use Prophet::Test tests => 47;
+use Prophet::Test tests => 44;
 use Test::Exception;
 
 use File::Temp qw'tempdir';
@@ -80,13 +80,6 @@ is($context->arg_names, 0, 'no args were set');
 is($context->prop_names, 0, 'no props were set');
 reset_context($context);
 
-diag('primary commands only, grabbing uuid from the CLI');
-$context->parse_args(qw(show 10));
-is_deeply($context->primary_commands, [ 'show' ], 'primary commands are correct');
-is($context->arg('id'), '10', 'id was grabbed from primary commands');
-is($context->prop_names, 0, 'no props were set');
-reset_context($context);
-
 diag('primary commands + args with no values');
 $context->parse_args(qw(show --verbose --test));
 is_deeply($context->primary_commands, [ 'show' ], 'primary commands are correct');
@@ -166,7 +159,7 @@ reset_context($context);
 # XXX other errors?
 
 diag('put it all together with setup_from_args');
-$context->setup_from_args( 'bug', 'show', $luid );
+$context->setup_from_args( 'bug', 'show', '--id', $luid );
 is_deeply($context->primary_commands, [ 'bug', 'show' ],
     'primary commands are correct');
 is($context->uuid, $uuid, 'uuid is correct');
diff --git a/t/cli.t b/t/cli.t
index d13309c..493bfc6 100644
--- a/t/cli.t
+++ b/t/cli.t
@@ -1,11 +1,26 @@
 #!/usr/bin/perl
 use warnings;
 use strict;
-use Prophet::Test tests => 2;
+use Prophet::Test tests => 9;
 
 as_alice {
     run_command(qw(init));
     like(run_command(qw(create --type Bug -- --status new --from alice)), qr/Created Bug/, "Created a record as alice");
+
     like(run_command(qw(show 1 --type Bug --batch)), qr/id: 1/, "'show 1' dwims");
-};
+    like(run_command(qw(display 1 --type Bug --batch)), qr/id: 1/, "'display 1' dwims");
+
+    like(run_command(qw(update 1 --type Bug --batch -- status=open)),
+        qr/Bug 1 \(.+\) updated/, "'update 1' dwims");
+    like(run_command(qw(edit 1 --type Bug --batch -- status=new)),
+        qr/Bug 1 \(.+\) updated/, "'edit 1' dwims");
 
+    like(run_command(qw(history 1 --type Bug --batch)), qr/^ alice\@example.com/, "'history 1' dwims");
+
+    like(run_command(qw(delete 1 --type Bug --batch)), qr/Bug (.+) deleted/, "'delete 1' dwims");
+    run_command(qw(create --type Bug -- --status new --from alice));
+    like(run_command(qw(del 2 --type Bug --batch)), qr/Bug (.+) deleted/, "'del 2' dwims");
+    run_command(qw(create --type Bug -- --status new --from alice));
+    like(run_command(qw(rm 3 --type Bug --batch)), qr/Bug (.+) deleted/, "'rm 3' dwims");
+
+};

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list