Skip to content

Commit

Permalink
Add the ability to work with MX::ConfigFromFile, just like MX::Getopt
Browse files Browse the repository at this point in the history
(would be nice MX::Getopt made this part separate from new_with_options
so it can be reused)
  • Loading branch information
Daisuke Maki committed Jul 16, 2008
1 parent fa6500a commit b52abd9
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 2 deletions.
24 changes: 22 additions & 2 deletions lib/MooseX/App/Cmd/Command.pm
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,36 @@ has app => (
sub _process_args {
my ( $class, $args, @params ) = @_;

my $config_from_file;
if($class->meta->does_role('MooseX::ConfigFromFile')) {
local @ARGV = @ARGV;

my $configfile;
my $opt_parser = Getopt::Long::Parser->new( config => [ qw( pass_through
) ] );
$opt_parser->getoptions( "configfile=s" => \$configfile ); if(!defined $configfile) {
my $cfmeta = $class->meta->find_attribute_by_name('configfile');
$configfile = $cfmeta->default if $cfmeta->has_default;
}

if(defined $configfile) {
$config_from_file = $class->get_config_from_file($configfile);
}
}

my %processed = $class->_parse_argv(
argv => $args,
options => [ $class->_attrs_to_options() ],
options => [ $class->_attrs_to_options( $config_from_file ) ],
);

return (
$processed{params},
$processed{argv},
usage => $processed{usage},
%{ $processed{params} }, # params from CLI are also fields in MooseX::Getopt
# params from CLI are also fields in MooseX::Getopt
%{ $config_from_file ?
{ %$config_from_file, %{$processed{params}} } :
$processed{params} },
);
}

Expand Down
45 changes: 45 additions & 0 deletions t/configfile.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!perl -T

use strict;
use warnings;
use Test::More;

BEGIN
{
eval {
require MooseX::ConfigFromFile;
require YAML;
};
if ($@) {
plan( skip_all => "These tests require MooseX::ConfigFromFile and YAML" );
} else {
plan( tests => 2 );
}
}

use lib 't/lib';
use Test::ConfigFromFile;

my $cmd = Test::ConfigFromFile->new;

{
local @ARGV = qw(moo);
eval { $cmd->run };

like(
$@,
qr/Required option missing/,
"command died with the correct string",
);
}

{
local @ARGV = qw(moo --configfile=t/lib/Test/ConfigFromFile/config.yaml);
eval { $cmd->run };

like(
$@,
qr/cows go moo1 moo2 moo3/,
"command died with the correct string",
);
}
6 changes: 6 additions & 0 deletions t/lib/Test/ConfigFromFile.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package Test::ConfigFromFile;
use Moose;

extends qw(MooseX::App::Cmd);

1;
34 changes: 34 additions & 0 deletions t/lib/Test/ConfigFromFile/Command/moo.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package Test::ConfigFromFile::Command::moo;
use Moose;
use YAML();

extends qw(MooseX::App::Cmd::Command);
with 'MooseX::ConfigFromFile';

=head1 NAME
Test::MyCmd::Command::moo - reads from config file
=cut

has 'moo' => (
isa => "ArrayRef",
is => "ro",
required => 1,
auto_deref => 1,
documentation => "required option field",
);

sub run {
my ($self, $opt, $arg) =@_;

die ("cows go " . join(' ', $self->moo));
}

sub get_config_from_file {
my ($self, $file) = @_;

return YAML::LoadFile($file);
}

1;
4 changes: 4 additions & 0 deletions t/lib/Test/ConfigFromFile/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
moo:
- moo1
- moo2
- moo3

0 comments on commit b52abd9

Please sign in to comment.