[Bps-public-commit] Prophet - A disconnected, replicated p2p database branch, master, updated. 1d33fbd0dc3fc7a9984cbfa1c3698f17ee4fa122
jesse
jesse at bestpractical.com
Tue Feb 17 11:22:23 EST 2009
The branch, master has been updated
via 1d33fbd0dc3fc7a9984cbfa1c3698f17ee4fa122 (commit)
from 2085145865b59ba5eca002ffbb014dd353171832 (commit)
Summary of changes:
lib/Prophet/CLI/Command.pm | 41 ++++++-----------------------------
lib/Prophet/CLI/Command/Aliases.pm | 2 +-
lib/Prophet/CLI/Command/Merge.pm | 2 +-
lib/Prophet/CLI/Command/Pull.pm | 2 +-
lib/Prophet/CLI/Command/Server.pm | 2 +-
lib/Prophet/CLI/Command/Settings.pm | 2 +-
lib/Prophet/CLI/Command/Show.pm | 2 +-
lib/Prophet/CLI/Command/Update.pm | 2 +-
t/cli-arg-translation.t | 9 +++----
9 files changed, 18 insertions(+), 46 deletions(-)
- Log -----------------------------------------------------------------
commit 1d33fbd0dc3fc7a9984cbfa1c3698f17ee4fa122
Author: Jesse Vincent <jesse at bestpractical.com>
Date: Tue Feb 17 11:22:05 2009 -0500
Rework cli arg translation to use functions rather than a stash
diff --git a/lib/Prophet/CLI/Command.pm b/lib/Prophet/CLI/Command.pm
index b5e3785..7671040 100644
--- a/lib/Prophet/CLI/Command.pm
+++ b/lib/Prophet/CLI/Command.pm
@@ -23,18 +23,13 @@ has context => (
);
-our %ARG_TRANSLATIONS = (
- 'v' => 'verbose',
- 'a' => 'all',
-);
sub ARG_TRANSLATIONS {
my $self = shift;
-
- return \%ARG_TRANSLATIONS if !@_;
- %ARG_TRANSLATIONS = %{ $_[0] };
+ return ( 'v' => 'verbose',
+ 'a' => 'all' );
}
-=head2 register_arg_translations
+=head2 Registering argument translations
This is the Prophet CLI's way of supporting short forms for arguments,
e.g. you want to let '-v' be able to used for the same purpose as
@@ -44,7 +39,7 @@ to have short commands.
To use, have your command subclass do:
- __PACKAGE__->register_arg_translations( f => 'file' );
+ around ARG_TRANSLATIONS => sub { shift->(), f => 'file' };
You can register as many translations at a time as you want.
The arguments will be translated when the command object is
@@ -53,34 +48,12 @@ table, it is overwritten with the new value.
=cut
-sub register_arg_translations {
- my $class = shift;
- my %args = @_;
-
- $class->ARG_TRANSLATIONS({ %{$class->ARG_TRANSLATIONS}, %args });
-}
-
-=head2 clear_arg_translations
-
-Don't like the defaults? Get rid of 'em.
-
-Example:
-
- __PACKAGE__->clear_arg_translations;
-
-=cut
-
-sub clear_arg_translations {
- my $class = shift;
-
- $class->ARG_TRANSLATIONS({});
-}
-
sub _translate_args {
my $self = shift;
+ my %translations = $self->ARG_TRANSLATIONS;
- for my $arg (keys %{$self->ARG_TRANSLATIONS}) {
- $self->set_arg($self->ARG_TRANSLATIONS->{$arg}, $self->arg($arg))
+ for my $arg (keys %translations) {
+ $self->set_arg($translations{$arg}, $self->arg($arg))
if $self->has_arg($arg);
}
}
diff --git a/lib/Prophet/CLI/Command/Aliases.pm b/lib/Prophet/CLI/Command/Aliases.pm
index 7c372c6..5306fa9 100644
--- a/lib/Prophet/CLI/Command/Aliases.pm
+++ b/lib/Prophet/CLI/Command/Aliases.pm
@@ -5,7 +5,7 @@ use Params::Validate qw/validate/;
extends 'Prophet::CLI::Command';
with 'Prophet::CLI::TextEditorCommand';
-__PACKAGE__->register_arg_translations( a => 'add', d => 'delete', s => 'show' );
+around ARG_TRANSLATIONS => sub { shift->(), a => 'add', d => 'delete', s => 'show' };
sub run {
my $self = shift;
diff --git a/lib/Prophet/CLI/Command/Merge.pm b/lib/Prophet/CLI/Command/Merge.pm
index c354ebf..0a6c96b 100644
--- a/lib/Prophet/CLI/Command/Merge.pm
+++ b/lib/Prophet/CLI/Command/Merge.pm
@@ -5,7 +5,7 @@ extends 'Prophet::CLI::Command';
has source => ( isa => 'Prophet::Replica', is => 'rw');
has target => ( isa => 'Prophet::Replica', is => 'rw');
-__PACKAGE__->register_arg_translations( f => 'force' );
+around ARG_TRANSLATIONS => sub { shift->(), f => 'force' };
sub run {
my $self = shift;
diff --git a/lib/Prophet/CLI/Command/Pull.pm b/lib/Prophet/CLI/Command/Pull.pm
index 6c6d975..9e64fbf 100644
--- a/lib/Prophet/CLI/Command/Pull.pm
+++ b/lib/Prophet/CLI/Command/Pull.pm
@@ -2,7 +2,7 @@ package Prophet::CLI::Command::Pull;
use Any::Moose;
extends 'Prophet::CLI::Command::Merge';
-__PACKAGE__->register_arg_translations( l => 'local' );
+around ARG_TRANSLATIONS => sub { shift->(), l => 'local' };
sub run {
my $self = shift;
diff --git a/lib/Prophet/CLI/Command/Server.pm b/lib/Prophet/CLI/Command/Server.pm
index 8eebe1c..ecb466e 100644
--- a/lib/Prophet/CLI/Command/Server.pm
+++ b/lib/Prophet/CLI/Command/Server.pm
@@ -2,7 +2,7 @@ package Prophet::CLI::Command::Server;
use Any::Moose;
extends 'Prophet::CLI::Command';
-__PACKAGE__->register_arg_translations( p => 'port', w => 'writable' );
+around ARG_TRANSLATIONS => sub { shift->(), p => 'port', w => 'writable' };
use Prophet::Server;
diff --git a/lib/Prophet/CLI/Command/Settings.pm b/lib/Prophet/CLI/Command/Settings.pm
index 54b019d..393ae6f 100644
--- a/lib/Prophet/CLI/Command/Settings.pm
+++ b/lib/Prophet/CLI/Command/Settings.pm
@@ -6,7 +6,7 @@ use JSON;
extends 'Prophet::CLI::Command';
with 'Prophet::CLI::TextEditorCommand';
-__PACKAGE__->register_arg_translations( s => 'show' );
+around ARG_TRANSLATIONS => sub { shift->(), s => 'show' };
sub run {
my $self = shift;
diff --git a/lib/Prophet/CLI/Command/Show.pm b/lib/Prophet/CLI/Command/Show.pm
index 8fbb159..82182e5 100644
--- a/lib/Prophet/CLI/Command/Show.pm
+++ b/lib/Prophet/CLI/Command/Show.pm
@@ -4,7 +4,7 @@ use Params::Validate;
extends 'Prophet::CLI::Command';
with 'Prophet::CLI::RecordCommand';
-__PACKAGE__->register_arg_translations( 'b' => 'batch' );
+around ARG_TRANSLATIONS => sub { shift->(), 'b' => 'batch' };
sub run {
my $self = shift;
diff --git a/lib/Prophet/CLI/Command/Update.pm b/lib/Prophet/CLI/Command/Update.pm
index 85e22ee..b0820c1 100644
--- a/lib/Prophet/CLI/Command/Update.pm
+++ b/lib/Prophet/CLI/Command/Update.pm
@@ -3,7 +3,7 @@ use Any::Moose;
extends 'Prophet::CLI::Command';
with 'Prophet::CLI::RecordCommand';
-__PACKAGE__->register_arg_translations( e => 'edit' );
+around ARG_TRANSLATIONS => sub { shift->(), e => 'edit' };
sub edit_record {
my $self = shift;
diff --git a/t/cli-arg-translation.t b/t/cli-arg-translation.t
index a22f9ab..74ae7bc 100644
--- a/t/cli-arg-translation.t
+++ b/t/cli-arg-translation.t
@@ -1,7 +1,6 @@
use warnings;
use strict;
-use Test::More tests => 7;
-
+use Prophet::Test tests => 7;
use File::Temp qw'tempdir';
# test coverage for Prophet::CLI::Command arg translation
@@ -25,8 +24,8 @@ my $command = Prophet::CLI::Command->new( handle => $cxn, context => $context );
is($command->has_arg('all'), 1, 'translation of -a to --all correct');
is($command->has_arg('verbose'), 1, 'translation of -v to --verbose correct');
-diag('Checking a subclass arg translation (with value)');
use_ok('Prophet::CLI::Command::Server');
$context->set_arg( p => '8080');
-my $server = Prophet::CLI::Command->new( handle => $cxn, context => $context );
-is($command->context->arg('port'), '8080', 'translation of -p to --port correct');
+my $server = Prophet::CLI::Command::Server->new( handle => $cxn, context => $context );
+diag('Checking a subclass arg translation (with value)');
+is($server->context->arg('port'), '8080', 'translation of -p to --port correct');
-----------------------------------------------------------------------
More information about the Bps-public-commit
mailing list