[Bps-public-commit] r16794 - in Prophet: .
sunnavy at bestpractical.com
sunnavy at bestpractical.com
Sun Nov 9 21:10:49 EST 2008
Author: sunnavy
Date: Sun Nov 9 21:10:48 2008
New Revision: 16794
Modified:
Prophet/ (props changed)
Prophet/trunk/lib/Prophet/CLI.pm
Log:
r17651 at sunnavys-mb: sunnavy | 2008-11-10 09:42:09 +0800
added alias support.
parse_args happens before dispatch, so there is no good and clean way to set
aliases in different command modules since we do not know the modules then.
so we implemented in a different, also more convenient way:
store aliases in prophetrc, then substitute the cmd and rerun if found any
match at very early stage.
e.g. if there's
'alias pull -a = pull --all' in config
then 'pull -a' will be run as 'pull --all'
we will add aliases and alias cmd soon so we can edit aliases easily.
Modified: Prophet/trunk/lib/Prophet/CLI.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/CLI.pm (original)
+++ Prophet/trunk/lib/Prophet/CLI.pm Sun Nov 9 21:10:48 2008
@@ -84,12 +84,38 @@
my $self = shift;
my @args = (@_);
- # really, we shouldn't be doing this stuff from the command dispatcher
-
- $self->context(Prophet::CLIContext->new(app_handle => $self->app_handle));
+ # find the first alias that matches, rerun the aliased cmd
+ # note: keys of aliases are treated as regex,
+ # we need to substitute $1, $2 ... in the value if there's any
+
+ my $ori_cmd = join ' ', @args;
+ my $cmd = $ori_cmd;
+ my $aliases = $self->app_handle->config->aliases;
+ for my $key ( keys %$aliases ) {
+ if ( $cmd =~ /$key/ ) {
+ my $value = $aliases->{$key};
+ no strict 'refs';
+
+ # we want to start at index 1, as @+
+ my @captures = ( undef, map { ${$_} } 1 .. @+ );
+ $value =~ s/\$$_\b/$captures[$_]/g for 1 .. @+;
+ $cmd =~ s/$key/$value/;
+
+ # we don't want to recursively call if people stupidly write
+ # alias pull --local = pull --local
+ if ( $cmd ne $ori_cmd ) {
+ $self->run_one_command( split /\s+/, $cmd );
+ return;
+ }
+ }
+ }
+
+ # really, we shouldn't be doing this stuff from the command dispatcher
+ $self->context(
+ Prophet::CLIContext->new( app_handle => $self->app_handle ) );
$self->context->setup_from_args(@args);
- my $dispatcher = $self->dispatcher_class->new(cli => $self);
+ my $dispatcher = $self->dispatcher_class->new( cli => $self );
my $command = join ' ', @{ $self->context->primary_commands };
my $dispatch = $dispatcher->dispatch($command);
More information about the Bps-public-commit
mailing list