@@ -1,14 +1,25 @@
+0.09 Mon Feb 15 09:35:57 GMT-3 2010
+ - Minor POD correction
+ - Added IPC::Run and Moose 0.90 to dependencies
+
+0.08 Fri Feb 12 03:29:31 GMT-3 2010
+ - Added README
+ - Explicitly using Carp::confess instead of relying on Moose exports
+
+0.07 Tue Feb 9 15:50:28 GMT 2010
+ - (somewhat belatedly) removed 'get_attribute_map' (deprecated in Moose 0.93)
+
0.06 Tue Oct 13 15:22:06 BST 2009
- Attributes starting with '_' are ignored
0.05 Tue Jun 2 17:27:38 BST 2009
- Added POD
-
+
- Added trait attribute: 'cmdopt_env'
- allow ENV variables to be set rather than command line params
-
+
- private: _attr_name_to_cmd_options()
- - changed to _attr_to_cmd_options (i.e. attribute not attribute name)
+ - changed to _attr_to_cmd_options (i.e. attribute not attribute name)
to provide more flexibility
0.04 Fri Dec 5 10:43:01 GMT 2008
@@ -18,12 +29,12 @@
0.03 2008-10-22 19:17:00
- Added: attribute trait:
- lib/MooseX/Role/Cmd/Meta/Attribute/Trait.pm
-
+
- Added: tests and test/lib
- t/lib/Test/Cmd/Dir.pm
- t/lib/Test/Cmd/DirWithTraits.pm
- t/traits.t
-
+
- Changed: lib/MooseX/Role/Cmd.pm
- Added the attribute trait
- Added extra logic to decide how to call args '--verbose' or '-v'
@@ -3,11 +3,12 @@ package MooseX::Role::Cmd;
use strict;
use warnings;
+use Carp ();
use IPC::Cmd ();
use Moose::Role;
use MooseX::Role::Cmd::Meta::Attribute::Trait;
-our $VERSION = '0.06';
+our $VERSION = '0.09';
=head1 NAME
@@ -34,9 +35,9 @@ Use it somewhere else:
use Cmd::Perl;
my $perl = Cmd::Perl->new(e => q{'print join ", ", @ARGV'});
-
+
print $perl->run(qw/foo bar baz/);
-
+
# prints the STDOUT captured from running:
# perl -e 'print join ", ", @ARGV' foo bar baz
@@ -51,7 +52,7 @@ command strings which are passed to L<IPC::Cmd>.
=head2 $cmd->bin_name
Sets the binary executable name for the command you want to run. Defaults
-the to last part of the class name.
+the to last part of the class name, lower-cased.
=cut
@@ -114,18 +115,18 @@ sub run {
my $cmd = $self->bin_name;
my $full_path;
if ( !( $full_path = IPC::Cmd::can_run($cmd) ) ) {
- confess qq{couldn't find command '$cmd'};
+ Carp::confess(qq{couldn't find command '$cmd'});
}
-
+
# build full list of cmd args from attrs
@args = $self->cmd_args( @args );
-
+
#warn "CMD: " . $full_path . " " . join (" ", map { "'$_'" } @args );
my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
IPC::Cmd::run( command => [ $full_path, @args ] );
if ( !$success ) {
- confess "error running '$full_path': " . $error_code;
+ Carp::confess("error running '$full_path': " . $error_code);
}
$self->stdout($stdout_buf);
@@ -144,9 +145,12 @@ sub cmd_args {
# exclude this role's attributes from the flag list
# could use custom metaclasses and introspection, but this will do for now
- my %non_flag = map { $_ => 1 } __PACKAGE__->meta->get_attribute_list;
- my @flag_attrs = grep { !$non_flag{$_->name} } values %{ $self->meta->get_attribute_map };
-
+ my %non_flag = map { $_ => 1 } __PACKAGE__->meta->get_attribute_list;
+
+ my @flag_attrs = grep { !$non_flag{$_->name} }
+ map { $self->meta->get_attribute($_) }
+ $self->meta->get_attribute_list;
+
#####
# IS: 2008/10/15
# Changed the following to make a start on the suggestion above...
@@ -163,14 +167,14 @@ sub cmd_args {
=head2 Setting the Executable
-By default the name of the binary executable is taken from the last part of the class name
+By default the name of the binary executable is taken from the last part of the class name
(in lower case). The path is set during the L<run> method by scanning through your current
PATH for the given executable (see also the 'can_run' function from L<IPC::Cmd>)
package MyApp::Commands::Scanner;
use Moose;
with 'MooseX::Role::Cmd';
-
+
$cmd = MyApp::Commands::Scanner->new();
$cmd->bin_name
# /path/to/scanner
@@ -206,18 +210,18 @@ These points are illustrated in the following example:
package MyApp::Commands::Scanner;
use Moose;
with 'MooseX::Role::Cmd';
-
+
has 'i' => ( is => 'rw', isa => 'Str', default => 'input.txt' );
has 'out' => ( is => 'rw', isa => 'Str' );
has 'verbose' => ( is => 'rw', isa => 'Bool', default => 1 );
has 'level' => ( is => 'rw', isa => 'Int' );
has 'option' => ( is => 'rw', isa => 'Str' );
-
+
has '_internal' => ( is => 'ro', isa => Str, reader => internal, default => 'foo' );
# attribute names starting with '_' are not included
-
+
$scanner = MyApp::Commands::Scanner->new( output => '/tmp/scanner.log', level => 5 );
-
+
$scanner->run;
# /path/to/scanner -i input.txt --out /tmp/scanner.log --verbose --level 5
@@ -244,7 +248,7 @@ form of multi-character options).
=head3 cmdopt_name
This lets you completely override the option name with whatever string you want
-
+
has 'option' => ( traits => [ 'CmdOpt' ],
isa => 'Bool',
cmdopt_name => '+foo'
@@ -253,7 +257,7 @@ This lets you completely override the option name with whatever string you want
=head3 cmdopt_env
-This will set an environment variable with the attribute name/value rather than pass
+This will set an environment variable with the attribute name/value rather than pass
it along as a command line param
has 'home_dir' => ( traits => [ 'CmdOpt' ],
@@ -261,7 +265,7 @@ it along as a command line param
cmdopt_env => 'APP_HOME'
default => '/my/app/home'
);
-
+
# ENV{APP_HOME} = /my/app/home
See L<MooseX::Role::Cmd::Meta::Attribute::Trait>
@@ -277,18 +281,18 @@ to the given attribute name.
sub _attr_to_cmd_options {
my ( $self, $attr ) = @_;
-
+
my $attr_name = $attr->name;
-
+
# decide the default settings
my $opt_prefix = length( $attr_name ) == 1 ? '-' : '--';
my $opt_name = $attr_name;
-
+
my $attr_value = $attr->get_value( $self );
-
+
# override defaults with Traits
if ( $attr->does('MooseX::Role::Cmd::Meta::Attribute::Trait') ) {
-
+
# deal with $ENV
if ($attr->has_cmdopt_env) {
my $env_key = $attr->cmdopt_env;
@@ -302,32 +306,32 @@ sub _attr_to_cmd_options {
if ($attr->has_cmdopt_prefix) {
$opt_prefix = $attr->cmdopt_prefix;
}
-
+
if ($attr->has_cmdopt_name) {
$opt_prefix = ''; # name overrides prefix
$opt_name = $attr->cmdopt_name;
}
}
-
+
# create the full option name
my $opt_fullname = $opt_prefix . $opt_name;
-
+
my @options = ();
if ( $attr->type_constraint->is_a_type_of( 'Bool' ) ) {
push @options, ( $opt_fullname )
if $attr_value; # only add if attr is true
}
else {
-
+
if ( defined $attr_value # only add if attr value is defined
- &&
+ &&
$attr_name !~ / ^ _ /xms # and attr name doesn't start with '_'
)
{
push @options, ( $opt_fullname, $attr_value )
}
}
-
+
return wantarray ? @options : \@options;
}